Docker Compose

Docker Compose

Imagine you have multiple containers and you are individually running each of those containers, here comes the docker compose where you configure a .yaml file where you can configure all your containers, image, volume and networks all at one place.

From Docs:

Compose is a tool for defining and running multi-container Docker applications. With Compose, you use a YAML file to configure your application’s services. Then, with a single command, you create and start all the services from your configuration.

Compose works in all environments: production, staging, development, testing, as well as CI workflows. It also has commands for managing the whole lifecycle of your application:

  • Start, stop, and rebuild services

  • View the status of running services

  • Stream the log output of running services

  • Run a one-off command on a service

Configuring Compose

version: "3.1"

services:
  wordpress-website:
    image: wordpress
    restart: always
    ports:
      - 3000:80
    environment:
      WORDPRESS_DB_HOST: wordpress-database
      WORDPRESS_DB_USER: piyushgarg
      WORDPRESS_DB_PASSWORD: piyushgarg
      WORDPRESS_DB_NAME: mydb
    volumes:
      - custom-wordpress-data:/var/www/html
    networks:
      - milkyway
    depends_on:
      - wordpress-database

  wordpress-database:
    image: mysql:5.7
    platform: linux/amd64
    environment:
      MYSQL_DATABASE: mydb
      MYSQL_USER: piyushgarg
      MYSQL_PASSWORD: piyushgarg
      MYSQL_RANDOM_ROOT_PASSWORD: "1"
    volumes:
      - custom-db:/var/lib/mysql
    networks:
      - milkyway

volumes:
  custom-wordpress-data:
  custom-db:

networks:
  milkyway:

Explanation of the tags:

  • version: "3.1": Specifies the version of the Docker Compose file format being used.

  • services:: Begins the section where you define the services or containers.

  • wordpress-website:: The name of the service/container.

    • image: wordpress: Specifies the Docker image to use for the WordPress website service.

    • restart: always: Instructs Docker to automatically restart the container if it stops or crashes.

    • ports:: Specifies the port mapping between the host and the container.

    • environment:: Sets environment variables for the WordPress container.

    • volumes:: Mounts a volume to store custom WordPress data.

    • networks:: Assigns the service to a specific network.

    • depends_on:: Specifies the dependency on the wordpress-database service. Thus wordpress-database starts before wordpress-website so errors dosen't occur since it depends upon it.

  • wordpress-database:: The name of the MySQL database service.

    • image: mysql:5.7: Specifies the Docker image to use for the MySQL service.

    • platform: linux/amd64: Specifies the platform architecture for the MySQL service.

    • environment:: Sets environment variables for the MySQL container.

    • volumes:: Mounts a volume to store the MySQL database data.

    • networks:: Assigns the service to a specific network.

  • volumes:: Defines the custom volumes for storing WordPress data and MySQL database data.

  • networks:: Defines the custom network for the services to communicate.

To build the compose file

From your project directory, type docker compose up to build the app with the updated Compose file, and run it.

After this all the requirements are fetched if not present already.

As you can see, the dockerprac folder is created where our .yml was present under Containers section in docker application.

Now run docker compose down to actually to bring everyting actually down.

Some other docker commands

  • If you want to run your services in the background, you can pass the -d flag (for “detached” mode) to docker compose up and use docker compose ps to see what is currently running:

  • The docker compose run command allows you to run one-off commands for your services. For example, to see what environment variables are available to the web service:

  • See docker compose --help to see other available commands.

  • If you started Compose with docker compose up -d, stop your services once you’ve finished with them:

  • You can bring everything down, removing the containers entirely, with the down command. Pass --volumes to also remove the data volume used by the Redis container

Thanks a lot for reading the article.
Hope you found it helpful.

My Linkedin: https://www.linkedin.com/in/tautikk/
My Email:

Did you find this article valuable?

Support Tautik Agrahari by becoming a sponsor. Any amount is appreciated!