
- Docker - Home
- Docker - Overview
- Docker - Installing on Linux
- Docker - Installation
- Docker - Hub
- Docker - Images
- Docker - Containers
- Docker - Registries
- Docker - Compose
- Docker - Working With Containers
- Docker - Architecture
- Docker - Layers
- Docker - Container & Hosts
- Docker - Configuration
- Docker - Containers & Shells
- Docker - Dockerfile
- Docker - Building Files
- Docker - Public Repositories
- Docker - Managing Ports
- Docker - Web Server
- Docker - Commands
- Docker - Container Linking
- Docker - Data Storage
- Docker - Volumes
- Docker - Networking
- Docker - Security
- Docker - Toolbox
- Docker - Cloud
- Docker - Build Cloud
- Docker - Logging
- Docker - Continuous Integration
- Docker - Kubernetes Architecture
- Docker - Working of Kubernetes
- Docker - Generative AI
- Docker - Hosting
- Docker - Best Practices
- Docker - Setting Node.js
- Docker - Setting MongoDB
- Docker - Setting NGINX
- Docker - Setting ASP.Net
- Docker - Setting MySQL
- Docker - Setting Go
- Docker - Setting Rust
- Docker - Setting Apache
- Docker - Setting MariaDB
- Docker - Setting Jupyter
- Docker - Setting Portainer
- Docker - Setting Rstudio
- Docker - Setting Plex
- Docker Setting - Flame
- Docker Setting - PostgreSql
- Docker Setting - Mosquitto
- Docker Setting - Grafana
- Docker Setting - Nextcloud
- Docker Setting - Pawns
- Docker Setting - Ubuntu
- Docker Setting - RabbitMQ
- Docker - Setting Python
- Docker - Setting Java
- Docker - Setting Redis
- Docker - Setting Alpine
- Docker - Setting BusyBox
- Docker Setting - Traefik
- Docker Setting - WordPress
- Docker Useful Resources
- Docker - Quick Guide
- Docker - Useful Resources
- Docker - Discussion
Docker - Setting Go
Go, often called Golang, is a statically typed, compiled programming language aimed at simplicity, efficiency, and concurrency. It has been gaining popularity and is already in use by tech giants and startups alike.
Although Go guarantees excellent performance and a really good developer experience, containerization with Docker allows isolation, portability, and efficient resource use. This allows us to make an application robust, highly scalable, and easily deployable.
In this chapter, we will help you Dockerize your Go application. We will explain how to set up a very simple Go project, compile, and run it inside a Docker container.
Prerequisites for Dockerizing Golang Application
First of all, make sure you have the following.
- Golang installed − Download and install Go from https://golang.org/dl/.
- Docker − Install Docker Desktop from https://www.docker.com/ based on the operating system.
- Basic knowledge of Golang − It is useful to know the syntax, structure, and build commands of Go.
- Basics of Docker − Understanding the different Docker concepts, such as images and containers, and Dockerfile is helpful.
Once you are equipped with these prerequisites; you are ready to build up your Go application and further containerize it.
Setting up a Golang Project
To start, let us create a new directory for our Go project. You can choose any name you prefer, but for this tutorial, we'll use go-docker-app.
$ mkdir go-docker-app $ cd go-docker-app

Go uses modules for dependency management. So, let us first initialize a new module for our project. In the below command, you can replace go-docker-app with your actual project name.
$ go mod init go-docker-app
The above command will create a go.mod file in your project directory. This file helps track dependencies and module versions.

Now, let's create a basic main.go file in the project's root directory. In this file, we will have the entry point for our application.
$ touch main.go
Insert the following code into main.go −
package main import ( "fmt" "net/http" ) func main() { http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { fmt.Fprintf(w, "Hello from Go!") }) http.ListenAndServe(":8080", nil) }
This code defines a simple HTTP server that listens on port 8080 and responds with "Hello from Go!" when accessed.
Running the Go Project Locally
Before Dockerizing our Golang project, let's test our Go application locally. You can run the below command −
$ go run main.go

