Containerize A Go Module

Golang is a high level programming language that can be used to create web applications, APIs, and simple function programs.

It is nice to build and use Go modules in the command line, but containerizing them is much more useful and scalable. What does containerizing a module consist of? Well, once the code is built or compiled it can be moved to any OS, which is a fantastic feature of Go. With a container, the application can be moved into the cloud or other remotely accessed infrastructure.

Organize The Code

The first step in the containerizing process is to organize the Go code based on best practices. Additionally, testing the code at this point is advised to ensure it will function as expected.

Create A Docker Image

After testing is done and the code is organized, create a file in the root of the project directory and name it “Dockerfile”. This file will be used to create a Docker Image.

This example uses the Alpine image for simplicity.

<mark class="has-inline-color" style="background-color:#FFD1D1">FROM golang:alpine</mark>

Create the working directory for the project inside of the image that is to be built.

<mark class="has-inline-color" style="background-color:#FFD1D1">WORKDIR /app</mark>

Copy the go.mod file into the image and download all the required dependencies to run the module.

<mark class="has-inline-color" style="background-color:#FFD1D1">COPY go.mod ./</mark>

RUN go mod download

Next copy all of the other Go files associated with the project to the image, and build the module. Be sure to give the compiled file of the module a similar name.

COPY *.go ./

RUN go build -o /<COMPILED_FILE>

The compiled file can be moved anywhere inside of the image, but for this example it will stay in the WORKDIR.

Lastly, if the container should immediately call on the application add the command.

CMD [“/COMPILED_FILE”]

The full Dockerfile should look as follows:

“`

WORKDIR /app

COPY go.mod ./

RUN go mod download

COPY *.go ./

RUN go build -o /COMPILED_FILE

CMD ["/COMPILED_FILE"]

To build the image itself simply run <mark class="has-inline-color" style="background-color:#FFD1D1">docker build -t <IMAGE_NAME>:<IMAGE_TAG></mark>. The image should be listed under <mark class="has-inline-color" style="background-color:#FFD1D1">docker images</mark>.

Run Image in a Container

To verify that the image will run as expected, it is time to run it inside of a container.

The key here is to run the container on the correct network. Docker by default runs containers on a separate network than the host network (localhost), if the tool relies on data located on the host, simply tell docker to run on the host network using <mark class="has-inline-color" style="background-color:#FFD1D1">–net=host</mark>.

docker run --net=host <IMAGE_NAME>:<IMAGE_TAG> [additional flags and arguments]

To ensure that the container does not remain after running, add the flag <mark class="has-inline-color" style="background-color:#FFD1D1">–rm</mark> so that docker knows to delete the stopped container. See the list of all containers using <mark class="has-inline-color" style="background-color:#FFD1D1">docker container ls -a</mark>.

docker run  --net=host --rm <IMAGE_NAME>:<IMAGE_TAG> [additional flags and arguments]

The module is containerized!

~

Optional: Share the Image

  • Share the Dockerfile on GitHub so that others can build similar images.
  • Publish the image on Docker Hub by using GitHub workflows.
  • Share the project, including the Dockerfile, on GitHub.