What/Why is Docker Compose

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.

Let's understand with a simple example, When you are working in the real-time industry there may be a requirement where you need to design an application that has web-tier and data-base tier and if you want to design such application using docker, you need to create two or more container as per the requirement.

So, let's dockerize a sample word press application which has a web tier and database tier. In word press application we define instructions inside a YAML file and those steps in the YAML file give information to the web tier about the database.

Note: In web-based applications, the first DataBase server needs to come up before the webserver.

Steps to dockerize word press application:

  • First, we need to make the database container UP and the command to make the data-base container up is:

      gaurav@learning-ocean:~$ docker container run --name some-mysql -e MYSQL_ROOT_PASSWORD=mypassword -d mysql:5.7
    
  • Get the IP address of data-base container using command below command from field "IP Address"

      gaurav@learning-ocean:~$ docker container inspect <first 4-digit container id>
    
  • Now, we need to run a web-tier container using the below command, suppose 172.17.0.2 is the database container's IP address

      gaurav@learning-ocean:~$ docker container run --name wordpress -e WORDPRESS_DB_HOST=172.17.0.2:3306 -e WORDPRESS_DB_USER=root -e WORDPRESS_DB_PASSWORD=mypassword -d wordpress
    
  • get the IP of the web-tier container as explained in Step 2.

  • After getting the web-tier IP address, you can access the WordPress application by giving the IP address in the URL.

The above steps of sequence define the manual procedure to make web-tier applications up. Now let's understand how docker-compose will make it easier and efficient.

Let's create a docker-compose.yaml file as shown in below snippet:

version: "3.9"
services:
  db:
    image: mysql:5.7
    volumes:
      - db_data:/var/lib/mysql
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: somewordpress
      MYSQL_DATABASE: wordpress
      MYSQL_USER: wordpress
      MYSQL_PASSWORD: wordpress

  wordpress:
    depends_on:
      - db
    image: wordpress:latest
    volumes:
      - wordpress_data:/var/www/html
    ports:
      - "8000:80"
    restart: always
    environment:
      WORDPRESS_DB_HOST: db:3306
      WORDPRESS_DB_USER: wordpress
      WORDPRESS_DB_PASSWORD: wordpress
      WORDPRESS_DB_NAME: wordpress
volumes:
  db_data: {}
  wordpress_data: {}

The first 12 lines explain the database and later instruction is for web-tier. Now go to the folder where the docker-compose.yaml file is present and run the command

gaurav@learning-ocean:~$ docker-compose up -d

when you hit this command it actually searches for docker-compose.yaml file in the running directory and it executes all the instructions given in YAML file one by one, it will create container application, network, and volume, based on given instruction in YAML file. Run docker container ls command to check whether the container came up or not, you should see two containers web-tier and database, to check network run docker network ls, it will show default network which will get create for word press application, to check volume run docker volume ls, it will show volume related to WordPress application. To delete the application which was created by the docker-compose up command run docker-compose down and it will delete all the created containers, network, etc except volume. If you want to delete the volume also then run

gaurav@learning-ocean:~$ docker-compose down --volume

and it will delete volume also.