Joseph Jude

Docker based development environment for Golang applications

2018.06.06 / code / devops / golang /

Write your golang programs in any directory. Take your golang development environment with you to anywhere.

Go is a lean language. The 3rd party dependencies aren’t that big like in the node.js world. Still having a containerized development environment helps in developing Golang applications.

Golang in Docker

Containerized environment gives you and the team capability to create repeatable, sharable, and deterministic development environment.

This is a huge advantage when team members are working on multiple environments (node.js, python, and go), as it happens in any enterprise projects.

When you work with docker containers, you also get the possibility of creating an environment as close to production as possible. That brings ease and confidence — for us, for our teams, and for our clients. In software development, ease and confidence is value.

In this post, let us build a docker container for developing cli based golang applications. We can build docker containers for developing web applications based on golang based on this minimal container.

Install Docker

The first step is to install Docker. Follow the guides from Docker documentation:

Writing Dockerfile

Docker uses Dockerfile to build docker images. Create a file named Dockerfile. Start with these contents:

FROM golang:1.10.2-alpine3.7
RUN apk update; apk upgrade
RUN apk add git

What does these lines mean?

Line 1: We are basing our container on alpine 3.7 (which is a secure OS with small footprint). We are also installing the latest Golang version (which is 1.10.2 as of this writing)
Line 2: Update the OS
Line 3: Add git version control system

Let us keep going.

We want to store the application code itself in the host computer (that is from where we are running the docker). We want the docker container to contain only the golang compiler and other development tools.

We will install only the dependency management tool dep within the container. Add another line to the Dockerfile.

RUN go get -u github.com/golang/dep/cmd/dep

This will install the dep tool. If you want you can install other golang tools like this.

Docker uses a concept called Volume to share code between the container and the host. Let us add this. So we are going to indicate a working directory and share that working directory with the host.

WORKDIR /go/src/app
VOLUME ["/go/src/app"]

The whole Dockerfile looks like this:

FROM golang:1.10.2-alpine3.7
RUN apk update; apk upgrade
RUN apk add git
RUN go get -u github.com/golang/dep/cmd/dep
WORKDIR /go/src/app
VOLUME ["/go/src/app"]

You can get all of my dockerfiles from my gitlab repository.

Building Docker Image

Once you have this docker file, you can build the docker image. Issue the command: docker build -t jjude/go . from the terminal. You can change jjude/go to your own name or just go. Note that you have to issue this command only from the directory that contains the Dockerfile.

This will download all the required files and build an image. You should start to see something like this:

Sending build context to Docker daemon  3.584kB
Step 1/9 : FROM golang:1.10.2-alpine3.7
1.10.2-alpine3.7: Pulling from library/golang

If every thing worked well, it should end like this:

Successfully built 48e6e95b489c
Successfully tagged jjude/go:latest

You can check by issuing docker images. You should see the newly built image.

REPOSITORY          TAG                 IMAGE ID            CREATED              SIZE
jjude/go            latest              48e6e95b489c        About a minute ago   438MB

This has built the image. Now you have to create a container. You can create as many containers as you want from this image.

Using Docker Container

Go to the folder where you want to store the go code. This can be different from where you stored the Dockerfile. Issue this command from that directory (Ex: ~/Code/go):

docker run -v "$(pwd)":/go/src/app -it jjude/go /bin/sh

This will create a golang container with dep installed. It will display a shell like this:

/go/src/app #

If you issue a dep init, it will create 3 files:

├── Gopkg.lock
├── Gopkg.toml
└── vendor

1 directory, 2 files

You can view them both inside the docker container as well as from the host directory (~/Code/go).

Now you can install any dependencies using dep and start to create golang applications.

Further Reading:


Share this post on

Twitter | | |
Sign up for my weekly newsletter

I will send blog updates to this email. You can unsubscribe at any time using the link in those emails.