Now that we have a clean project structure (/app, /data, /conf) andDockerCompose is installed, it’s time to learn how to translate your commandsdocker runinto YAML services.
The goal: to move from multiple individual commands to a single configuration file that describes the entire stack.
Reminder: Example of adocker run simple
Suppose we want to start MariaDB:
docker run -d \
--name mariadb \
-e MYSQL_ROOT_PASSWORD=secret \
-v ./data/mariadb:/var/lib/mysql \
--network monprojet-network \
mariadb:10.11
For Apache/PHP:
docker run -d \
--name php-web \
-p 8080:80 \
-v ./app:/var/www/html \
--network monprojet-network \
php:8.2-apache
You can quickly imagine that if you have 5 or 6 containers, things get complicated.
Translation into YAMLDockerCompose
Here’s how the same services translate intodocker-compose.yml :
services:
mariadb:
image: mariadb:10.11
container_name: mariadb
environment:
MYSQL_ROOT_PASSWORD: ${DB_ROOT_PASSWORD} # récupéré depuis .env
volumes:
- ./data/mariadb:/var/lib/mysql
networks:
- monprojet-network
php-web:
image: php:8.2-apache
container_name: php-web
ports:
- "8080:80"
volumes:
- ./app:/var/www/html
networks:
- monprojet-network
networks:
monprojet-network:
driver: bridge
Explanations
| YAML Element | Role |
|---|---|
services: | Declares all containers (services) in the stack. |
image: | The imageDockerto use. |
container_name: | Container name (optional but useful). |
environment: | Environment variables (you can use .env). |
volumes: | Mounting local directories into the container. |
ports: | Host-to-container port mapping. |
networks: | Definition of the networks used by the container. |
networks: At the end | Declaring the network if necessary (bridge, overlay…). |
Tip: YAML is read from top to bottom, and each indentation is critical. Two spaces are recommended.
Key points
docker rundocker-compose.ymlReplaces all your commands.- Using
.envallows you to avoid hard-coding passwords or ports. - YAML is easy to read and edit, making it ideal for versioning your project.
- You can now launch the entire stack with:
docker compose up -d
- And stop everything with:
docker compose down
📌 Key takeaways
- Docker Compose simplifies multi-container management in YAML.
- Each
docker runbecomes a service in the YAML.- Dynamic variables are passed into
.env.- The folder structure (
/app,/data,/conf) integrates naturally into YAML.