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:
- Developing minimal REST endpoint
- Coding a Docker file
- Running the Docker container with the API
- Finally will push the Docker image to a docker registry
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@example.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 🙂
Project to watch: