Bird
Raised Fist0
Dockerdevops~5 mins

Defining services in Docker - Commands & Configuration

Choose your learning style10 modes available
Introduction
When you want to run multiple containers together that work as parts of one app, you define services. Services let you describe each container's settings in one file so you can start them all easily.
When you want to run a web app and a database together on your computer.
When you need to start multiple containers with one command instead of many commands.
When you want to share network settings between containers automatically.
When you want to set environment variables or volumes for containers in one place.
When you want to stop or restart all parts of your app together.
Config File - docker-compose.yml
docker-compose.yml
version: '3.9'
services:
  web:
    image: nginx:1.23
    ports:
      - "8080:80"
  db:
    image: postgres:15
    environment:
      POSTGRES_USER: exampleuser
      POSTGRES_PASSWORD: examplepass
      POSTGRES_DB: exampledb
    volumes:
      - db-data:/var/lib/postgresql/data
volumes:
  db-data:

This file defines two services: web and db.

The web service uses the nginx image and maps port 8080 on your computer to port 80 in the container.

The db service uses the postgres image with environment variables to set up the database user, password, and database name. It also uses a volume to save data persistently.

The volumes section creates a named volume db-data to keep database files even if the container stops.

Commands
This command starts all services defined in the docker-compose.yml file in detached mode, so they run in the background.
Terminal
docker-compose up -d
Expected OutputExpected
Creating network "default" with the default driver Creating volume "db-data" with default driver Creating example_web_1 ... done Creating example_db_1 ... done
→
-d - Run containers in the background (detached mode)
This command lists all running services started by docker-compose, showing their status and ports.
Terminal
docker-compose ps
Expected OutputExpected
Name Command State Ports ---------------------------------------------------------------------------- example_db_1 docker-entrypoint.sh postgres Up 5432/tcp example_web_1 nginx -g 'daemon off;' Up 0.0.0.0:8080->80/tcp
This command shows the logs of the web service to help you check if it started correctly or if there are errors.
Terminal
docker-compose logs web
Expected OutputExpected
example_web_1 | /docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration example_web_1 | /docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/ example_web_1 | /docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh example_web_1 | 10-listen-on-ipv6-by-default.sh: Getting the checksum of /etc/nginx/conf.d/default.conf example_web_1 | 10-listen-on-ipv6-by-default.sh: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf example_web_1 | /docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh example_web_1 | /docker-entrypoint.sh: Configuration complete; ready for start up
This command stops and removes all containers, networks, and volumes created by docker-compose for this project.
Terminal
docker-compose down
Expected OutputExpected
Stopping example_web_1 ... done Stopping example_db_1 ... done Removing example_web_1 ... done Removing example_db_1 ... done Removing network default
Key Concept

If you remember nothing else from this pattern, remember: defining services in a docker-compose.yml file lets you start and manage multiple containers as one app easily.

Common Mistakes
Not mapping ports in the service definition
Without port mapping, you cannot access the service from your computer's browser or other tools.
Always include the ports section with host:container port mapping for services you want to access externally.
Forgetting to create volumes for databases
Without volumes, database data is lost when the container stops or is removed.
Define volumes in the compose file and attach them to database services to keep data persistent.
Running docker-compose up without -d and then closing the terminal
The containers stop when the terminal session ends if not run in detached mode.
Use the -d flag to run containers in the background so they keep running after you close the terminal.
Summary
Define each container as a service in docker-compose.yml with image, ports, environment, and volumes.
Use 'docker-compose up -d' to start all services in the background.
Use 'docker-compose ps' to check running services and 'docker-compose logs' to see their output.
Use 'docker-compose down' to stop and clean up all services and networks.

Practice

(1/5)
1. What is the main purpose of defining services in a Docker Compose file?
easy
A. To install software inside containers manually
B. To write shell scripts for container startup
C. To group containers with their settings for easy management
D. To create virtual machines inside Docker

