0
0
Dockerdevops~5 mins

Port mapping in Compose in Docker - Commands & Configuration

Choose your learning style9 modes available
Introduction
Port mapping in Docker Compose lets you connect your app running inside a container to your computer's network. This way, you can access the app from your browser or other tools outside the container.
When you want to run a web app inside a container and access it from your browser on your computer.
When multiple containers run on the same machine and you want to avoid port conflicts by mapping different ports.
When you need to expose a database or service running in a container to other apps on your local network.
When testing an app locally that listens on a specific port inside the container.
When sharing your app with teammates who connect to your machine's IP and port.
Config File - docker-compose.yml
docker-compose.yml
version: '3.8'
services:
  webapp:
    image: nginx:1.23
    ports:
      - "8080:80"

This file defines a service named webapp using the official nginx image version 1.23.

The ports section maps port 8080 on your computer to port 80 inside the container, where nginx listens by default.

This means when you open http://localhost:8080 in your browser, you see the nginx welcome page served from inside the container.

Commands
This command starts the containers 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 webapp ... done
-d - Run containers in the background (detached mode)
This command lists the running containers started by docker-compose, showing their status and port mappings.
Terminal
docker-compose ps
Expected OutputExpected
Name Command State Ports ---------------------------------------------------------------------------- webapp nginx -g 'daemon off;' Up 0.0.0.0:8080->80/tcp
This command fetches the default web page served by nginx inside the container through the mapped port 8080 on your computer.
Terminal
curl http://localhost:8080
Expected OutputExpected
<!DOCTYPE html> <html> <head> <title>Welcome to nginx!</title> </head> <body> <h1>Welcome to nginx!</h1> </body> </html>
This command stops and removes the containers, networks, and other resources created by docker-compose.
Terminal
docker-compose down
Expected OutputExpected
Stopping webapp ... done Removing webapp ... done Removing network default
Key Concept

If you remember nothing else from port mapping in Compose, remember: the format "host_port:container_port" connects your computer's port to the container's port.

Common Mistakes
Using the wrong port order like "80:8080" instead of "8080:80"
This maps the container's port 8080 to host port 80, which may not be what your app listens on, causing connection failures.
Always write host port first, then container port, like "8080:80" to map host port 8080 to container port 80.
Not exposing the container port in the Dockerfile or app configuration
If the app inside the container does not listen on the port you map, the connection will fail even if port mapping is correct.
Ensure the app inside the container listens on the container port you map, e.g., port 80 for nginx.
Trying to map a host port already in use by another service
Docker will fail to start the container because the host port is busy, causing errors.
Choose a free host port or stop the service using that port before running docker-compose.
Summary
Create a docker-compose.yml file with a ports section to map host ports to container ports.
Use 'docker-compose up -d' to start containers in the background with port mapping active.
Verify running containers and port mappings with 'docker-compose ps'.
Access the app on your computer using the host port you mapped.
Stop and clean up containers with 'docker-compose down' when done.