Bird
Raised Fist0
Dockerdevops~20 mins

Docker Compose for dev environment - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Docker Compose Dev Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2:00remaining
Output of docker-compose up with multiple services
You run docker-compose up with this docker-compose.yml file:
version: '3.8'
services:
  web:
    image: nginx:alpine
    ports:
      - "8080:80"
  db:
    image: postgres:13
    environment:
      POSTGRES_PASSWORD: example

What will you see in the terminal output immediately after running the command?
Docker
version: '3.8'
services:
  web:
    image: nginx:alpine
    ports:
      - "8080:80"
  db:
    image: postgres:13
    environment:
      POSTGRES_PASSWORD: example
ABoth nginx and postgres containers start, logs from both services stream in the terminal.
BOnly nginx container starts; postgres container is created but not started.
CError: missing build context for nginx service.
DOnly postgres container starts; nginx container fails due to missing ports.
Attempts:
2 left
💡 Hint
Think about what docker-compose up does by default with multiple services.
❓ Configuration
intermediate
2:00remaining
Correct volume syntax for live code reload
You want to mount your local ./app folder into the container at /usr/src/app to enable live code reload during development. Which volume syntax in docker-compose.yml is correct?
A
volumes:
  - ./app\:/usr/src/app
B
volumes:
  - /usr/src/app:./app
C
volumes:
  - /app:usr/src/app
D
volumes:
  - ./app:/usr/src/app
Attempts:
2 left
💡 Hint
Local path comes first, then container path separated by a colon.
❓ Troubleshoot
advanced
2:00remaining
Why does docker-compose fail with 'port is already allocated' error?
You have this docker-compose.yml snippet:
services:
  web:
    image: nginx
    ports:
      - "8080:80"
  api:
    image: node
    ports:
      - "8080:3000"

You run docker-compose up and get an error: Bind for 0.0.0.0:8080 failed: port is already allocated. What is the cause?
Docker
services:
  web:
    image: nginx
    ports:
      - "8080:80"
  api:
    image: node
    ports:
      - "8080:3000"
AThe container port 80 is already used by another container.
BBoth services try to bind host port 8080, causing a conflict.
CDocker daemon is not running, so ports cannot be allocated.
DThe node image does not expose port 3000 by default.
Attempts:
2 left
💡 Hint
Host ports must be unique across all services.
🔀 Workflow
advanced
2:00remaining
Order of commands to update code in a running dev environment
You have a running dev environment with docker-compose up. You updated your local source code and want the container to use the new code without restarting the whole environment. What is the correct order of commands?
A1,3
B4,1,2
C3,1,2
D2,1,3
Attempts:
2 left
💡 Hint
You only need to rebuild and restart the containers to apply code changes.
✅ Best Practice
expert
2:00remaining
Choosing the best way to share environment variables securely
You want to share sensitive environment variables like database passwords with your dev containers using Docker Compose. Which approach is best practice?
AHardcode passwords directly in the <code>docker-compose.yml</code> file under <code>environment</code>.
BPass environment variables at runtime using <code>docker-compose run -e</code>.
CStore secrets in a Docker Swarm secret and use <code>secrets</code> in Compose.
DUse a separate <code>.env</code> file and reference variables in <code>docker-compose.yml</code>.
Attempts:
2 left
💡 Hint
Think about secure storage and avoiding exposing secrets in files.

Practice

(1/5)
1. What is the main purpose of using docker-compose.yml in a development environment?
easy
A. To build Docker images only
B. To define and run multiple containers with one command
C. To replace Docker CLI commands completely
D. To monitor container resource usage

Solution

  1. Step 1: Understand Docker Compose role

    Docker Compose uses a YAML file to define multiple containers and their settings.
  2. Step 2: Identify main benefit in dev

    It allows running all containers together with a single command, simplifying setup.
  3. Final Answer:

    To define and run multiple containers with one command -> Option B
  4. Quick Check:

    Compose = multiple containers, one command [OK]
Hint: Compose runs many containers with one file and command [OK]
Common Mistakes:
  • Thinking Compose only builds images
  • Believing Compose replaces all Docker CLI commands
  • Confusing Compose with monitoring tools
2. Which of the following is the correct syntax to expose port 8080 on the host mapped to port 80 in a docker-compose.yml service?
easy
A. ports: - "80:8080"
B. expose: - "80:8080"
C. expose: - "8080:80"
D. ports: - "8080:80"

Solution

  1. Step 1: Understand ports syntax in Compose

    The format is "host_port:container_port" to map host port to container port.
  2. Step 2: Match ports for host 8080 to container 80

    To expose container port 80 on host port 8080, use "8080:80" under ports.
  3. Final Answer:

    ports: - "8080:80" -> Option D
  4. Quick Check:

    HostPort:ContainerPort = 8080:80 [OK]
