How to dockerize a simple Go app

14626

In this tutorial, I’m going to show you how to dockerize a simple app (written in Go) that shows the current time. Github repo is here, in case you’re interested.

Dockerfile

So, if you haven’t played with Docker before, official docs would be a good start.

For our Go app, we need to create a file named Dockerfile with the following content:

Go code

As for our app, the code is rather short and simple:

If you haven’t played with Go before, official docs would be a good place to start.

The only important thing to note here is that we’re listening on port 8080, which we need to set in the Dockerfile through EXPOSE.

Steps on how to make this yourself

Go code

  • Create the Go app (for testing purposes, you may use the same exact code)
  • Test it locally with go run curtime.go
  • Open your browser and go to http://localhost:8080
  • You should see the current time displayed

Docker

  • Create a Dockerfile with that content (Make sure you set the correct port in case you’ll be using a different one)
  • Inside the source folder (where you have curtime.go and Dockerfile) run docker build -t yourUserName/curtime . – this will create the image
  • You can check if the image was created with docker images
  • Run the image in a container: docker run -d -p 8000:8080 yourUserName/curtime
  • Access the app via your browser on port 8000. I used 8000 and 8080 intentionally so that it’s easier to explain that the 8000 port is the one on your computer and the 8080 is the port to which we’re ‘binding’ to

Few other useful Docker commands:

  • docker ps will list all the running containers. By adding the -a switch, you’ll see even the stopped ones
  • docker stop – stop the running container
  • docker rm containerID – delete the container with id containerID
  • docker images – list the images that are on your machine
  • docker rmi imageID – delete the image with id imageID

Written by Nikola Brežnjak