Solution

  1. Step 1: Understand the role of services in Docker Compose

    Services define how containers run, including images, ports, and environment variables.
  2. Step 2: Recognize the benefit of grouping containers

    Grouping containers with their settings allows easy start, stop, and management of multiple containers together.
  3. Final Answer:

    To group containers with their settings for easy management -> Option C
  4. Quick Check:

    Services group containers = D [OK]
Hint: Services group containers and settings in one file [OK]
Common Mistakes:
  • Confusing services with scripts
  • Thinking services create virtual machines
  • Believing services install software manually
2. Which of the following is the correct way to define a service named web using the image nginx in a Docker Compose YAML file?
easy
A. services: web: image: nginx
B. services: web: image nginx
C. service: web: image: nginx
D. services: web: img: nginx

Solution

  1. Step 1: Check the correct YAML key for services

    The top-level key must be services (plural), not service.
  2. Step 2: Verify the syntax for image definition

    The correct syntax is image: nginx with a colon and proper indentation.
  3. Final Answer:

    services: web: image: nginx -> Option A
  4. Quick Check:

    Correct YAML keys and syntax = B [OK]
Hint: Use 'services:' plural and 'image:' with colon [OK]
Common Mistakes:
  • Using 'service' instead of 'services'
  • Missing colon after 'image'
  • Using 'img' instead of 'image'
3. Given this Docker Compose snippet, what port will be accessible on the host machine?
services:
  app:
    image: myapp
    ports:
      - "8080:80"
medium
A. Port 80 on the host
B. Port 8080 on the host
C. Port 80 inside the container only
D. Port 8080 inside the container only

Solution

  1. Step 1: Understand port mapping syntax

    The format "host_port:container_port" means host port 8080 maps to container port 80.
  2. Step 2: Identify which port is exposed on the host

    Port 8080 on the host machine will forward traffic to port 80 inside the container.
  3. Final Answer:

    Port 8080 on the host -> Option B
  4. Quick Check:

    Host port is first in mapping = A [OK]
Hint: Host port is before colon in ports mapping [OK]
Common Mistakes:
  • Confusing host and container ports
  • Thinking container port is exposed on host
  • Ignoring quotes around ports
4. Identify the error in this Docker Compose service definition:
services:
  db:
    image: postgres
    ports:
      - "5432:"
    environment:
      POSTGRES_PASSWORD: example
medium
A. Missing container port in ports mapping
B. Environment variable name is incorrect
C. Image name should be capitalized
D. Service name cannot be 'db'

Solution

  1. Step 1: Check ports syntax

    Ports must be mapped as "host_port:container_port" or just container port. Here, "5432:" is invalid because missing container port after colon.
  2. Step 2: Confirm environment and image correctness

    Environment variable and image name are correct; service name 'db' is valid.
  3. Final Answer:

    Missing container port in ports mapping -> Option A
  4. Quick Check:

    Ports need host:container or container port only = A [OK]
Hint: Ports need host:container or container port only [OK]
Common Mistakes:
  • Using only one port number with quotes
  • Assuming environment variable names are wrong
  • Thinking service names have restrictions
5. You want to define two services, frontend and backend, where frontend depends on backend to start first. Which is the correct way to express this dependency in Docker Compose?
hard
A. services: frontend: image: frontend-image backend: image: backend-image depends_on: - frontend
B. services: backend: image: backend-image depends_on: - frontend frontend: image: frontend-image
C. services: frontend: image: frontend-image links: - backend backend: image: backend-image
D. services: frontend: image: frontend-image depends_on: - backend backend: image: backend-image

Solution

  1. Step 1: Understand 'depends_on' usage

    The depends_on key under a service lists services that must start before it.
  2. Step 2: Identify correct dependency direction

    Since frontend depends on backend, frontend should have depends_on: - backend.
  3. Step 3: Check other options for correctness

    The incorrect options either reverse the dependency direction or use the deprecated links for dependency control.
  4. Final Answer:

    services: frontend: image: frontend-image depends_on: - backend backend: image: backend-image -> Option D
  5. Quick Check:

    depends_on under dependent service = C [OK]
Hint: Put depends_on under service that waits for others [OK]
Common Mistakes:
  • Reversing dependency direction
  • Using links instead of depends_on
  • Placing depends_on under wrong service
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