Deploying Go Web Apps With Docker

In my previous post, “The Evolution Of Cloud PaaS To Container Ecosystem”, we have discussed the evolution of Cloud PaaS to container technologies such as Docker and Kubernetes. In this post, I will demonstrate how to deploy a Go web app into an application container using Docker. You can download the sample app from https://github.com/shijuvar/golang-docker

What is Docker?

Docker is a revolutionary application container platform for developing, shipping, and running apps on the container virtualization environment. A container is lightweight Linux environment which can be used as an abstraction layer for your apps. When you develop apps for virtual machines, you are actually interacting with an infrastructure layer including operating systems, network infrastructure layers. An application container separate your apps from your infrastructure and it separate your app from where it runs. Application container lets you develop apps against idealized operating system — a static Linux environment, instead of developing apps against idealized hardware environment in traditional virtual machines. This capability provides great opportunities to application developer and IT administrators for developing, managing and shipping distributed apps. IMHO, application container technology is the big revolution after Cloud computing.

Docker revolutionized the application container technology with their powerful ecosystem. The Docker ecosystem consisting of Docker Engine, a portable, lightweight run-time and packaging tool for building containerized apps, and Docker Hub, a cloud service for sharing apps and automating workflows. Docker Hub is a registry system in the Docker ecosystem for storing Docker images, from where you upload or download images. You will be created containers from an image. Docker lets the developers containerize their apps which can run on anywhere. Docker allows you compose your containerized apps by linking between different containers. Docker is a great platform for building next-generation distributed apps with a Microservices architecture where you can put your Microservice apps into individual containers and compose these apps by linking between containers.

Docker client tool works with all operating systems. If you want to run Docker clients in Mac and Windows, you need to run boot2docker, which is a lightweight Linux distribution for Docker.

Deploying Go Web App with Docker

Write HTTP Server in Go

Let’s a write a simple HTTP server in Go for deploying into a container using Docker. The source repository is available at https://github.com/shijuvar/golang-docker

Code Listing — main.go

Write Dockerfile for Docker Build

When you run the command “docker build” in the Docker client’s terminal, Docker can build images by reading the instructions from a Dockerfile. Dockerfile is a text file that contains all the commands to build a Docker image. Let’s write a Dockerfile for building our Docker image for install and run our Go HTTP server in a Docker container.

Code Listing — Dockerfile

In the above Dockerfile, we are building our image from a base image Golang which is created for running Go apps where Gopath configured at /go. The image name golang:latest means that we are building our image from Golang image with latest tag. Using the ADD command, we are adding the Go package source into our container’s Gopath. Then, we install our Go app to bin folder of Gopath and finally setting up the entry point for the app. The EXPOSE command exposing port 8080 for for the HTTP server.

Build Docker Image with Dockerfile

In my local machine, I am working with boot2docker client. The source repository is located on github.com\shijuvar\golang-docker. Let me mount the source repository directory with the Dcoker container.

sudo mount -t vboxsf golang-docker /home/docker/golang-docker

Before executing the mount command, I had created a shared folder named golang-docker to map local directory github.com\shijuvar\golang-docker in the VM VirtualBox. In the mount command, we mount the shared folder golang-docker with /home/Docker/golang-docker directory.

Let’s run the “docker build” command from golang-docker directory to build our Docker image by reading instructions from Dockerfile.

sudo docker build -t golang-docker .

The above build command reads instructions from the Docker file. The command “FROM golang:latest” in the Dockerfile, will fetch the golang:latest image from Dokcer Hub. The “ADD” command add the package source into Gopath of golang image. The “RUN” command build the Go package and the “ENTRYPOINT” command set up entry point for our container for running the golang-docker command by default, when the container starts. The “EXPOSE” command expose port 8080 for our HTTP server. We tag the resulting image as golang-docker.

Run the Docker Image

The docker build command did build the docker image from golang base image and finally build the Go package and exposed port 8080 from the container. Let’s run the Docker image by publishing an external port to container’s port 8080.

$ docker run —publish 3000:8080 —name goweb —rm golang-docker

In the above run command, we run the golang-docker repository by publishing external port 3000 to container’s port 8080, and gives container’s name as goweb. Even we can use port 8080 to multiple containers and map these ports with different ports of external system. The —rm flag specify to remove the container image when our HTTP server exits.

Now you can access your web app from a web browser. If you are using Linux system you can access the web app from http://localhost:3000/. I am using boot2docker Docker client for Windows where the exposed IP address for boot2docker is 192.168.59.103. Let me navigate to http://192.168.59.103:3000/ for accessing the Go web app hosted on a Docker container.

Summary

In this post, we developed a simple HTTP server in Go and deployed the web app into a application container using Docker. We fetch a golang image from Docker Hub for creating our Docker image. Docker Hub is a important component in Docker ecosystem where we can leverage thousands of preconfigured images for building our containers. Using Docker, we can separate apps from infrastructure and develop apps against an idealized operating system — a static Linux environment. Docker is a great platform for building distributed apps with Microservices architecture.

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.