googleads
How To Create The Smallest Docker Image For Your Golang App
preloder
Golang Web Development

How To Create The Smallest Docker Image For Your Golang App

Keeping big sized docker image in golang app will always affect us the performance of the application. Today, on this blog, we’re planning to guide you to create the small Docker image for your golang app that remains small, secure and easy to load. Also will see how to deploy it into the docker container.

Prerequisites:

To install Docker https://docs.docker.com/install/ on your machine
Log in to a Docker registry https://docs.docker.com/registry/

Creating Golang Web Application:

Here I’m creating a sample golang web application which is an open source API i got from online so it would be easy for you to understand much better. 

Application structure:

├── Dockerfile
└── main.go

main.go:

 

Write A Dockerfile To Build A Docker Image:

Now, we need to write a Dockerfile for building Docker image with our golang web application. This Dockerfile contains the basic commands which are required to construct Docker image.

Dockerfile:

 

 

Build The Docker Image

Run the below command to build docker image, it creates the image from docker_imgs directory that builds Docker image.

 

The above command will take some time to process and create an image with name reddysai/docker_imgs.

List the docker images with detailed info using below command.

 

In the above list, our current build application showing very big size. Actually, our application adds just 11 MB to that. But, the bases are 776 MB. Because our application was compiled inside the container. So, it will install Go and package managers. It will build an image with static binaries and Golang dependent packages.

To reduce the size of our application, we have to build the image in multi stages.

Write A Dockerfile With Docker Multi-Stage Builds:

Here, I’m going to write new Dockerfile using Multi-stage builds. Multi-stage builds feature is available in Docker 17.05 version onwards so you can install and use it. It will mostly help us to optimize the Dockerfiles and makes it easy to analyze and maintain.

Dockerfile:

 

Now, re-build the application using below command.

 

After completing the latest build, you can see how small is our application now.

 

What i have modified in our build script is:

 

We are disabling cgo which gives us a static binary. And, also setting the OS to Linux on this container and we are using -a flag to rebuild all the packages in the docker container.

Run the docker image using below command,

 

After running this command, Now you can able to notice your running application and this container will expose an application from port 6060.

You can visit in your browser with http://localhost:6060/users to see it’s response to list all users.

Liked it? Similarly you find more informative blogs on our largest blog repository,  Stay updated with latest topics & tricks and don’t forget to subscribe us to get the latest updates from diverse technologies. Besides all, Post us your valuable thoughts in the comment section.
For any queries reach us via info@agiratech.com.

 

We offer Golang development services for building world-class enterprise apps. We have expertise in building most complex software solutions using Google’s Go language. Chat with us now and hire golang developers within 72 hours.

Turn your vision to magnificent reality With
Our Web and Mobile Solutions
The following two tabs change content below.

Reddy Sai

Young Senior Software Developer having 3+years of experience in Web development, skilled in Golang, Ruby, AngularJS, Ruby on Rails & NodeJS. With a handful of skills, he keeps thriving through all the Edge of Web development to become a perfect back end developer. Besides, this young techie will always buzz up & surprise us with his beautiful Movie collections.

Leave a Reply

Your email address will not be published. Required fields are marked *

[contact-form-7 id="120788" title="Web Page Form"]

Schedule Your Call