DEV Community

Khalil Drissi
Khalil Drissi

Posted on

Containerization your Go application with Docker


Docker is a container technology for packaging and shipping applications. a container will run the same way regardless of the host OS. It provides a lot of the advantages of a virtual machine, but in a more lightweight container. It's possible to limit the resource consumption of individual containers and sandbox your environment.This recipe will set up a Docker container for a basic Go application, store some version information about the container, and demonstrate hitting a handler from a Docker endpoint.


You must have Docker and Dockercompose installed on your system. You can install Docker from:
*This will also include Docker Compose.

Let's begin

  1. create a new directory for your project and navigate to it.

  2. Run the following command:

$ go mod init myapp
Enter fullscreen mode Exit fullscreen mode

*You should see a file called go.mod appear in your folder.

  1. Create a file called dockerfile with the following content:
FROM alpine

ADD ./example/example /example
Enter fullscreen mode Exit fullscreen mode
  1. Create a file called with the following content:
#!/usr/bin/env bash
pushd example
env GOOS=linux go build -ldflags "-X main.version=1.0 -X
main.builddate=$(date +%s)"
docker build . -t example
docker run -d -p 8000:8000 example
Enter fullscreen mode Exit fullscreen mode
  1. Create a file called version.go with the following content:
package docker
import (
// VersionInfo holds artifacts passed in
// at build time
type VersionInfo struct {
Version string
BuildDate time.Time
Uptime time.Duration
// VersionHandler writes the latest version info
func VersionHandler(v *VersionInfo) http.HandlerFunc {
t := time.Now()
return func(w http.ResponseWriter, r *http.Request) {
v.Uptime = time.Since(t)
vers, err := json.Marshal(v)
if err != nil {

Enter fullscreen mode Exit fullscreen mode

6.Create a new directory named example and navigate to it.

  1. Create a main.go file with the following content:
package main
import (
// these are set at build time
var (
version string
builddate string
var versioninfo docker.VersionInfo
func init() {
// parse buildtime variables
versioninfo.Version = version
i, err := strconv.ParseInt(builddate, 10, 64)
if err != nil {
tm := time.Unix(i, 0)
versioninfo.BuildDate = tm
func main() {
fmt.Printf("version %s listening on :8000\n",
panic(http.ListenAndServe(":8000", nil))
Enter fullscreen mode Exit fullscreen mode
  1. Navigate back to the starting directory.
  2. Run the following command:
$ bash
Enter fullscreen mode Exit fullscreen mode
  1. Run the following commands:
$ docker ps
b390ef429fbd example "/bin/sh -c /example" 22 seconds ago Up 23 seconds>8000/tcp optimistic_wescoff

$ curl localhost:8000/version
$docker kill optimistic_wescoff # grab from first output

Enter fullscreen mode Exit fullscreen mode
  1. The go.mod file may be updated and the go.sum file should now be present in the top-level recipe directory.


we created a script that compiles the Go binary for the Linux architecture and sets private variables in main.go. These variables are used to return version information on a version endpoint. Once the binary is compiled, a Docker container is created that contains the binary. This allows us to use very small container images since the Go runtime is self-contained in the binary. We then run the container while exposing the port on which the container is listening for HTTP traffic. Lastly, we curl the port on localhost and see our version information returned.

Top comments (0)

Timeless DEV post...

Git Concepts I Wish I Knew Years Ago

The most used technology by developers is not Javascript.

It's not Python or HTML.

It hardly even gets mentioned in interviews or listed as a pre-requisite for jobs.

I'm talking about Git and version control of course.

One does not simply learn git