Dockerize your golang application.

Playing with docker for last couple of projects. So far it’s good experience. In this post we will develop a REST API using Golang and then Dockerize the deployment. The REST API will be very basic with just a single endpoint. So we will cover the following section in the post:

  1. Developing minimal REST endpoint
  2. Coding a Docker file
  3. Running the Docker container with the API
  4. Finally will push the Docker image to a docker registry

docker_loves_golang

Developing REST endpoint

For developing the REST API we are going to use Gin which is a HTTP web framework. It provides standard solutions/API for solving common problems using features like middleware support, Routing and standard convention of error management. But, sometime it make sense only using Go’s net/http can be used to develop any http based application but to avoid developing everything from scratch, Gin will give us a good starting point.

The REST API returns current time when in the route “/”. The code look like below:

In the main function we are declaring the Router for the API and passing the handler function associated with the endpoint. And in the handler method we are getting current time and passing it to Gin context.

Creating the Dockerfile

For Dockerizing the REST API lets develop the Dockerfile. We will use Golang official docker image because its used by many other developers and again we don’t need to do all the work of choosing an OS, pulling Golang setting up environment further steps.

In the Dockerfile we are using bare bone docker image scratch which is minimal base image.

In line 5, we are cloning the binary of the api.

In line 7 running the latest build and finally exposing the port of the docker container via EXPOSE 8080.

So far we developed the API with the endpoint and also coded the Docker file. Lets build the docker image and run the Docker container.

Running the docker container

In the local development machine we are using Docker-machine. So we need to follow following steps:

docker-machine start default
docker build -t mush/gondar .

docker build builds docker image (name of our docker image is gondar) for the container and then we need to run the image as a container with the following command:

docker images #to list current docker images
docker run -d -p 80:8080 mush/gondar

In line 2 we are running the newly build docker image and forwarding port from docker to host via -p. And we are good to go, our docker container is running and we can check it via a curl command like below:

api=$(docker-machine ip default) #getting IP of the docker machine host
curl $api:80

which returns current time as JSON response.

Pushing newly build Docker image

So the last step would be pushing the latest created Docker image to a registry, we will use docker hub.

First, we need to login to docker hub via following command:

docker login --username=yourhubusername --email=youremail@company.com

And then we need to push newly created Docker image via following command:

docker push mush/gondar 

That’s it, we just created a REST API and also developed Docker image for deployment it as a container based service🙂

Further reading:

Project to watch:

5 comments

  1. tbroyer · February 11

    build (possibly within docker) then package only the binary from a “scratch” image.

    • mushfiq · April 3

      Hey tbroyer, thanks for the feedback, I updated the docker file as you suggested🙂

  2. Homme · February 18

    A nice walk through, thanks. However, I’d recommend using the exec form of `ENTRYPOINT` (i.e. `ENTRYPOINT [“./main”]`) rather than the shell form you’ve shown to ensure that signals get propagated from the docker container to the go program.

    • mushfiq · April 3

      Hey Homme good point mentioned, I updated the docker file. Thx!

  3. Pingback: Cheatsheet: 2016 02.01 ~ 02.29 - IT大道

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s