Generic Category (English)728x90
Introduction to TensorFlow for AI, ML, and DL.
Blockchain Council announces 25% off SITEWIDE,sale is valid until 20th May 2019.Use Coupon Code - 2yrsblockcouncil Blockchain Certification

Docker tutorial – Create Docker Images using Multi-stage builds

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

  • Containers are an abstraction at the app layer that packages code and dependencies together. Multiple containers can run on the same machine and share the OS kernel with other containers, each running as isolated processes in user space. Containers take up less space than VMs (container images are typically tens of MBs in size), and start almost instantly.
  • A container image is a lightweight, stand-alone, executable package of a piece of software that includes everything needed to run it: code, runtime, system tools, system libraries, settings.
  • Containers run apps natively on the host machine’s kernel. They have better performance characteristics than virtual machines that only get virtual access to host resources through a hypervisor. Containers can get native access, each one running in a discrete process, taking no more memory than any other executable.

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 earlier posts for understanding on Docker & how to install and containerize applications.

Why Multi-stage builds?

In your project,you would have two or more Dockerfiles i.e., one for Development & other 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.

Get your Zoom Pro account for as low as $12.49/month with annual plan ($14.99/month without). Host up to 100 participants on HD video, record meetings to the cloud, share content, annotate, chat, and more!

With 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 :


Subscribe to Emails

More than 1000+ subscribers across the world trust & read Upnxtblog articles. Subscribe to emails updates!
* indicates required
FROM golang:1.7.3
WORKDIR /go/src/
RUN go get -d -v  
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/ .
CMD ["./app"]  

Step #1. Setup Docker

  1. From the docker site,install the latest version of the docker for your platform.Docker is available in two editions: Community Edition (CE) and Enterprise Edition (EE). Docker Community Edition (CE) is ideal for developers and small teams looking to get started with Docker and experimenting with container-based apps. Docker Enterprise Edition (EE) is designed for enterprise development and IT teams who build, ship, and run business critical applications in production at scale.
  2. Once the installation of docker is over,check the installation by running following command docker run hello-world:


    Image- docker hello world

  3. Run docker –version to check the version of the docker you’re running.


    Image- check docker version

OK, now we have got the docker setup,next step is to define the docker container.

Step #2. Create Multi-stage build Dockerfile for our container

List images using docker images command

Image – List images using docker images command

Before we start,use docker images command to check the list of images. GRE 728x90

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 a optimised Docker Image.

FROM golang:1.6-alpine

RUN mkdir /app
ADD . /app/
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o main .

# Second Stage
FROM alpine
CMD ["/app"]

# Copy from first stage
COPY --from=0 /app/main /app

Advance your IT skills and career with Cisco training from the Cisco Learning Network Store.

Step #3. Build Docker Image

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 .
Build container image using docker build command

Image – Build container image using docker build command

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.

List the images using docker images command

Image – List the images using docker images command

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!

Additional Resources:

Also published on Medium.

Docker tutorial - Create Docker Images using Multi-stage builds
Article Name
Docker tutorial - Create Docker Images using Multi-stage builds
In this article, 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.
Publisher Name
Publisher Logo
%d bloggers like this: