Docker Compose Port Mapping
Last Updated :
24 May, 2024
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.
What is Docker?
Docker is an important tool that is used to encapsulate the application and its dependencies into compact units called Docker containers. The Docker container contains all the application code, runtime, libraries, and other tools that an application needs to run. Here for the application, first a Dockerfile is created. The Dockerfile consists of a base image, commands to install dependencies, and commands to run the application. This Dockerfile is then built to generate a Docker image. The Docker image is very lightweight and portable. By using this Docker image, we can run a Docker container on any operating system with the only condition is the system should have installed Docker on it. This allows developers to run their application code on their operating system without facing any errors related to the operating system or hardware requirements. Developers can run multiple Docker containers on a single host. This results in maximizing resource utilization and reducing infrastructure costs. In summary, we can say Docker has become a very important and essential tool for developers and organizations to accelerate their software deployment and delivery pipelines.
What Is Docker Compose?
Docker Compose is an important tool for managing multi-docker container applications. Here all the configurations, services, dependencies, and environment variables of the application are defined in a single YAML file. In this YAML file, multiple docker container configuration details are written. With a single command, users can start all the docker containers that are mentioned in the Docker Compose YAML file. Docker Compose allows users to easily deploy, scale, and manage the docker containers. This saves time and effort in the development process. It increases productivity and efficiency. As the deployment process becomes more easy, users can now easily focus more on developing application logic. Overall Docker Compose simplifies complex workflows and accelerates development cycles making it a vital tool in the modern software development and deployment process.
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 detailed geeks for geeks articles to install Docker and Docker Compose on your system.
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 :

Conclusion
Here in this article you have first understand what is Docker . Then you have 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 .
Similar Reads
What Is Docker Compose Up?
Docker Compose is a powerful tool utilized for defining and running multi-container Docker applications. It improves on the most common way of managing complex applications composed of multiple interconnected services by allowing you to characterize their configuration in a single YAML file. With Do
15+ min read
Docker - Managing Ports
Pre-requisites: Docker Docker is a set of platform-as-a-service products that use OS-level virtualization to deliver software in packages called containers. These containers may need to talk to each other or to services outside docker, for this we not only need to run the image but also expose the c
4 min read
Docker Compose Restart Policy
The capability of delivering application availability and resilience in the dynamic world of software development is enormous. Docker Compose is among the powerful tools in the Docker ecosystem that make multi-container application management very easy by letting developers describe services, networ
8 min read
Installation of Docker Compose
Docker Compose is a powerful tool that helps in simplifying the orchestration of multi-container docker applications. In this we have to configure the application services inside the Yaml file and docker-compose will read the configuration data from that Yaml script. Before Moving into the installat
4 min read
Docker Compose
An open-source platform called Docker makes designing, shipping, and deploying applications simple. It runs an application in an isolated environment by compiling its dependencies into a so-called container. for additional information on Docker. In a normal case, several services, such as a database
15+ min read
docker compose override
Docker is a widely used platform designed to help developers build, share, and run container applications. We handle the tedious setup so that you can focus on the code service that offers free and premium tiers. The software that hosts containers is known as Docker Engine. The docker-compose.overri
5 min read
What does Flag do in Docker Compose?
Docker Compose is a tool that is mostly used to create and execute multi-container apps. It is the secret to opening the door to a more simplified and effective deployment and development process. What is Docker Compose?Docker Compose makes it simple to manage services, networks, and volumes in a si
5 min read
Docker Compose CLI Registry
Docker Compose is a powerful tool that is used to manage multi-container docker applications. Here in this guide, I will first discuss about Docker Compose CLI Registry. Then I will discuss the steps to set up the Docker Compose CLI Registry on an Ubuntu machine. After this, I will guide you through
4 min read
How to Use Docker Compose With Jenkins
In the fast-paced world of software development, it is mainly ensured by automation of repetition and task persistence. This is where Docker Compose and Jenkins come into play. Docker Compose is a tool for defining and running multi-container Docker applications. On the other hand, Jenkins is a wide
8 min read
Docker - Continuous Integration
Continuous Integration ( CI ) with Docker improves the productivity of software development. Docker make the applications portable and independent of the system making its environment uniform. Development of the pipelines can be improved with CI technology tools like Jenkins which automates building
8 min read