Running Go binary into Docker

We will see how to run api_get.go into a Docker, this go file make a get into an API, that API provides a JSON with a cat information, this get will execute in loop every 4 seconds and after will write into a log. We will use docker-volume to view the log in our machine.

The binary will execute when the container is up, and will finish the execution only when the container stops.

  1. We need to compile the .go main file, for compile we need to have installed go in our computer.

go build api_get.go

  1. ¬†We will create a Dockerfile, in this case we will use a docker image Ubuntu, because if we use go docker image, we can’t do http.get because this image don’t have the library system for to do this. And the other reason is that we compile the go binary before.

FROM ubuntu


RUN mkdir ./logs/

RUN chmod 777 ./logs/


RUN apt-get update

RUN apt-get install ca-certificates -y

RUN update-ca-certificates


COPY api_get /go/bin/


WORKDIR /go/bin/


CMD ["/go/bin/api_get"]

  1. When we have the Dockerfile, we will create the docker-compose.yaml, this file will contain the configuration of directory logs and the name of the image. The directory log, will create in your current directory when you start the Docker image.

version: '3.3'



 container_name: docker-api

 build: .

 image: docker-api


 - ./logs_api/:/go/bin/logs/

  1. In this case, we will mount a Docker Volume, configured with docker-compose, because we want to see the log that API generates.

First we need to configure the Docker Compose, to use docker compose we need to install the docker Engine, when have installed the docker Engine then we can install the docker Compose

  1. When we have all installed and configured we will build the image with command docker-compose, the parameter -f is for specifying the compose file.

docker-compose -f docker-compose.yml build

With this command, we create a docker image. And before we need to start the image. With the parameter -d we put the container in background.

docker-compose up -d

For know if container api-docker is running.

docker ps

This binary will execute when the container starts, when this happens this will do infinite requests every 4 seconds, and this will stop when the container stops.


Website explain how to build docker container with go application
Docker compose documentation

Docker volumes documentation


View Github