Docker Compose Port Mapping
Last Updated :
31 May, 2025
When you run an application inside a Docker container, it operates in isolation. To access it from your computer or the internet, you need port mapping. Before we cover port mapping, let’s quickly understand Docker Compose, a tool that lets you define and run multiple containers together using a simple YAML file.
Port mapping allows external access to your container by linking its internal port to a port on your host machine. For example, accessing localhost:8080
routes your request to the correct container. In this article, we’ll walk through how Docker Compose handles port mapping step-by-step.
Pre-requisites
Before moving on to the next section, make sure that you have installed Docker and Docker Compose on your system. If you have not installed Docker and Docker Compose, then follow these articles to install Docker and Docker Compose on your system.
How Port Mapping Works in Docker Compose Files
In Docker Compose, port mapping is set up in the docker-compose.yml
file using the ports
option. You specify which port on your computer should connect to which port inside the container. The format usually looks like this:
ports:
- "host_port:container_port"
For example, if you write "8080:80," it means your computer's port 8080 will forward traffic to port 80 inside the container. When you type localhost:8080 in your browser, it will access the app running inside the container on port 80
Steps To Do Port Mapping In A Docker Compose YAML File
The below are the steps you can follow to do port mapping in a docker compose yaml file.
Step 1 : Create a Docker Compose YAML file .
version: '3.1'
services:
webserver:
image: nginx
ports:
- 80:80
Step 2 : Then run the docker compose file using the command below .
docker compose up -d
Here the flag '-d' is used to run the container in the detached mode .
Step 3 : Now connect localhost:80 from your browser you will see you can connect the nginx web page.
Step 4 : You can also map multiple ports to port 80 and also can connect from the port from the browser . Look this updated Docker Compose file where i have mentioned multiple ports.
version: '3.1'
services:
webserver:
image: nginx
ports:
- 80:80
- 8080:80
- 8091:80
Step 5 : Now again run the docker compose command .
docker compose up -d
Step 6 : Connect the ports 80,8080 and 8091 from the browser to see the nginx server .
Through port 80 :
Through port 8080 :
Through port 8091 :
Common Use Cases for Port Mapping in Docker Compose
The below are some use cases of port mapping in Docker Compose:
1. Accessing Web Applications
When you run a web app inside a container, like a website or API, it usually listens on a certain port inside the container (for example, port 80). To open that app in your browser, you map that container port to a port on your computer, such as 8080. This way, you can visit http://localhost:8080
and see your app.
2. Connecting to Databases
If you run a database like MySQL or PostgreSQL inside a container, you can map its default port (e.g., 3306 for MySQL) to your computer’s port. This lets you use database tools on your machine to connect to the database inside the container.
3. API Gateways and Microservices
In setups with many services (microservices), an API gateway listens on a port to receive requests and sends them to the right service inside containers. Port mapping makes the gateway accessible from outside the Docker environment.
4. Development and Debugging
When developing apps, you might need to access special ports for debugging tools or logs. Port mapping helps expose those ports so you can debug or monitor the app easily on your computer.
5. Scaling and Load Balancing
If you run multiple copies of the same app for better performance, port mapping works with load balancers to distribute traffic evenly. This ensures your app stays available and fast.
Conclusion
Here in this article you have first understand about Docker Compose. After this you have created a Docker Compose file by using a Nginx image and also you have exposed the port in the Docker Compose so that you can connect the Nginx from the browser at the port 80. We have also discussed how to map multiple ports to one container and why port mapping is useful for web apps, databases, APIs, and development. Port mapping is key to making your Docker apps easy to use and reach from outside the container.