Best practices for Golang application containers with Docker

Go’s small application binary sizes are a perfect fit for the microservices deployment strategies that Docker and Kubernetes excel at delivering. Golang applications and containers are made for each other. This synergy is not without its challenges, though. It’s important to understand container best practices and key concepts to avoid security pitfalls that can easily creep into your container images.

In this article, the author and you will code up a sample Golang application and learn how best to containerize it and run it securely.

Prerequisites (Golang and Docker)

To follow along with this tutorial, you’ll need to have Docker and Golang installed on your machine and a basic familiarity with both. Good tutorials for Go can be found in the official Golang documentation, or by skipping to the Golang installation page for Linux. You can find introductory instructions and downloads for Docker on the orientation and setup page of the Docker documentation

Create a sample Golang application

To start, you have to create our Go API. First, navigate to the directory where you want your Golang application to reside on your machine. Then, create a directory named “godocker.” In this godocker/ directory, run the following command to define your Go module:

go mod init godocker

Next, create a “main.go” file inside the “godocker” directory. This file will hold your API code. Now, you can add logic to provide and handle the current time via the API.