Day 18 of #90DaysOf DevOps Challenge

Docker-compose

Day 18 of #90DaysOf DevOps Challenge

❄Docker Compose

Using docker commands, we can only run and manage a single container at a time, but there can be scenarios when you need to create multiple containers and manage them. It would help if you had a single command solution to create various containers, manage them and then stop them with a single command.

Docker-compose does precisely what I mentioned above. It allows docker to set up the multi-container environment.

There is a three-step process to work with Docker Compose.

1. Define the application environment with Dockerfile for all services.

2. Create a docker-compose.yml file defining all services under the application.

3. Run the docker-compose up command to run all services under applications.

You can run the docker-compose down command to destroy the above infrastructure.

Pre-requisites:

  • You should have a Docker engine running on your system.

Docker-compose comes by default with docker installation.

  • You should know YAML because the docker-compose files are written in the YAML language.

Now, let's learn a little bit about YAML which is very important for a DevOps engineer because it is used in many DevOps tools, like docker, Ansible, K8s, pipelines, etc.

❄YAML

YAML is a human-readable data serialization language that is often used for writing configuration files.

YAML stands for yet another markup language or YAML ain’t markup language (a recursive acronym), which emphasizes that YAML is for data, not documents.

  • Yaml files use .yml or .yaml extension.

  • The indentation is very important in YAML as it does not use '{ }' like json or '[ ]', '?' symbols like other programming languages. The hierarchy of objects is defined by indentation only. So, you have to be very careful with indentation (white spaces) while using YAML.

Tip: Try to use spaces instead of tabs.

  • You can use the hash symbol '#' to comment out a line.

  • 3 dashes (---) are used to signal the start of a document, while each document ends with three dots (...).

  • The structure of a YAML file is a map or a list.

Docker-Compose Commands

Sample docker-compose file -

version : "3.3"
service
  web:
    image: nginx:latest
    ports:
    - "80:80"

  db:
    image: mysql
    ports:
    - "3306:3306"
    environment:
    - "MYSQL_ROOT_PASSWORD=test@123":s

build –

The build option is used to build images for services for which the build is defined.

$ docker-compose build             # Build all services
$ docker-compose build web         # Build single service

up –

Use to create docker containers with available services in the docker-compose.yml file in the current directory. Use the -d switch to launch containers in daemon mode.

$ docker-compose up -d            # Create all containers
$ docker-compose up -d web        # Create single container

down –

This will stop and delete all containers, network, and associated images for the services defined in a config file.

$ docker-compose down           # Restart all containers
$ docker-compose down web       # Restart single container

ps –

This will list all containers created for the services defined in a config file with their status, port bindings, and command.

$ docker-compose ps

exec –

This will execute a command to the running container. For example list files in a container associated with a web service. (Similar to the docker exec command)

$ docker-compose exec web ls -l

start –

This will start stopped containers of the services defined in the config file.

$ docker-compose start            # Start all containers
$ docker-compose start web        # Start single container

stop –

This will stop running containers for the services defined in the config file

$ docker-compose stop             # Stop all containers
$ docker-compose stop web         # Stop single container

restart –

This will restart containers of the services defined in the config file

$ docker-compose restart           # Restart all containers
$ docker-compose restart web       # Restart single container

pause –

This will pause running containers for the services defined in the config file.

$ docker-compose pause            # Pause all containers
$ docker-compose pause web        # Pause single container

unpause –

This will start paused containers for the services defined in the config file.

$ docker-compose unpause            # Start all paused containers
$ docker-compose unpause web        # Start single paused container

rm –

This will remove stopped containers for the services defined in the config file.

$ docker-compose rm               # Start all paused containers
$ docker-compose rm web           # Start single paused container

❄Task

Task 1 -

Learn how to use the docker-compose.yml file, to set up the environment, configure the services and links between different containers, and also to use environment variables in the docker-compose.yml file.

Example:

In below configuration file, we are creating two containers. One is for the Apache webpage and the other is for the database.

version: '3.3'
services:
  db:
     image: mysql
     container_name: mysql_db
     restart: always
     environment:
       MYSQL_ROOT_PASSWORD: pass
  web:
    image: apache
    build: ./webapp
    depends_on:
      - db
    container_name: apache_web
    restart: always
    ports:
      - 8080:80
  1. To see what are all the available environment variables for the image which you are using for your docker container, you can refer to the docker hub page for that image where you will get all the required information and environment details you need to specify in your configuration file.

In this case, I used the MySQL image so I referred to the below page -https://hub.docker.com/_/mysql

You can refer to the example on the same page.

❄Docker Compose

Using docker commands, we can only run and manage a single container at a time, but there can be scenarios when you need to create multiple containers and manage them. It would help if you had a single command solution to create various containers, manage them and then stop them with a single command.

Docker-compose does precisely what I mentioned above. It allows docker to set up the multi-container environment.

There is a three-step process to work with Docker Compose.

1. Define the application environment with Dockerfile for all services.

2. Create a docker-compose.yml file defining all services under the application.

3. Run the docker-compose up command to run all services under applications.

You can run the docker-compose down command to destroy the above infrastructure.

Pre-requisites:

  • You should have a Docker engine running on your system.

Docker-compose comes by default with docker installation.

  • You should know YAML because the docker-compose files are written in the YAML language.

