Bird
Raised Fist0
Dockerdevops~10 mins

docker-compose.yml structure - Step-by-Step Execution

Choose your learning style10 modes available
Process Flow - docker-compose.yml structure
Start: Define version
↓
Define services
↓
For each service:
↓
Set image/build
↓
Set ports
↓
Set environment variables
↓
Set volumes
↓
Define networks (optional)
↓
Define volumes (optional)
↓
End: Compose file ready
The docker-compose.yml file starts with a version, then defines services with their settings, optionally networks and volumes, ending with a ready configuration.
Execution Sample
Docker
version: '3.8'
services:
  web:
    image: nginx
    ports:
      - "80:80"
Defines a compose file with version 3.8, one service named web using the nginx image, exposing port 80.
Process Table
StepYAML Section ReadAction TakenResulting Configuration State
1version: '3.8'Set compose file versionversion = '3.8'
2services:Start services sectionservices = {}
3web:Define service 'web'services = {web: {}}
4image: nginxSet image for 'web'services = {web: {image: 'nginx'}}
5ports:Start ports list for 'web'services = {web: {image: 'nginx', ports: []}}
6- "80:80"Add port mapping to 'web'services = {web: {image: 'nginx', ports: ['80:80']}}
7(end of file)Compose file fully parsedComplete config with version and one service 'web' with image and ports
💡 Reached end of file, full docker-compose.yml structure parsed
Status Tracker
VariableStartAfter Step 3After Step 4After Step 6Final
versionundefined'3.8''3.8''3.8''3.8'
servicesundefined{web: {}}{web: {image: 'nginx'}}{web: {image: 'nginx', ports: ['80:80']}}{web: {image: 'nginx', ports: ['80:80']}}
Key Moments - 3 Insights
Why do we start with 'version' in the docker-compose.yml?
The 'version' defines the syntax rules for the file. Execution_table step 1 shows setting the version first, which tells Docker Compose how to read the rest.
How are multiple services defined in the file?
Each service is a key under 'services'. Execution_table step 3 shows defining one service 'web'. Additional services would be siblings under 'services'.
What does the 'ports' section do?
It maps container ports to host ports. Execution_table steps 5 and 6 show adding port '80:80' to the 'web' service, meaning host port 80 connects to container port 80.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of 'services' after step 4?
A{web: {}}
B{}
C{web: {image: 'nginx'}}
Dundefined
💡 Hint
Check the 'Resulting Configuration State' column at step 4 in the execution_table.
At which step is the port mapping '80:80' added to the 'web' service?
AStep 6
BStep 3
CStep 5
DStep 2
💡 Hint
Look for the action 'Add port mapping to web' in the execution_table.
If we add another service under 'services', how would the 'services' variable change after step 3?
AIt would remain empty
BIt would include both services as keys
CIt would overwrite the existing 'web' service
DIt would cause an error
💡 Hint
Refer to variable_tracker and key_moments about how services are structured.
Concept Snapshot
docker-compose.yml structure:
- Start with 'version' to set syntax
- Define 'services' as main section
- Each service has keys: image, ports, environment, volumes
- Optional 'networks' and 'volumes' sections
- Indentation and YAML syntax must be correct
- Compose file configures multi-container Docker apps
Full Transcript
The docker-compose.yml file begins by specifying a version, which sets the rules for the file's syntax. Then it defines services, each representing a container configuration. For each service, you specify details like the image to use, ports to expose, environment variables, and volumes to mount. Optionally, you can define networks and volumes at the bottom. The execution table shows step-by-step how the file is read and how the configuration state builds up. Variables like 'version' and 'services' change as the file is parsed. Key moments clarify why version is first, how multiple services are added, and what ports do. The visual quiz tests understanding of these steps. This structure helps Docker Compose run multiple containers easily with one file.

Practice

(1/5)
1. What is the main purpose of the services section in a docker-compose.yml file?
easy
A. To define the containers that will run and their settings
B. To specify the Docker version to use
C. To list the volumes to be created
D. To set environment variables globally

Solution

  1. Step 1: Understand the role of services

    The services section lists all containers to run and their configurations.
  2. Step 2: Compare with other sections

    version sets compose file version, volumes defines shared storage, environment variables are per service or global but not the main purpose of services.
  3. Final Answer:

    To define the containers that will run and their settings -> Option A
  4. Quick Check:

    services = container definitions [OK]
Hint: Services section lists containers and their configs [OK]
Common Mistakes:
  • Confusing version with services
  • Thinking volumes are defined inside services only
  • Assuming environment variables are global by default
