Bird
Raised Fist0
Dockerdevops~20 mins

Port mapping in Compose in Docker - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Port Mapping Pro
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2:00remaining
Understanding port mapping syntax in docker-compose
Given this snippet from a docker-compose.yml file, what port will be accessible on the host machine?
services:
  web:
    image: nginx
    ports:
      - "8080:80"
APort 80 on the host will be accessible
BPort 8080 on the container will be accessible on port 80 of the host
CPort 80 on the container will be accessible on port 80 of the host
DPort 8080 on the host will be accessible
Attempts:
2 left
💡 Hint
Remember the format is hostPort:containerPort
🧠 Conceptual
intermediate
2:00remaining
Effect of omitting host port in docker-compose ports
What happens if you specify only the container port in the ports section of docker-compose.yml like this?
services:
  app:
    image: myapp
    ports:
      - "5000"
AThe container will not expose any ports to the host
BPort 5000 will be exposed on the host with the same port number
CDocker will randomly assign a free port on the host mapped to container port 5000
DThis syntax is invalid and will cause a docker-compose error
Attempts:
2 left
💡 Hint
Think about how Docker handles ports when host port is not specified
❓ Troubleshoot
advanced
2:00remaining
Troubleshooting port conflicts in docker-compose
You have two services in your docker-compose.yml:
services:
  service1:
    image: app1
    ports:
      - "8080:80"
  service2:
    image: app2
    ports:
      - "8080:80"

What will happen when you try to start both services together?
ADocker-compose will fail to start the second service due to port conflict
BThe second service will start but override the first service's port mapping
CBoth services will start successfully and share port 8080 on the host
DDocker will automatically assign a different host port to the second service
Attempts:
2 left
💡 Hint
Two services cannot bind the same host port at the same time
🔀 Workflow
advanced
2:00remaining
Configuring multiple port mappings in docker-compose
You want to expose container ports 80 and 443 to host ports 8080 and 8443 respectively. Which docker-compose ports configuration achieves this?
A
ports:
  - "8080:80"
  - "8443:443"
B
ports:
  - "80:8080"
  - "443:8443"
C
ports:
  - "8080:443"
  - "8443:80"
D
ports:
  - "80"
  - "443"
Attempts:
2 left
💡 Hint
Remember the format is hostPort:containerPort
✅ Best Practice
expert
3:00remaining
Best practice for exposing ports in production with docker-compose
Which approach is best to securely expose a web service running in a container on port 80 to the internet using docker-compose?
AMap container port 80 directly to host port 80 and open host firewall for port 80
BUse a reverse proxy container mapped to host ports 80 and 443, forwarding to the web service container on internal ports
CExpose container port 80 without mapping to host ports and rely on Docker internal networking
DMap container port 80 to a random host port and share that port publicly
Attempts:
2 left
💡 Hint
Think about security and flexibility in production environments

Practice

(1/5)
1. What does the ports section in a Docker Compose file do?
easy
A. It defines the container's CPU and memory limits.
B. It sets environment variables inside the container.
C. It specifies the container's restart policy.
D. It maps ports from the host machine to the container ports.

Solution

  1. Step 1: Understand the role of ports in Compose

    The ports section connects a port on your computer (host) to a port inside the container.
  2. Step 2: Identify what ports does not do

    It does not set environment variables, resource limits, or restart policies.
  3. Final Answer:

    It maps ports from the host machine to the container ports. -> Option D
  4. Quick Check:

    Port mapping = connecting host to container ports [OK]
Hint: Ports section maps host ports to container ports [OK]
Common Mistakes:
  • Confusing ports with environment variables
  • Thinking ports set resource limits
  • Mixing ports with restart policies
2. Which of the following is the correct syntax to map port 8080 on the host to port 80 in a Docker Compose service?
easy
A. ports: - "8080:80"
B. ports: - "80"
C. ports: - "8080"
D. ports: - "80:8080"

Solution

  1. Step 1: Understand port mapping format

    The format is host_port:container_port. To map host 8080 to container 80, write "8080:80".
  2. Step 2: Check each option

    ports: - "80:8080" reverses ports, mapping host 80 to container 8080, which is incorrect. Options B and C specify only one port, which publishes the container port to a randomly assigned host port, not specific host 8080.
  3. Final Answer:

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

    Host port first, container port second [OK]
Hint: Remember format: host_port:container_port [OK]
Common Mistakes:
  • Reversing host and container ports
  • Using single port without mapping
  • Incorrect YAML indentation
3. Given this Compose snippet:
services:
  web:
    image: nginx
    ports:
      - "5000:80"
What happens when you open http://localhost:5000 in your browser?
medium
A. You get a connection refused error because port 80 is not exposed.
B. You access the Nginx server running inside the container on port 80.
C. You access port 5000 inside the container, which is not running any service.
D. The browser will show the Docker Compose file content.

Solution

  1. Step 1: Understand port mapping in the snippet

    Port 5000 on the host is mapped to port 80 inside the container where Nginx listens.
  2. Step 2: Explain browser behavior

    Opening http://localhost:5000 sends requests to container's port 80, so you reach the Nginx server.
  3. Final Answer:

    You access the Nginx server running inside the container on port 80. -> Option B
  4. Quick Check:

    Host port 5000 maps to container port 80 [OK]
Hint: Host port connects to container port service [OK]
Common Mistakes:
  • Thinking port 80 is not exposed
  • Confusing host and container ports
  • Expecting file content instead of service
4. You wrote this Compose ports mapping:
ports:
  - "80:8080"
But your app inside the container listens on port 8080. Why can't you access it via http://localhost:80?
medium
A. The container port 8080 is not exposed correctly to the host port 80.
B. The ports are reversed; host port should be 8080 and container port 80.
C. The app inside the container is not running on port 8080.
D. The Compose file syntax is invalid.

Solution

  1. Step 1: Analyze the port mapping

    Mapping "80:8080" means host port 80 connects to container port 8080.
  2. Step 2: Identify the problem

    If access fails, likely the container app is not listening on port 8080 or firewall blocks host port 80. The mapping syntax is correct.
  3. Final Answer:

    The app inside the container is not running on port 8080. -> Option C
  4. Quick Check:

    Check app listens on container port [OK]
Hint: Check app listens on container port, not just mapping [OK]
Common Mistakes:
  • Assuming ports reversed when they are correct
  • Ignoring app's actual listening port
  • Blaming syntax without checking app
5. You want to run two services in Compose: a web app on container port 3000 and an API on container port 5000. You want to access them on host ports 3000 and 5000 respectively. Which Compose ports section is correct?
hard
A. web: ports: - "3000:3000" api: ports: - "5000:5000"
B. web: ports: - "3000:80" api: ports: - "5000:80"
C. web: ports: - "80:3000" api: ports: - "80:5000"
D. web: ports: - "3000" api: ports: - "5000"

Solution

  1. Step 1: Match host and container ports

    To access web on host 3000 and container 3000, map "3000:3000". Similarly for API, map "5000:5000".
  2. Step 2: Check other options

    web: ports: - "3000:80" api: ports: - "5000:80" maps container ports incorrectly to 80. web: ports: - "80:3000" api: ports: - "80:5000" reverses ports. web: ports: - "3000" api: ports: - "5000" uses short syntax without mapping, which assigns random host ports.
  3. Final Answer:

    web: ports: - "3000:3000" api: ports: - "5000:5000" -> Option A
  4. Quick Check:

    Host and container ports must match for access [OK]
Hint: Map host port to same container port for easy access [OK]
Common Mistakes:
  • Reversing host and container ports
  • Using single port without mapping
  • Mapping both services to same container port
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