Fatskills
Practice. Master. Repeat.
Study Guide: Docker Docker Docker Compose dockercomposeyml Syntax Version Services Networks Volumes
Source: https://www.fatskills.com/kubernetes/chapter/docker-docker-docker-compose-dockercomposeyml-syntax-version-services-networks-volumes

Docker Docker Docker Compose dockercomposeyml Syntax Version Services Networks Volumes

By Fatskills Exam Guides Team — the exam nerds behind 28,500+ quizzes and 2.1M practice questions across 500+ global exams.

⏱️ ~3 min read

1. Core Command / Concept

Define a multi-container application with a single configuration file using Docker Compose. docker-compose.yml syntax.

2. Step-by-Step

  1. Create a new directory for your project and navigate into it: mkdir myproject && cd myproject
  2. Create a new file called docker-compose.yml and add the following content: version: '3'
  3. Define a service in the docker-compose.yml file: services: web: image: httpd:latest
  4. ⚠️ Start the containers in detached mode: docker-compose up -d
  5. Check the containers are running: docker-compose ps
  6. Stop the containers: docker-compose stop
  7. ⚠️ Remove all stopped containers: docker-compose rm -f
  8. Clean up the docker-compose.yml file: rm docker-compose.yml

3. Real Commands with Examples


Example 1: Define a web service with a custom port

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.

Example 2: Define a database service with a volume

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.

Example 3: Define a network for services to communicate

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.

4. Common Errors


Error 1: Invalid YAML syntax

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 2: Service not found

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 3: Network not found

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

5. Quick Checks


Scenario 1: Check if a service is running

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

Scenario 2: Check if a network is created

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

Scenario 3: Check if a volume is mounted

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

6. Last-Minute Reference

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.




ADVERTISEMENT