Dockerize your Go Web Application

Brian Dowe
3 min readJan 19, 2021

In this article, we’re going to run a simple Go Web application using docker. I’ll assume that you have some familiarity with docker and that you have docker installed and running. If not, visit this link to get set up. I’ll also assume that you have Go installed on your local machine. If you don’t you can visit this link to get Go downloaded and installed.

Part 1: Setting up the Go Web App

First, let’s build a simple Go web app and get it running locally without using docker. Make a new directory for this project and navigate to it in your terminal, then create a new file called main.go and populate it with the following code:

Next, run go run main.go in the command prompt and navigate to http://localhost:8080 in the browser. You should see that the app is up and running:

Part 2: Dockerizing the Go web Application

Now that we have our hello world app up and running, let’s run it in a docker container. First, we’ll need a Dockerfile. This is essentially a blueprint of instructions for setting up the environment in which our Go web application will run. We’ll use this Dockerfile to build a docker image, which we can run as a docker container.

Create a new file called Dockerfile and populate it with the following code:

Let’s go through this file line by line:

  • Line 1 uses the official golang docker image as the base image for our dockerfile. This essentially takes care of making sure that we have the latest stable version of Go installed in our container (1.15.6 at the time of writing this article)
  • Line 2 sets /app as the working directory inside our container. All commands run inside the container after this line will be run from this directory.
  • Line 3 copies the contents of our working directory on our host machine to the /app directory in the container
  • Line 4 exposes port 8080 in our docker container, so that it can accept traffic to our app on that port.
  • Line 5 executes our code in main.go to start up our webserver. This command will execute immediately after our container is built and run.

Now that we have the Dockerfile set up, we can build our image and run our docker container. To build the image, run the following command:

docker build -t simple-go-app .

This will build the image and tag it with the name “simple-go-app” using the instructions in the Dockerfile, which it will search for in the current directory (to access a Dockerfile in another directory, specify the path to that file instead of . in the command above).

Once the image is built, we can run it as a container using the docker run command:

docker run simple-go-app

However, if we try to visit our app at http://localhost:8080, we’ll find that it is inaccessible. This is because even though the app is listening at port 8080, this is referring to the port inside the container, not on our host machine. To fix this, we need to set up port-forwarding so that our local machine knows to forward traffic on port 8080 to the same port in docker. We can do this easily by modifying the docker-run command as follows

docker run -p 8080:8080 simple-go-app

Now if you visit the app in the browser, you should see that it is working successfully:

Congratulations! You just dockerized a go web application. There’s much more to learn about Docker, but this is already a great start that you can use to apply as a starting point for your own applications. Please feel free to leave any questions in the comments below. Thanks for reading!

--

--

Brian Dowe

Professional Full Stack Web Developer and Backend/DevOps Engineer