Docker Compose: Basics and Usage

Docker Compose is a powerful and popular tool used for managing and deploying applications based on Docker. It allows you to define, configure, and run multiple Docker containers as a single project, simplifying application deployment and ensuring consistency between development and production environments.

Below are some concepts and examples of Docker Compose:

Define the project using the docker-compose.yml file

In the docker-compose.yml file, you can define the services required for your application. For example, to deploy a PHP web application with a MySQL database, you can define two services as follows:

version: "3"
    image: php:7.4-apache
      - "80:80"
      - ./app:/var/www/html

    image: mysql:5.7
      MYSQL_ROOT_PASSWORD: password
      MYSQL_DATABASE: my_database

In the above code snippet, we define two services: web and db. The web service will use the PHP 7.4 image with Apache, listen on port 80, and mount the ./app directory from the host into the /var/www/html directory in the container. The db service will use the MySQL 5.7 image and set some environment variables required for the database.


Using Docker Compose command

Once you have defined the project in the docker-compose.yml file, you can use Docker Compose commands to manage the services.

  • Start the project: docker-compose up

    This command will start the containers for the services defined in the docker-compose.yml file.

  • Stop and remove the containers: docker-compose down

    This command stops and removes all containers related to the project.

  • List running containers: docker-compose ps

    This command will display the status of the containers in the project.

  • View service logs: docker-compose logs

    This command shows the logs of the services in the project.


Environment variables and customization

Docker Compose allows you to use environment variables to customize configurations for different environments, such as development and production. You can use environment variables in the docker-compose.yml file and define their values in corresponding .env files.

For example, if you want to define an environment variable for the port of the web service, you can add a line to the .env file like this:


Then, in the docker-compose.yml file, you can use this environment variable like this:

version: "3"
    image: php:7.4-apache
      - "${WEB_PORT}:80"
      - ./app:/var/www/html

When running the docker-compose up command, the web service will listen on port 8080 instead of port 80.


Integrating with Docker Swam

If you want to deploy your application on a distributed environment with multiple nodes, Docker Compose can integrate with Docker Swarm. This allows you to manage services across multiple nodes in a Docker cluster.

To use this integration, you just need to add the --orchestrate or --with-registry-auth options when running docker stack deploy or docker-compose up commands in a Swarm environment.


Docker Compose is a useful tool for easy and efficient application development, testing, and deployment. It minimizes the differences between development and production environments, ensures consistency in the software development process, and enhances the productivity of development teams.