Docker

What is the Purpose of a docker-compose.yml File in Docker?

Docker compose is a utility/tool that is utilized to create and execute applications that consist of multiple containers. It uses YAML files for setting up application services. A docker-compose.yml file is a configuration file that defines how multiple Docker containers can work together as a single service. It can start all services with a single “docker-compose up” command as well as stop all services using a single “docker-compose down” command. Moreover, it can also be used to scale up selected services when required.

This article will explain:

What is the Purpose of a docker-compose.yml File in Docker?

The main purpose of a “docker-compose.yml” file is to simplify the process of deploying and managing multi-container Docker applications. It is also utilized for additional purposes, such as:

    • Starting and stopping multiple containers at once.
    • Specifying container dependencies.
    • Defining and managing network connections between containers.
    • Managing volumes and other shared resources between containers.
    • Setting environment variables and other configuration options for containers.
    • Scaling containers up or down as required.

How to Use a docker-compose.yml File in Docker?

To see how to create and use the docker-compose.yml file in Docker, follow the provided step-by-step instructions.

Step 1: Create Compose File

On Visual Studio Code, create a new file named “docker-compose.yml”. After that, configure the required services. For instance, we have configured the following services:

version: '3'

services:
web:
image: nginx:latest
ports:
- "9090:80"

web1:
build: .
ports:
- "8080:80"

 
In the above snippet:

    • The “version” key specifies the version of the Docker Compose file format to use. Here, we are using version 3.
    • The “services” key is used to configure the compose services. Here, we have configured two services i.e., “web” and “web1”.
    • The “web” service uses the “nginx:latest” image and maps port “9090” on the host machine to port “80” in the container.
    • The “web1” service builds an image from the Docker file in the “.” directory. and “8080:80” is the allocated port.

Step 2: Create Docker File

Now, create another file named “Dockerfile” and paste the below code into it:

FROM nginx:latest
ENTRYPOINT ["nginx", "-g", "daemon off;"]

 
In the above snippet:

    • The “FROM” statement is utilized to define a base image for the container i.e., “nginx:latest”.
    • The “ENTRYPOINT” is used to set the execution point for the container.

Step 3: Start Compose Services

Then, execute the  “docker-compose up” command along with the “-d” option to start the compose service in the detached mode:

docker-compose up -d

 

It can be seen that the compose service has started successfully.

Step 4: Verification

Now, navigate to the allocated ports of the local host and verify whether the services are executing in the containers or not:


On port “9090”, the “web” service is using the “nginx:latest” image and executing it.


On port “8080”, the “web1” service is using the Docker file to run the “nginx” image.

This is how we can configure numerous services and run multiple containers at once using the “docker-compose.yml” file.

Conclusion

The “docker-compose.yml” file permits developers to specify all the containers and their configuration including images, ports, volumes, networks, etc., in a single file. It makes it easier to create, start, and stop multiple containers as part of a single application. It is useful for developing, evaluating, and deploying applications that contain multiple components that are needed to communicate with each other. This article explained the purposes of the “docker-compose.yml” file and its usage in Docker.

About the author

Laiba Younas

I have done bachelors in Computer Science. Being passionate about learning new technologies, I am interested in exploring different programming languages and sharing my experience with the world.