0
0
Dockerdevops~20 mins

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

Choose your learning style9 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