Now, let's learn a little bit about YAML which is very important for a DevOps engineer because it is used in many DevOps tools, like docker, Ansible, K8s, pipelines, etc.

❄YAML

YAML is a human-readable data serialization language that is often used for writing configuration files.

YAML stands for yet another markup language or YAML ain’t markup language (a recursive acronym), which emphasizes that YAML is for data, not documents.

  • Yaml files use .yml or .yaml extension.

  • The indentation is very important in YAML as it does not use '{ }' like json or '[ ]', '?' symbols like other programming languages. The hierarchy of objects is defined by indentation only. So, you have to be very careful with indentation (white spaces) while using YAML.

Tip: Try to use spaces instead of tabs.

  • You can use the hash symbol '#' to comment out a line.

  • 3 dashes (---) are used to signal the start of a document, while each document ends with three dots (...).

  • The structure of a YAML file is a map or a list.

Docker-Compose Commands

Sample docker-compose file -

version : "3.3"
service
  web:
    image: nginx:latest
    ports:
    - "80:80"

  db:
    image: mysql
    ports:
    - "3306:3306"
    environment:
    - "MYSQL_ROOT_PASSWORD=test@123":s

build –

The build option is used to build images for services for which the build is defined.

$ docker-compose build             # Build all services
$ docker-compose build web         # Build single service

up –

Use to create docker containers with available services in the docker-compose.yml file in the current directory. Use the -d switch to launch containers in daemon mode.

$ docker-compose up -d            # Create all containers
$ docker-compose up -d web        # Create single container

down –

This will stop and delete all containers, network, and associated images for the services defined in a config file.

$ docker-compose down           # Restart all containers
$ docker-compose down web       # Restart single container

ps –

This will list all containers created for the services defined in a config file with their status, port bindings, and command.

$ docker-compose ps

exec –

This will execute a command to the running container. For example list files in a container associated with a web service. (Similar to the docker exec command)

$ docker-compose exec web ls -l

start –

This will start stopped containers of the services defined in the config file.

$ docker-compose start            # Start all containers
$ docker-compose start web        # Start single container

stop –

This will stop running containers for the services defined in the config file

$ docker-compose stop             # Stop all containers
$ docker-compose stop web         # Stop single container

restart –

This will restart containers of the services defined in the config file

$ docker-compose restart           # Restart all containers
$ docker-compose restart web       # Restart single contain

pause –

This will pause running containers for the services defined in the config file.

$ docker-compose pause            # Pause all containers
$ docker-compose pause web        # Pause single container

unpause –

This will start paused containers for the services defined in the config file.

$ docker-compose unpause            # Start all paused containers
$ docker-compose unpause web        # Start single paused container

rm –

This will remove stopped containers for the services defined in the config file.

$ docker-compose rm               # Start all paused containers
$ docker-compose rm web           # Start single paused container

❄Task

Task 1 -

Learn how to use the docker-compose.yml file, to set up the environment, configure the services and links between different containers, and also to use environment variables in the docker-compose.yml file.

Example:

In below configuration file, we are creating two containers. One is for the Apache webpage and the other is for the database.

version: '3.3'
services:
  db:
     image: mysql
     container_name: mysql_db
     restart: always
     environment:
       MYSQL_ROOT_PASSWORD: pass
  web:
    image: apache
    build: ./webapp
    depends_on:
      - db
    container_name: apache_web
    restart: always
    ports:
      - 8080:80
  1. To see what are all the available environment variables for the image which you are using for your docker container, you can refer to the docker hub page for that image where you will get all the required information and environment details you need to specify in your configuration file.

In this case, I used the MySQL image so I referred to the below page -https://hub.docker.com/_/mysql

You can refer to the example on the same page.

No alt text provided for this image

2. To link two different containers or define the dependency, we can use the depends_on option.

Task 2 -

  • Pull a pre-existing Docker image from a public repository (e.g. Docker Hub) and run it on your local machine. Run the container as a non-root user (Hint- Use the usermod command to give the user permission to docker). Make sure you reboot the instance after permitting the user.

From user Aish, I cannot do docker ps as it is a non-root user.

So, I added this user to the group - docker.

sudo usermod -a -G docker $USER

Need to reboot the system so that the changes can be applied.

sudo systemctl reboot

Now I can run docker commands without sudo. You can see that the user is added to the docker group.

id

Above command gives details about the current user's uid, gid, and groups to which the user is added.

I can create the docker container with the name C1 and image nginx.

docker run -itd --name C1 nginx

  • Inspect the container's running processes and exposed ports using the docker inspect command.

The below command gives detailed information about the container and we can find the ExposedPorts under config.

docker inspect C1

The below command will show the running processes in the container.

docker top C1

You can also use the ps aux command.

docker exec C1 ps aux
  • Use the docker logs command to view the container's log output.
docker logs C1

  • Use the docker stop and docker start commands to stop and start the container.

To stop the running container, run the below command -

docker stop C1

To start the stopped container, run the below command -

docker start C1

  • Use the docker rm command to remove the container when you're done.

To remove a running container, run the below command -

docker rm -f C1

Or you can first stop the container and then remove it without the -f (forcefully) flag.


That's it for this Article.

Thank you for reading! 📘