Building a Go Application With Docker: Optimized Dockerfile
In this article, we will walk through the creation of an optimized Dockerfile for building and running a Go application. This Dockerfile will focus on building only the binary of the Go application, resulting in a smaller and more efficient Docker image.
|
|
In this Dockerfile, we use a multi-stage build to optimize the final Docker image. Here’s a breakdown of what each section does:
-
Stage 1 (builder):
- We start from the official Golang image, specifically version 1.17.
- Set the working directory inside the container to
/app
. - Copy the Go application source code from your local directory into the container.
- Set the
CGO_ENABLED
environment variable to 0 to build a statically linked binary. - Fetch the project’s dependencies using
go get
. - Build the Go binary and place it in
/tmp/api-server
.
-
Stage 2 (minimal runtime image):
- We use the
scratch
image as the base image. This image is essentially empty, which makes our final image very small and lightweight. - Copy the binary (
api-server
) from the builder stage into/usr/bin/api-server
in the final image. - Define the default command to execute when the container starts, which is
["api-server", "start"]
.
- We use the
This Dockerfile optimizes the final Docker image size by separating the build environment from the runtime environment. It results in a minimal Docker image that contains only the Go binary and its necessary dependencies, making it efficient and suitable for production deployment.
For more details on using this Dockerfile and building a Go application with Docker, you can refer to the original article.
Remember to replace the COPY . .
line in the Dockerfile with the appropriate path to your Go application source code, as specified in your project structure.