2. Which of the following is the correct way to specify a port mapping in docker-compose.yml under a service named web?
easy
A. port: - "80:80"
B. ports: - "80:80"
C. ports: - 80:80
D. mapping: - "80:80"

Solution

  1. Step 1: Check correct key for port mapping

    The correct key is ports (plural), not port or mapping.
  2. Step 2: Validate syntax for port mapping

    Port mappings must be strings with quotes like "80:80" to avoid YAML parsing issues.
  3. Final Answer:

    ports: - "80:80" -> Option B
  4. Quick Check:

    Use ports with quoted strings [OK]
Hint: Use 'ports' key with quoted strings for mappings [OK]
Common Mistakes:
  • Using singular 'port' instead of 'ports'
  • Not quoting port mappings causing YAML errors
  • Using wrong key like 'mapping'
3. Given this docker-compose.yml snippet:
version: '3'
services:
  app:
    image: myapp:latest
    environment:
      - DEBUG=true
    ports:
      - "5000:5000"
What will happen when you run docker-compose up?
medium
A. The container runs but port 5000 is not exposed
B. An error occurs because environment variables must be in key:value format
C. The image 'myapp:latest' is ignored and default image is used
D. A container named 'app' runs with DEBUG=true and port 5000 exposed

Solution

  1. Step 1: Analyze environment variable format

    Environment variables can be listed as strings like "DEBUG=true" in docker-compose.yml.
  2. Step 2: Check port mapping and image usage

    Port 5000 is correctly mapped and image 'myapp:latest' will be used to run the container.
  3. Final Answer:

    A container named 'app' runs with DEBUG=true and port 5000 exposed -> Option D
  4. Quick Check:

    Env vars as strings and ports mapped = container runs correctly [OK]
Hint: Env vars as strings and ports quoted work fine [OK]
Common Mistakes:
  • Thinking environment variables must be key:value pairs
  • Assuming ports need special syntax to expose
  • Believing image name is ignored if not official
4. Identify the error in this docker-compose.yml snippet:
version: '3'
services:
  db:
    image: postgres
    ports:
      - 5432:5432
    environment:
      POSTGRES_PASSWORD: example
medium
A. Port mapping should be quoted strings like "5432:5432"
B. Environment variables must be listed as a sequence, not mapping
C. The version '3' is invalid and should be '3.8'
D. The image name 'postgres' must include a tag

Solution

  1. Step 1: Check port mapping syntax

    Port mappings should be quoted strings like "5432:5432" to avoid YAML parsing errors.
  2. Step 2: Validate environment variable format

    Environment variables can be mappings like POSTGRES_PASSWORD: example, which is correct here.
  3. Final Answer:

    Port mapping should be quoted strings like "5432:5432" -> Option A
  4. Quick Check:

    Port mappings need quotes to parse correctly [OK]
Hint: Always quote port mappings to avoid YAML errors [OK]
Common Mistakes:
  • Not quoting port mappings
  • Confusing environment variable formats
  • Thinking version '3' is invalid
5. You want to run two services, web and db, where web depends on db. Which docker-compose.yml snippet correctly expresses this dependency and shares a volume named data between them?
hard
A. version: '3' services: web: image: webapp depends_on: - db db: image: postgres volumes: data: external: true
B. version: '3' services: web: image: webapp volumes: - data:/var/www/data db: image: postgres depends_on: - web volumes: - data:/var/lib/postgresql/data volumes: data:
C. version: '3' services: web: image: webapp depends_on: - db volumes: - data:/var/www/data db: image: postgres volumes: - data:/var/lib/postgresql/data volumes: data:
D. version: '3' services: web: image: webapp depends_on: - db volumes: - /data:/var/www/data db: image: postgres volumes: - /data:/var/lib/postgresql/data

Solution

  1. Step 1: Check dependency declaration

    web depends on db using depends_on with - db under web.
  2. Step 2: Verify volume sharing and declaration

    Both services use the named volume data mapped to their paths, and volumes: section declares data properly.
  3. Final Answer:

    version: '3' services: web: image: webapp depends_on: - db volumes: - data:/var/www/data db: image: postgres volumes: - data:/var/lib/postgresql/data volumes: data: -> Option C
  4. Quick Check:

    depends_on + named volume declared under volumes = correct [OK]
Hint: Use depends_on and declare shared volumes under volumes [OK]
Common Mistakes:
  • Reversing depends_on order
  • Using host paths instead of named volumes
  • Not declaring volumes section for named volumes
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard