By Fatskills Exam Guides Team — the exam nerds behind 28,500+ quizzes and 2.1M practice questions across 500+ global exams.
Define a multi-container application with a single configuration file using Docker Compose. docker-compose.yml syntax.
mkdir myproject && cd myproject
docker-compose.yml
version: '3'
services: web: image: httpd:latest
docker-compose up -d
docker-compose ps
docker-compose stop
docker-compose rm -f
rm docker-compose.yml
Purpose: Run a web service on port 8080.Command: services: web: image: httpd:latest ports: - "8080:80" Brief explanation of flags: ports flag maps the host port 8080 to the container port 80.
services: web: image: httpd:latest ports: - "8080:80"
ports
Purpose: Run a database service with a persistent volume.Command: services: db: image: mysql:latest volumes: - ./data:/var/lib/mysql Brief explanation of flags: volumes flag mounts the host directory ./data to the container directory /var/lib/mysql.
services: db: image: mysql:latest volumes: - ./data:/var/lib/mysql
volumes
./data
/var/lib/mysql
Purpose: Run services on a custom network.Command: services: web: image: httpd:latest networks: - mynet db: image: mysql:latest networks: - mynet Brief explanation of flags: networks flag defines a custom network mynet for services to communicate.
services: web: image: httpd:latest networks: - mynet db: image: mysql:latest networks: - mynet
networks
mynet
Error message or symptom: ERROR: The Compose file '.\docker-compose.yml' is invalid because... Why it happens: YAML syntax errors in the docker-compose.yml file.One-line fix: docker-compose config --check
ERROR: The Compose file '.\docker-compose.yml' is invalid because...
docker-compose config --check
Error message or symptom: ERROR: Service 'web' not found. Why it happens: Service name mismatch in the docker-compose.yml file.One-line fix: docker-compose config --services
ERROR: Service 'web' not found.
docker-compose config --services
Error message or symptom: ERROR: Network 'mynet' not found. Why it happens: Network name mismatch in the docker-compose.yml file.One-line fix: docker-compose config --networks
ERROR: Network 'mynet' not found.
docker-compose config --networks
Situation: You want to check if the web service is running.Command to run: docker-compose ps What a correct response looks like: Name Command State Ports web httpd-foreground Up 0.0.0.0:8080->80/tcp
Name Command State Ports
web httpd-foreground Up 0.0.0.0:8080->80/tcp
Situation: You want to check if the custom network is created.Command to run: docker network ls What a correct response looks like: NETWORK ID NAME DRIVER SCOPE myproject_mynet mynet bridge local
docker network ls
NETWORK ID NAME DRIVER SCOPE
myproject_mynet mynet bridge local
Situation: You want to check if the volume is mounted.Command to run: docker volume ls What a correct response looks like: DRIVER VOLUME NAME local myproject_data
docker volume ls
DRIVER VOLUME NAME
local myproject_data
• version: '3' is the default version for Docker Compose 1.29 and later.• services is a required section in the docker-compose.yml file.• ports flag maps host ports to container ports.• volumes flag mounts host directories to container directories.• networks flag defines custom networks for services to communicate.• docker-compose config --check validates YAML syntax.• docker-compose config --services lists service names.• docker-compose config --networks lists network names.• docker-compose up --build builds images before starting containers.• docker-compose down --rmi local removes local images after stopping containers.• ⚠️ docker-compose rm -f removes all stopped containers.• ⚠️ docker-compose down --volumes removes all volumes after stopping containers.
services
docker-compose up --build
docker-compose down --rmi local
docker-compose down --volumes
Join 4M+ learners. Unlock unlimited quizzes, wrong-answer tracking, flashcards + reminders, study guides, and 1-on-1 challenges.