Dockerized Your Golang Application

Eky Pradhana
KANO Engineering
Published in
5 min readNov 6, 2018

When we finally jump into the modern software development world, especially in the microservices architecture, one thing we should understand is containerized environment. It makes us so easier to deploy our application anywhere without risking other applications that are running on the same server, with minimum effort of configuration. We can say, build once, run anywhere.

And Docker, is very common and powerful platform to create and manage containerized environment. It lets us to package up the entire applications and everything else that is needed by the application to run, including the libraries, dependencies, environment configuration, etc. In other words, we can think of containers as portable, packaged bits of functionality.

Source : Docker.com

This post will give us a ‘getting-started’ guide how to Dockerized a Golang application. I assume we have installed Docker in our machine. If not, it is better for us to follow their official documentation about how to install Docker here. Please choose your operating system on the left menu, and we’re good to go.

Prepare The Application

Before we start, I have prepared a simple Golang application serving on defined port (8080) and accessible through http request.

Let’s run it and access it using a browser :

Application is running
Accessed via browser

Dockerfile

To create a Docker container, we will use something called Docker Image. And to build a Docker Image, we need to provide a Dockerfile, so Docker knows what to do. In general, Dockerfile is placed on the root of the project directory.

Seems like a bunch of instruction?

  1. FROM : it will tell Docker to specify which base image from Docker Hub we want to build from. In this case, we use golang:latest. We can also choose some other versions we want. Why would we love to use this? because we don’t want to create golang environment from the scratch, like installing golang manually, defining the environment variable ($GOPATH, $GOROOT), creating the project folder structure, etc. By using this image, we will have the containerized environment with golang pre-installed there. So we can directly build our code, and then run it as a independent service. Don’t worry, it is official. And most importantly, it is free to use.
  2. MAINTAINER : when you want to tell the world that you are the one who responsible of this great and cool Docker image. It is a narcissistic command I think :)
  3. ADD : copy our whole project to specific destination of the image.
  4. WORKDIR : will tell the Docker to move the active working directory. So after we copy the file to /go/src/DockerizedGoApp, we enter that directory. It is like when we type cd /go/src/DockerizedGoApp in the linux administration terminal.
  5. RUN : will tell Docker to run the following command. In this case, we want to build our application to become an executable binary.
  6. EXPOSE : exposes a port which the container will listen on.
  7. CMD : sets the default command to execute when we start our container.

Finally, let’s build our image and tag it with name ‘dockerizedgoapp’.

docker build . -t dockerizedgoapp
Docker build process

Well done, we have successfully built the image. We can see the list of our images with command docker images.

Run It!

Sometimes you gotta run, before you can walk. -Tony Stark

Nice quote, but is there any correlation with this article? Man.. come on!

docker run -p 8080:8080 dockerizedgoapp

Wait, why on earth we need that -p flag? Since the docker is isolated environment, we can’t just access it outside from its network, even it is from the host machine. So we need to publish the specific port in the container and map it on the specific port in our host machine. Or we can call it as port-forwarding.

But, we have exposed the port 8080 in our Dockerfile, right? Yes, but still it is just accessible from its internal network. When we have more than 1 Docker containers running and it is connected each other on the same network, they will be able to communicate through their exposed port. Well I think we should have another post regarding this later, please remind me then :)

So let’s give it a shot!

Container is running

And access it via browser again :

Application is serving from the Docker container and accessed from host machine’s browser

And we’re done. We have successfully dockerized our golang apps. So what’s next? is it only this? Of course not. Since Docker has become one of must-have skill for the software developer in this microservices era, especially for the DevOps engineer, I highly recommend that we can master it well. So let’s keep exploring my friend :)

Credit : docker.com, google.com

Please find the github repo for this project here.

And don’t forget, sometimes you gotta run before you can walk!

Man please.. not again.. -_-

--

--

Eky Pradhana
KANO Engineering

Writing as an act of giving back and paying it forward. Let's sparking change, and fostering a culture of compassion!