Docker

What is Port Mapping in Docker?

Docker is a well-known and open-source platform that is frequently used to create, share and run software and applications. It uses different components to execute the projects inside the containers such as Docker images, Docker engine or daemon, and Docker client. However, to access the container’s inner process, port mapping is required.

This post will discuss:

What is Docker Port Mapping?

While executing the services or applications inside the container, users usually want to access the app or services to the outside world rather than inside the container. To access the container’s processes or services from outside, the port mapping process is used in Docker. Port mapping is a process that creates the firewall rule which maps the container’s open port to the open port of the Docker host through which the executing applications or services are accessible from the host system.

How to Map a Port in Docker?

To map a port in Docker, two options are used to publish the port on the host; “–publish” or “-p”. To map a port in Docker, follow the provided instructions.

Step 1: Make Dockerfile

First, create the Dockerfile that will execute the simple HTML program “index.html”. In the below code:

  • FROM” key is used to define the container’s base image.
  • COPY” statement is utilized to copy the “index.html” file to the container’s path.
  • ENTRYPOINT” are describing the executables of Docker containers:
FROM nginx:latest
COPY index1.html /usr/share/nginx/html/index.html
ENTRYPOINT ["nginx", "-g", "daemon off;"]

Step 2: Create Image in Docker

Next, create the new image using mentioned command:

docker build -t html .

Step 3: Create and Map the Container on Host

Next, create and map the container on host through the “docker run” command. Here, the “-p” option is actually utilized to map the container on localhost port “80”:

docker run -p 80:80 --name html-cont html

Next, list down all the containers and verify if the port is mapped or not:

docker ps -a

Step 4: Verification

Next, verify if the “index.html” program which is executing inside the container is accessible on the host or not. For this purpose, navigate to the “http://localhost:80” URL in the browser:

How to Map Port in Docker Compose?

To map the port in Docker Compose to access the compose services from outside the Docker container, follow the listed steps.

Step 1: Make “docker-compose.yml” File

First, create the “docker-compose.yml” file and paste the following instructions:

  • services” key defines the composing service. For instance, the “web” service is created:
  • build” key is used to access the build context. In the code below, the “.” mean we are using the Dockerfile that is placed in the currently opened directory.
  • ports” key is specifically used to map the port. It defines the port where the container will expose:
version: "3"
services:
 web:
  build: .
  ports:
  - 80:80

Step 2: Execute Compose Service

Next, execute the “docker-compose up” command to fire up the services on the mapping port and inside the container:

docker-compose up -d

From the output, you can see that we have accessed the “web” services from outside the container on the host:

That’s all about port mapping in Docker.

Conclusion

Port mapping is a process of creating the firewall rule which maps the container’s port to the open port of the Docker host through which the executing applications or services are accessible from the host system. To map the port in Docker, the “-p” or “–publish” option is used in the “docker run” command. In the compose file, the “ports” key is used to map the port. This post has demonstrated what port mapping is in Docker and how to map the port.

About the author

Rafia Zafar

I am graduated in computer science. I am a junior technical author here and passionate about Programming and learning new technologies. I have worked in JAVA, HTML 5, CSS3, Bootstrap, and PHP.