Adding docker-compose To An Existing Project


Docker-compose is an amazing CLI tool for managing multi-container applications. This post gives an overview on how an API project incorporates docker-compose (including makefile commands for ease of use).

The App

The app is an implementation of the freeCodeCamp’s timeserver API project. I used Golang with the Gin web framework to create the REST API. You can view the code for the project here.

Setting Up Docker-compose

The application already has a Dockerfile so all that is needed is a docker-compose yaml file. The default filename for the file is docker-compose.yml so we can create it in the root of the timeserver repository.


version: '3.8'
  context: ./
  dockerfile: ./Dockerfile
    - '8080:80'
    - PORT=80

The version field is specifying the docker-compose file format version to use when parsing the yaml file. 3.8 is the latest file format version at the time of this writing.

The services' section is what is used to specify all of the containers needed for the application. For this example the API is just one container which is defined by the Dockerfile in the repository whose path is defined in the yaml file by the dockerfile field. The ports section specifies what container ports to expose with the option of exposing host ports so the host could interact with the services. I set the container port to 80 because that is what the PORT environmental variable is set to which Gin will read to know which port to use. The PORT environmental variable is set in the environment section of the docker-compose.yml file shown above.

Adding docker-compose Commands to Makefile

A demonstration of the real power from docker-compose is when you can compose the complicated commands into simpler Makefile alias commands. The commands I created for timeserver’s Makefile are below:

    docker-compose -f ./docker-compose.yml up -d

    docker-compose -f ./docker-compose.yml down

dev-clean: dev-down
    docker-compose -f ./docker-compose.yml rm -f

dev-up will bring the application up in a docker container with the application live listening to the host machine’s 8080 port.

dev-down will stop the application’s container(s).

dev-clean will stop the applications container(s) and will delete the stopped containers.