Steps 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 :
Docker Compose Port Mapping
Docker is an essential tool that simplifies the deployment process by encapsulating applications within a container. To facilitate various types of communication in a Docker container, we need to expose port. Here in this guide, I will first cover what is Docker and Docker Compose and how Docker has become an essential tool among developers. Then I will guide you through the steps to do port mapping in a Docker compose YAML file.
Contact Us