MUHAMMED ARIF
Posted on January 5, 2024
In my previous post, I discussed the process of building a simple 'Hello, World!' image using Docker, and the support I received was truly awesome. Thanks, guys! If you haven't had a chance to read that post yet, please check it out here.
However, I encountered some significant issues in that project, which I detailed in the last section of the post. One notable problem is the final image size, which ballooned to around 900MB. This is quite surprising for a 'Hello, World!' application.
Today we are try to reduce our image size . Lets Gooooo..
Guys Don't Forget Like this post
before going to code. i have a request to you. if like this blogs. just share your feedback on linkedin or instagram. links on end of the post
Lets Start from our previous post.
# Base Image
FROM golang:latest
# Set the Current Working Directory inside the container
WORKDIR /app
# Copy everything from the current directory to the PWD(Present Working Directory) inside the container
COPY . .
# Download all the dependencies
RUN go mod download
# Build the Go app
RUN go build -o main .
# Expose port 8080 to the outside world
EXPOSE 8080
# Command to run the executable
CMD ["./main"]
This is our last created project's docker file
Let's Change This
# Base Image
FROM golang:latest as builder
# Set the Current Working Directory inside the container
WORKDIR /app
# Copy everything from the current directory to the PWD(Present Working Directory) inside the container
COPY . .
# Download all the dependencies
RUN go mod download
# Build the Go app
RUN go build -o main .
# Start fresh from a smaller image
FROM scratch
# Set the Current Working Directory inside the container
WORKDIR /root
# Copy the Pre-built binary file from the previous stage
COPY --from=builder /app/main .
# Expose port 8080 to the outside world
EXPOSE 8080
# Command to run the executable
CMD ["./main"]
I Changed some parts in our old file. changes is: ✒️
FROM golang:latest as builder
This line creates a new stage named "builder." It's like setting up a separate workspace to build our Go application.
FROM scratch
This line starts a fresh, minimal image from scratch for our final Docker image.
COPY --from=builder /app/main .
Instead of copying everything from the original workspace, we only copy the essential built binary (main) from the "builder" stage.
WORKDIR /root
In the final image, we set a smaller working directory to keep things tidy.
Build the Optimized Docker Image 📦
Finally we will complete our coding part. let's build our optimized Docker image. Open a terminal and navigate to the directory containing your Dockerfile. Run the following command: ⌨️
docker build -t {image_name} .
This command tells Docker to build an image based on the instructions in your Dockerfile and tag it with the name "optimized-hello-world." The . at the end indicates that the Dockerfile is in the current directory.
Verify the Optimized Image Size ✅
After completing the build process, you can verify the size of your optimized Docker image by running the following command in your terminal: ⌨️
docker images
This command will display a list of Docker images on your system. Look for the image with the tag "hello-world"
. You should see the reduced size, confirming that you've successfully optimized your Docker image.
As you can see in the image, the size of our optimized image is now only 7MB. This is a significant reduction from the initial 900MB size. Great job! 🚀
Finally just run and verify this image. That is your task 😇😇
If you like my posts connect and send your feedback on
Linkedin and Instagram
Happy Codinggggggggg...!👨💻👩💻
Posted on January 5, 2024
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.