From the last post, we have understood what is container & why do we use containers in general. Just to recap here are some of the key points
In this post, we are going to take look at optimizing the images using multi-stage builds. Multi-stage builds are a new feature requiring Docker 17.05 or higher on the daemon and client.
This quickstart assumes basic understanding of Docker concepts, please refer to earlier posts for understanding on Docker & how to install and containerize applications.
Quick Snapshot
In your project, you would have two or more Dockerfiles i.e., one for Development and another one for Production. One file would have the steps to build the binary and artifacts using a development container, the second would be optimized for production and not include any of the development tools.
With a multi-stage builds feature, you can use multiple FROM
statements in your Dockerfile. Each FROM
instruction can use a different base, and each of them begins a new stage of the build. You can selectively copy artifacts from one stage to another, leaving behind everything you don’t want in the final image. To show how this works, Let’s adapt the Dockerfile from the previous section to use multi-stage builds. Example below :
Dockerfile
:
FROM golang:1.7.3
WORKDIR /go/src/github.com/alexellis/href-counter/
RUN go get -d -v golang.org/x/net/html
COPY app.go .
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o app .
FROM alpine:latest
RUN apk --no-cache add ca-certificates
WORKDIR /root/
COPY --from=0 /go/src/github.com/alexellis/href-counter/app .
CMD ["./app"]
OK, now we have got the docker setup,next step is to define the docker container.
Before we start, use docker images
command to check the list of images.
Using your favorite editor, create a Multi-Stage Dockerfile. The first stage using the Golang SDK to build a binary. The second stage copies the resulting binary into an optimised Docker Image.
FROM golang:1.6-alpine
RUN mkdir /app
ADD . /app/
WORKDIR /app
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o main .
# Second Stage
FROM alpine
EXPOSE 80
CMD ["/app"]
# Copy from first stage
COPY --from=0 /app/main /app
Now that we have completed Dockerfile, next step is to build Docker image by docker build command
Build the image using the docker build command
below.
docker build -f m1docker -t golang-app .
The result will be two images. One untagged that was used for the first stage and the second is the target image.
Congrats! Today you’ve successfully built container image using Multi stage build.
When using multi-stage builds, you are not limited to copying from stages you created earlier in your Dockerfile. You can also use the COPY --from
instruction to copy from a separate image, either using the local image name, a tag available locally or on a Docker registry, or a tag ID. The Docker client pulls the image if necessary and copies the artifact from there. Example below
COPY --from=nginx:latest /etc/nginx/nginx.conf /nginx.conf
There is much more to the Docker platform than what was covered here, but now you would have got a good idea of the basics of building containers using multi-stage builds.
Like this post? Don’t forget to share it!
Of course, every site has different needs. In the end, however, there is one aspect…
In today's digital-first world, businesses must adopt effective strategies to stay competitive. Social media marketing…
62% of UX designers now use AI to enhance their workflows. Artificial intelligence (AI) rapidly…
The integration of artificial intelligence into graphic design through tools like Adobe Photoshop can save…
The cryptocurrency trading world has grown significantly in recent years, with automation playing a key…
The non-fungible token (NFT) market has witnessed explosive growth over the past few years, transforming…
This website uses cookies.