Setting up Docker for Go

/ / setting up docker for go

Updated: 2017-06-05

I will be using Ubuntu 16.04 but the instructions should work fine with most operating systems.

First, we need to register an account on Docker Hub. Once the registration is complete, click Create Reposity, choose a suitable name, set desired visibility (you get 1 free private repo) and click Create.

Pull the official golang image:

$ docker pull golang

I like to work using the following structure:

/home/USERNAME/docker/PROJECTNAME/github.com/GITHUBUSER/GITHUBPROJECT

For which I can create a new Docker container and attach it to the above path by running the following command:

docker run --publish 6060:8080 -ti -v /home/USERNAME/docker/PROJECTNAME:/go/src golang

This will allow you to execute code written within PROJECTNAME inside your Docker container. There is no reason why you can’t do the same using your local Go src path, but I like to separate my Docker projects.

You can view containers with the commands:

$ docker ps    // view running containers
$ docker ps -a // view all containers

From there you will see the CONTAINER ID of our golang image. You can then commit it with the command:

$ docker commit CONTAINERID DOCKERUSERNAME/DOCKERREPONAME

Before you can push it you should login with your DockerHub credentials using

$ docker login

And you can then push the project to Docker Hub with the command:

$ docker push DOCKERUSERNAME/DOCKERREPONAME

This setup allows you to easily backup your code with GitHub, and whenever you make any changes to the container (e.g. install a package) then you can commit and push the container to Docker Hub.

If you would like to run Docker in production then I recommend checking out my article about using Docker on Google Compute Engine with Ubuntu image.