Defining Your Multi-Container Application With docker-compose.yml

Docker is a well-liked and widely used solution to build and deploy projects. It provides essential components like containers, images, registry, and Docker compose. More specifically, the Docker compose is a key element of Docker that is mostly used to build and configure multi-container applications or that consist of multiple services. It also utilizes the Yaml file to configure the multi-container app.

This write-up will demonstrate how to define multi-container applications with a “docker-compose.yml” file.

How to Define Multiple Container Applications With “docker-compose.yml” File?

Multi-container applications consist of multiple services, and each service is required to execute within a separate container. Moreover, defining multi-container applications involve three basic steps:

Step 1: Create Dockerfile

First, create a Dockerfile that is also referred to as the instruction file that contains instructions to generate the image to containerize the application. Paste the following code into “Dockerfile” to execute the Golang application on the web server:

FROM golang

WORKDIR /go/src/app

ADD main.go .

RUN go build -o webserver .


CMD ["./webserver"]

In the above code:

  • FROM” command defines the base image for a container.
  • WORKDIR” command defines the working directory for the container.
  • ADD” command adds the specified file to the container path.
  • RUN” command executes the specified command.
  • EXPOSE” is utilized to allocate the exposing port to the container.
  • CMD” specifies the entry point or defaults for the container. Here, “CMD” defines the “./webserver” executable point:

Note: The name of the file must be “Dockerfile”, and do not attach any extension with the file name.

Step 2: Configure Services in “docker-compose.yml” File

In the next step, create a “docker-compose.yml” file to configure the multi-container application services. For instance, we have specified the following code:

: .
: web-container
: true
     - "8080:8080/tcp"

According to the above-provided snippet:

  • services” key defines two services: “web” and “golang”.
  • The “web” service uses a build of Dockerfile or Dockerfile as the base.
  • container_name” specifies the container’s name in which the “web” service will execute.
  • privileged” is used to allocate the host privileges to the container.
  • port” defines the exposing port for a container.
  • The “golang” service simply uses the “golang:alpine” image as a base image:

Step 3: Start the Containers

Now, start the services specified in the “docker-compose.yml” file by utilizing the “docker-compose up” command:

> docker-compose up -d

From the below output, you can see that two services are started into different containers. The first container runs the “web” service of the Golang program. The second container executes the “golang” service:

For the verification, navigate to the specified port of the local host:

It can be observed that we have successfully defined the multi-container app with the “docker-compose.yml” file.


To configure the multiple containers application, first create a simple Dockerfile file referred to as an instruction file. Then, configure the application services into a “docker-compose.yml” file. Each individual service will execute in a separate container. After that, fire up the containers with the help of the “docker-compose up” command. This write-up has demonstrated how to configure the multi-container application with the “docker-compose.yml” file.

About the author

Rafia Zafar

I am graduated in computer science. I am a junior technical author here and passionate about Programming and learning new technologies. I have worked in JAVA, HTML 5, CSS3, Bootstrap, and PHP.