This article will introduce container operations using Docker Compose.
What is Docker Compose?
Docker Compose is a tool that launches multiple Docker containers simultaneously.
From a practical standpoint, it is likely that multiple containers will be used together.
With Docker Compose, you can define the launch of multiple containers in a YAML file and launch them all together with a single command.
Docker Compose Practice
Practice using Docker Compose. It should have been installed with Docker. Run Docker Compose and verify that the help menu appears.
docker compose
>>
Usage: docker compose [OPTIONS] COMMAND
Define and run multi-container applications with Docker
Options:
--all-resources Include all resources, even those not
used by services
....
This time, we will build a simple web server with Nginx + MySQL.
Prepare the necessary files.
This time, prepare the following file configurations:
The docker-compose.yml file defines the containers to be launched.
demo-compose/
├── docker-compose.yml
├── nginx/
│ └── index.html
version: '3.8'
services:
web:
image: nginx:latest
ports:
- "8080:80"
volumes:
- ./nginx:/usr/share/nginx/html:ro
depends_on:
- db
networks:
- demo-net
db:
image: mysql:8.0
environment:
MYSQL_ROOT_PASSWORD: rootpass
MYSQL_DATABASE: demo_db
MYSQL_USER: demo_user
MYSQL_PASSWORD: demopass
ports:
- "3306:3306"
networks:
- demo-net
networks:
demo-net:
driver: bridge
Each container is defined in the service. In this case, the web (Nginx) and database (MySQL) containers are defined. Since it is assumed that the two containers can communicate with each other, a network called “demo-net” is defined to bridge them.
In this case, I will use the images that were published on Docker Hub.
<!DOCTYPE html>
<html>
<head>
<title>Docker Compose Demo</title>
</head>
<body>
<h1>Hello, Docker Compose with Nginx and MySQL!</h1>
</body>
</html>
Launch Containers
To start Docker Compose, run the following command from within the docker-compose.yml directory:
docker compose up -d
Check the created images.
docker images
>>
REPOSITORY TAG IMAGE ID CREATED SIZE
mysql 8.0 bfeba808a667 11 days ago 1.07GB
nginx latest 84ec966e61a8 2 weeks ago 279MB
Check the created containers.
docker ps -a
>>
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
eba54eeefc46 nginx:latest "/docker-entrypoint.…" 16 hours ago Up 10 seconds 0.0.0.0:8080->80/tcp docker_compose-web-1
670d149771c8 mysql:8.0 "docker-entrypoint.s…" 16 hours ago Up 10 seconds 0.0.0.0:3306->3306/tcp, 33060/tcp docker_compose-db-1
The Docker images and containers have been created and are running.
To view the contents of index.html, access ‘http://localhost:8080’.

The advantage of Docker Compose is that it allows you to launch multiple containers with a single command.
Launching multiple containers without Docker Compose
This process can be repeated without Docker Compose. The following commands can be used to do so:
First, create a network.
docker network create demo-net
Then, start the MySQL container.
docker run -d --name demo-mysql --network demo-net -e MYSQL_ROOT_PASSWORD=rootpass -e MYSQL_DATABASE=demo_db -e MYSQL_USER=demo_user -e MYSQL_PASSWORD=demopass -p 3306:3306 mysql:8.0
Finally, the Nginx container is started, which completes the process.
docker run -d --name demo-nginx --network demo-net -p 8080:80 -v "$(pwd)/nginx:/usr/share/nginx/html:ro" nginx:latest
Thus, environment variables and other elements need to be defined within the command, which makes the command complex.
Another advantage of Docker Compose is that environment variables can be defined in the Docker-Compose.yml file, which simplifies the command itself.