Prodinit Software Solutions
LinkedinGithub
  • Prodinit's Engineering Blog
  • aws
    • Ways to delete AWS ECR images
    • Enable Cloudwatch Alarm and SNS Topic for AWS Billing Alert
    • A-Z of AWS VPC and other services - with Terraform
    • How Internet Works?
    • How to download/view code running in your lambda functions?
  • backend engineering
    • What is idempotency?
  • databases
    • Database Optimisation - Indexing vs Sharding with Postgres and Django ORM examples
  • devops
    • Docker Best Practices
    • Docker Networking - Bridge vs Host vs Overlay
    • A comparision between multistage build and singlestage build in Docker
    • Things to remember before building your first blue/green deployment in Kubernetes
    • How to export env variables in circleci? (You wont find this in circleci documentation)
  • frontend engineering
    • Host your static website with s3, CloudFront, Route53, and domain from godaddy in 4 easy steps
  • product management
    • You'll fail as a lead developer, here's why ...
  • python
    • Achieve Peak Performance in Python
    • Play with List of dictionaries in Python
    • How we develop a custom autoscaling metrics based on number of tasks in the queues?
  • Contact Us
    • Who are we?
    • Work with us.
Powered by GitBook
On this page
  • Single stage dockerfile
  • Command to build the image
  • Multi stage docker build
  • Command to build the image
  • Enjoyed the blog? If so, you'll appreciate collaborating with the minds behind it as well.
  1. devops

A comparision between multistage build and singlestage build in Docker

PreviousDocker Networking - Bridge vs Host vs OverlayNextThings to remember before building your first blue/green deployment in Kubernetes

Last updated 1 year ago

Image size is one of the essential considerations while build a production grade docker image. Container images often include development tools, libraries and other necessary application dependencies. These tools and dependencies can drastically increase the image size.

In this blog, we will see a size comparision between a single stage docker image and a multi stage docker image of a simple hello-world golang application.

Before build the image, Lets build a simple golang application.

Single stage dockerfile

FROM golang:alpine

ENV GO111MODULE=off
ENV CGO_ENABLED=0

WORKDIR /app
COPY ../app/ /app/
RUN go build -o .
EXPOSE 8080
CMD ./app

Command to build the image

docker build -t singlestage:latest -f Dockerfile.singlestage .

Using the above dockerfile code, we can create a docker image of the golang application with only a single stage. Lets check the size of the image.

Did you notice 247MB? That too for just a hello world application. Isn't it way too heavy. Multistage builds are here to rescue us.

Multi stage docker build

############# Stage 1 #####################
FROM golang:alpine AS BUILD

ENV GO111MODULE=off
ENV CGO_ENABLED=0
WORKDIR /app
COPY ../app/ /app/
RUN go build -o .

############# Stage 2 #####################
FROM scratch
COPY --from=BUILD /app/app /app/app
ENTRYPOINT [ "/app/app" ]

Command to build the image

docker build -t multistage:latest -f Dockerfile.multistage .

Using the above dockerfile code, we can create a docker image of the golang application with multi stage builds.

###1.85MB docker image of the same hello world application.

74.89% lighter images.

We're constructing the image through a two-stage process. Initially, we employ a Golang base image, embed our code within it, and compile our executable file, App. Moving on to the next stage, we utilize a fresh Alpine base image, transferring the binary created in the previous step to this new stage.

This is just a small example. Imagine this for a large production grade application. Multistage builds are magical, if you haven’t explored it already – go ahead and do it.

Tags

Enjoyed the blog? If so, you'll appreciate collaborating with the minds behind it as well.

Written by -

Dishant Sethi