Hint: Ports use host:container format [OK]
Common Mistakes:
  • Reversing host and container ports
  • Using expose instead of ports for host mapping
  • Incorrect YAML indentation
3. Given this docker-compose.yml snippet, what will be the effect of the volume configuration?
services:
  app:
    image: myapp
    volumes:
      - ./src:/app/src
medium
A. The host's ./src folder is shared with /app/src inside the container
B. The container's /app/src folder is copied to host's ./src folder once
C. The container will have an empty /app/src folder
D. The volume will cause a syntax error

Solution

  1. Step 1: Understand volume syntax in Compose

    Format "host_path:container_path" mounts host folder inside container.
  2. Step 2: Analyze given volume mapping

    Host folder ./src is mounted to container path /app/src, sharing files live.
  3. Final Answer:

    The host's ./src folder is shared with /app/src inside the container -> Option A
  4. Quick Check:

    Volume mount = host folder shared inside container [OK]
Hint: Volumes share host folder inside container path [OK]
Common Mistakes:
  • Thinking volume copies files only once
  • Confusing source and target paths
  • Assuming volume creates empty folder
4. You wrote this docker-compose.yml but get an error when running docker-compose up. What is wrong?
services:
  web:
    image: nginx
    ports:
      - "80:80"
    volumes:
      - ./html:/usr/share/nginx/html
    environment:
      - DEBUG=true
    command: nginx -g 'daemon off;'
    restart: always
  db:
    image: postgres
    environment:
      POSTGRES_PASSWORD: example
    volumes:
      - pgdata:/var/lib/postgresql/data
volumes:
  pgdata:
medium
A. The environment variable for db should use list syntax or key-value mapping consistently
B. The volume pgdata is not defined properly under volumes
C. The command for web service is invalid
D. The environment variable for db uses wrong syntax, should be a list

Solution

  1. Step 1: Inspect YAML syntax in command field

    The command: nginx -g 'daemon off;' lacks quotes around the full string, causing YAML to parse it as separate unquoted scalars, leading to a syntax error.
  2. Step 2: Confirm other sections are valid

    Environment uses valid list/map styles (mixed OK), volumes properly defined.
  3. Final Answer:

    The command for web service is invalid -> Option C
  4. Quick Check:

    Multi-word command needs full quotes or YAML list [OK]
Hint: YAML command with spaces: quote whole or use list [OK]
Common Mistakes:
  • Blaming environment syntax mixing (both styles valid)
  • Misreading pgdata volume definition
  • Overlooking unquoted multi-word command
5. You want to set up a development environment with a web app and a database using Docker Compose. The web app needs live code updates from your host machine, and the database should persist data between restarts. Which docker-compose.yml configuration correctly achieves this?
hard
A. services: web: image: mywebapp volumes: - ./code:/app ports: - "3000:3000" db: image: mysql environment: MYSQL_ROOT_PASSWORD: rootpass volumes: - dbdata:/var/lib/mysql volumes: dbdata:
B. services: web: image: mywebapp ports: - "3000:3000" db: image: mysql environment: MYSQL_ROOT_PASSWORD: rootpass volumes: dbdata: driver: local
C. services: web: image: mywebapp volumes: - /app:/code ports: - "3000:3000" db: image: mysql environment: MYSQL_ROOT_PASSWORD: rootpass volumes: - dbdata:/var/lib/mysql volumes: dbdata:
D. services: web: image: mywebapp volumes: - ./code:/app ports: - "3000:3000" db: image: mysql environment: MYSQL_ROOT_PASSWORD: rootpass

Solution

  1. Step 1: Ensure live code updates for web app

    Mount host folder ./code to container /app using volumes for live updates.
  2. Step 2: Ensure database data persistence

    Use a named volume (dbdata) mounted to /var/lib/mysql to keep data between restarts.
  3. Step 3: Check ports and environment

    Expose port 3000 for web and set MYSQL_ROOT_PASSWORD for db environment.
  4. Final Answer:

    Configuration with volumes for web and named volume for db -> Option A
  5. Quick Check:

    Volumes for live code and persistent db = services: web: image: mywebapp volumes: - ./code:/app ports: - "3000:3000" db: image: mysql environment: MYSQL_ROOT_PASSWORD: rootpass volumes: - dbdata:/var/lib/mysql volumes: dbdata: [OK]
Hint: Use volumes for code and named volume for DB persistence [OK]
Common Mistakes:
  • Not mounting host code folder for live updates
  • Missing volume for database persistence
  • Incorrect volume paths or missing volumes section
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