This command will compile and run your Go application. Once running, you can visit the following URL in your browser to access the application: http://localhost:8080.
Creating the Dockerfile
Lets create a Dockerfile for our Golang application that contains all the commands required to build an image. For this, let's create a file called Dockerfile in the root of our project directory and copy the below code.
# Use the latest official Golang image as the base FROM golang:latest # Set the working directory inside the container WORKDIR /app # Copy the Go module file (go.mod) to the working directory COPY go.mod ./ # Download dependencies specified in go.mod RUN go mod download # Copy the main Go source file to the working directory COPY main.go ./ # Build the Go application and name the output file as go-docker-app RUN go build -o /go-docker-app # Specify the command to run when the container starts, executing the built binary CMD [ "/go-docker-app" ]
Explanation of the Dockerfile
- FROM golang:latest − Uses the latest official Golang image as the base.
- WORKDIR /app − Sets the working directory inside the container to /app.
- COPY go.mod ./ − Copies the go.mod file to the working directory.
- RUN go mod download − Downloads dependencies specified in go.mod.
- COPY main.go ./ − Copies the main.go file to the working directory.
- RUN go build -o /go-docker-app − Builds the Go application and names the output file go-docker-app.
- CMD [ "/go-docker-app" ] − Specifies the command to run when the container starts, executing the built binary.
In the next section, lets build the Docker image.
Building the Golang Docker Image
Now that we have our Dockerfile ready, we can go ahead and build our Docker image. Fire up your terminal and navigate to the project directory. Then, run the following command −
$ docker build -t my-go-app .
This command does the following −
- docker build − Instruct Docker to build an image.
- -t my-go-app − Tags the built image as my-go-app. You can use any desired tag name.
- . − Specifies the current directory as the build context, which includes the Dockerfile and your project files.
Docker will process all the instructions in the Dockerfile, layer by layer, to create the image.

Using the command below, you can verify the image build by listing all the Docker images in your host machine.
$ docker images

Running the Go Docker Container
Now that the image has been built, we can run a container associated with this image. You can use the following command −
$ docker run -p 8080:8080 my-go-app
This command does the following −
- docker run − Asks Docker to create and start a new container.
- -p 8080:8080 − Maps port 8080 of the container to port 8080 of your host machine. This allows you to access the application from your browser.
- my-go-app − Specifies the image to use for the container.
You can verify that the container is running by listing all the containers using the command below.
$ docker container ls

You can see that the container is running. You can now access your Go application at http://localhost:8080 in your web browser.

Conclusion
In this chapter, we have provided the basis necessary for Dockerizing a Go application. We learned how to effectively create a simple Go project, structure it appropriately, and write up a Dockerfile that builds a containerized image. Based on what is described herein, now you should be able to package and deploy your Go applications with complete ease once you have an overview of what plays a central role in the process.
You can further look into advanced Dockerfile techniques, image size optimization, multi-stage builds, and how to build your Dockerized application on container orchestration such as Kubernetes. Mastering these concepts will help you build Go applications that are robust, scalable, and above all, easy to maintain.
FAQs on Setting Golang in Docker
1. How do I create a Dockerfile for a Golang application?
A normal Dockerfile for a Golang application usually starts by inheriting an image containing the runtime environment of Golang. It then sets up the working directory, copies your Go source code into it, builds the executable, and finally sets the command to run your application. This image's size can generally be optimized using multi-stage builds.
2. How do I handle dependencies in a Dockerized Golang application?
For proper dependency management in a Dockerized Golang application, use go mod to manage dependencies within your project. The Dockerfile with a COPY instruction is used to copy your project directory, including the go.mod file. By doing this, the Docker build process will automatically resolve and download the dependencies for your project.
3. How can I improve the performance of a Dockerized Golang application?
The best performance for a Dockerized GoLang application can be attained with optimization at the base image level itself, using a compiled binary instead of reading and interpreting the Go code itself, and profiling your application. You may play with different Docker image layers or experiment with the caching of builds.