Bird
Raised Fist0
Nginxdevops~20 mins

Docker Compose with Nginx - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Docker Compose Nginx Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2:00remaining
Nginx container port exposure
You run a Docker Compose setup with Nginx service exposing port 80. What is the output of docker-compose ps regarding the ports column?
Nginx
version: '3.8'
services:
  web:
    image: nginx:latest
    ports:
      - "8080:80"
Aweb_1 nginx:latest Up 80/tcp
Bweb_1 nginx:latest Up 0.0.0.0:80->8080/tcp
Cweb_1 nginx:latest Up 0.0.0.0:8080->80/tcp
Dweb_1 nginx:latest Up 8080/tcp
Attempts:
2 left
💡 Hint
Remember the format is host_port:container_port
❓ Configuration
intermediate
2:00remaining
Correct Nginx service restart policy in Docker Compose
Which restart policy ensures the Nginx container restarts automatically only if it crashes, but not if stopped manually?
Arestart: always
Brestart: on-failure
Crestart: no
Drestart: unless-stopped
Attempts:
2 left
💡 Hint
Think about restart only on crashes, not manual stops.
❓ Troubleshoot
advanced
2:00remaining
Nginx container fails to start due to port conflict
You have this Docker Compose snippet for Nginx:
ports:
  - "80:80"
But the container fails to start with an error about port 80. What is the most likely cause?
AThe container's internal port 80 is blocked by firewall
BThe container image is missing the Nginx binary
CDocker Compose file version is incompatible
DAnother process on the host is already using port 80
Attempts:
2 left
💡 Hint
Check what else might be using port 80 on your computer.
🔀 Workflow
advanced
2:00remaining
Order of commands to update Nginx container with new config
You updated the Nginx config file on your host. Which sequence of commands correctly applies the changes to the running container using Docker Compose?
A3
B4,1,2
C1,4,2
D2,3
Attempts:
2 left
💡 Hint
You only changed config, not the image.
✅ Best Practice
expert
2:00remaining
Best way to persist Nginx logs in Docker Compose
You want to keep Nginx logs on the host machine so they are not lost when the container is removed. Which Docker Compose volume configuration is best?
A
volumes:
  - ./logs:/var/log/nginx
B
volumes:
  - logs:/var/log/nginx
volumes:
  logs: {}
C
volumes:
  - /var/log/nginx
D
volumes:
  - /var/log/nginx:/var/log/nginx
Attempts:
2 left
💡 Hint
Map a host folder to container log folder.

Practice

(1/5)
1. What is the main purpose of using Docker Compose with Nginx?
easy
A. To replace Nginx with a different web server
B. To run Nginx and other services together easily using a YAML file
C. To write Nginx configuration files in JSON format
D. To compile Nginx from source code automatically

Solution

  1. Step 1: Understand Docker Compose role

    Docker Compose helps run multiple services together using a simple YAML file.
  2. Step 2: Connect with Nginx usage

    Using Docker Compose with Nginx lets you start Nginx and other needed services easily.
  3. Final Answer:

    To run Nginx and other services together easily using a YAML file -> Option B
  4. Quick Check:

    Docker Compose + Nginx = easy multi-service setup [OK]
Hint: Docker Compose uses YAML to run multiple services together [OK]
Common Mistakes:
  • Thinking Docker Compose changes Nginx config format
  • Confusing Docker Compose with Nginx replacement
  • Assuming Docker Compose compiles software
2. Which of the following is the correct way to expose port 80 of an Nginx container in a Docker Compose file?
easy
A. publish: 80
B. expose: 80:80
C. ports: - "80:80"
D. port: 80

Solution

  1. Step 1: Recall Docker Compose port syntax

    To expose ports, use the 'ports' key with format "hostPort:containerPort" as a list item.
  2. Step 2: Match correct syntax

    Only 'ports: - "80:80"' correctly exposes container port 80 to host port 80.
  3. Final Answer:

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

    Expose ports with 'ports: - "host:container"' [OK]
Hint: Use 'ports: - "hostPort:containerPort"' to expose ports [OK]
Common Mistakes:
  • Using 'expose' with port mapping syntax
  • Writing 'port' instead of 'ports'
  • Using 'publish' which is invalid in Compose
3. Given this Docker Compose snippet, what will happen when you run docker-compose up?
services:
  web:
    image: nginx:latest
    ports:
      - "8080:80"
    volumes:
      - ./site-content:/usr/share/nginx/html:ro
medium
A. Nginx will serve files from /usr/share/nginx/html on port 80 only
B. Nginx will serve default content on port 80
C. Nginx will fail to start due to volume error
D. Nginx will serve files from ./site-content on port 8080

Solution

  1. Step 1: Analyze port mapping

    Port 8080 on host maps to port 80 in container, so access via localhost:8080.
  2. Step 2: Analyze volume mapping

    Local folder './site-content' is mounted read-only to Nginx's html folder, so custom files are served.
  3. Final Answer:

    Nginx will serve files from ./site-content on port 8080 -> Option D
  4. Quick Check:

    Port 8080 mapped and volume mounted = custom site served [OK]
Hint: Check ports and volumes to predict container behavior [OK]
Common Mistakes:
  • Ignoring port mapping and assuming port 80
  • Assuming volume mount fails without error
  • Confusing container and host ports
4. You wrote this Docker Compose service for Nginx but after running docker-compose up the container starts but you cannot access Nginx on port 80:
services:
  nginx:
    image: nginx
    ports:
      - "80"
    volumes:
      - ./html:/usr/share/nginx/html
What is the likely cause?
medium
A. Port mapping "80" maps a random host port to container 80; should be "80:80"
B. Volume path './html' does not exist
C. Image name 'nginx' is invalid
D. Missing environment variables for Nginx

Solution

  1. Step 1: Check port mapping syntax

    "80" is valid short syntax mapping random host port to container port 80; to bind host port 80 use "80:80".
  2. Step 2: Confirm other parts

    Volume path and image name are valid; environment variables are optional for basic Nginx.
  3. Final Answer:

    Port mapping "80" maps a random host port to container 80; should be "80:80" -> Option A
  4. Quick Check:

    Port mapping must be "hostPort:containerPort" [OK]
Hint: Port mapping needs both host and container ports separated by colon [OK]
Common Mistakes:
  • Using single port number without colon
  • Assuming volume path error without checking
  • Thinking image name 'nginx' is wrong
5. You want to run Nginx with Docker Compose and add a custom configuration file nginx.conf located in your project folder. Which Docker Compose volume setting correctly mounts this file to override the default Nginx config at /etc/nginx/nginx.conf?
hard
A. - ./nginx.conf:/etc/nginx/nginx.conf:ro
B. - ./nginx.conf:/usr/share/nginx/html/nginx.conf
C. - /etc/nginx/nginx.conf:./nginx.conf
D. - ./nginx.conf:/etc/nginx/conf.d/nginx.conf

Solution

  1. Step 1: Identify correct Nginx config path

    The main Nginx config file is at /etc/nginx/nginx.conf inside the container.
  2. Step 2: Match volume mount syntax

    Mount local nginx.conf to container path with read-only flag ':ro' to override default config safely.
  3. Final Answer:

    - ./nginx.conf:/etc/nginx/nginx.conf:ro -> Option A
  4. Quick Check:

    Mount local config to /etc/nginx/nginx.conf with ':ro' [OK]
Hint: Mount config file to /etc/nginx/nginx.conf with ':ro' for safety [OK]
Common Mistakes:
  • Mounting to wrong container path
  • Reversing source and target paths
  • Omitting ':ro' for config files
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