Open In App

How to Map Ports in Docker?

Improve
Improve
Like Article
Like
Save
Share
Report

In docker, all the application in the container runs on particular ports when you run a container. If you want to access the particular application with the help of a port number you need to map the port number of the container with the port number of the docker host.

Why We Use Port Mapping?

The network services of a container are made accessible to the host or external network through port mapping in Docker. Docker containers can only communicate with other containers on the same Docker network and run by default in isolation from the host and external networks. By publishing a container’s network service to the host or external network through port mapping, you can make it reachable from other networked devices.

When running a Docker container, you can map a port on the container to a port on the host or external network using the -p or —publish options. If you use the below command it will publish nginx, for instance, would publish port 80 from the container to port 8080 on the host or external network.

docker run -p 8080:80

Ways to Assign a New Port Mapping To A Running Container

There are a few ways to assign a new port mapping to a running container in Docker:

1. Stop And Restart The Container 

We can Stop the running Container with the docker command “docker stop” and again run the container with the command “docker run” While running the container again use the below command.

docker run -p <HostPort:containerport> imagename:tag 

2. Update The Running Container 

If you wish to update the port mapping of a container named my container to map port 8080 on the host to port 80 on the container using the command, for instance, you can map the container port to the host port by updating the container.

docker container update --publish-add 8080:80 my_container

3. Using the Docker API

Additionally, you can alter a running container’s port mapping using the Docker API. Sending a POST request with the new port mapping configuration in the request body to the /containers/(id)/update endpoint will do this.

Example: You can send a POST request to http://localhost/containers/(container_id)/update with a JSON payload containing the new port mapping configuration.

4. Update Config Files To Change The Container Port In The Docker

  1. By using “docker ps” list all the containers and pick the container from the list to which the container port is to be changed. 
  2. Stop the container to which you need to change the ports by using “docker stop”
  3. Find the port mapping definitions section of the configuration file. Typically, this section will have a queue like ports: – [HOST PORT]:[CONTAINER PORT]. The new port number you want to use should be entered as the value for “CONTAINER PORT”. In the configuration file, save the modifications. 
  4. Use the docker start command with the container name or ID to restart the container. By using “docker start”

Steps To Map Ports Of Docker Container With Docker Host 

In the implementation, we are going to download a Jenkins container from the docker hub and map the Jenkins container port number with the docker host port number.

Launching The Container

Step 1: Sign-up for your docker hub repository.

Sign-up in docker hub

 

Step 2: Search for the Jenkins image and use the docker pull command to download the Jenkins image on your local server.

Jenkins Image

 

Step 3: Download Jenkins’s image using the below command:

sudo docker pull jenkins

Pull the Jenkins image..

Step 4: To see the ports exposed by the Jenkins container type docker inspect command.

docker inspect Container/image

docker inspect.

Step 5: In this step, we run Jenkins and map the port by changing the docker run command by adding the p option which specifies the port mapping.

sudo docker run -p 8080:8080 50000:500000 jenkins

On the left-hand side, it is the Docker host port number, and on the right-hand side Docker container number.

Jenkins accessing from internet

 

Publishing Docker Ports Via -P or -p

1. Map The Host Ports To Specific Ports Like TCP or UDP

To try this on your system you can use Apache HTTPD docker image. 

#docker run \
-d \
-p 8080:80/tcp \
image 
#docker run \
-d \
-p 8080:80/udp 
image

-d is used to run the docker container server in detached mode.

2. Publishing Any Range Of Ports To Container

#docker run \
-d \
-p 8000-8004:4000-4004 \
image

3. Publish Random Ports

Do not specify any port in the published tag, this will publish some randomly chosen ports to all the exposed ports of the docker container application. Always remember -P is in capital letters for this random allocation.

#docker run \
-d \
-P \
image

Check The published Ports Of A Running Docker Container

1. List All The Mapped Ports Of TheContainer

First, create an HTTPD server with a docker container.

#docker run -itd --name cont_1 -p 8080:80 httpd

Now list the ports of the docker container “cont_1”.

#docker port cont_1
Output:
80/tcp -> 0.0.0.0:8080
80/tcp -> :::8080

2. List Randomly Allocated Ports

#docker run -itd --name cont_2 -P httpd
#docker port cont_2
Output:
80/tcp -> 0.0.0.0:49153
80/tcp -> :::49153

3. Check  Any Specific Port OfA Docker Container

#docker port cont_2 80/tcp
Output:
0.0.0.0:49153
:::49153
Checking Ports

List Docker Ports

Run Docker Container In Detached Mode

Most of the time you need to run the container in the detached mode or we can say in the background. By this, the application will continuously run when we are not engaged by this we can work on the remaining features of containers like networks or volumes of containers and also we can work on different multiple containers. To run the container in “detached” mode use the below command.

docker run -d -p 80:80 Nginix: latest

We can use “–detach” or “-d” in short. The Docker container will start and run in the “detach” mode and it returns you to the terminal prompt.

List containers In Docker 

How do we know whether our container is running or not and know if any other containers are running in the Docker cluster? By using the below command we can know all the containers that are running.

docker ps

Stop, Start, And Resart Containers

In real-time we need to perform some upgrades or any other modifications to the containers then we can perform upgrades while the container is running but it is not good practice to do then in that time we will stop the container and perform our patches. For stopping the container we can we below command. 

docker stop <ContainerName/container id> 

After completing our maintenance to the containers know we need to start the container again so that end users can access our application for that we can use the following command. 

docker start <ContainerName/Container ID>

In some conditions, like when we change the container port in a scenario we need to restart our containers and sometimes the container will have trouble we have performed an upgrade in the running container so in that time we need to restart the containers. By using the following command we can restart our container.

docker restart <ContainerName/Container ID>

Exposing Docker Ports Via EXPOSE or –expose

The port is not actually published by the EXPOSE command. It serves as a form of documentation regarding which ports are meant to be released between the person who produces the image and the person who runs the container. Use the -p flag on docker run to actually publish and map one or more ports when starting the container.

Note: The EXPOSE command tells Docker that the container is now listening on the given network ports.

Syntax:

EXPOSE 8080  

Differences between EXPOSE And Publish 

The meaning of “EXPOSE and PUBLISH” are both used to expose the ports of containers to the outside world. But the use cases are different.

  1. EXPOSE: This is used in Dockerfile the port on which the container will listen to incoming connections. With the help of Docker “EXPOSE” others who are using the Docker image, they will get to know which port the container will listen to it EXPOSE does not actually publish the port and does not make the port accessible from outside the container.
  2. PUBLISH: This command is used to publish or map a port on the host computer to a port on the container when a container is first started. By using the docker run command, the host and container ports are mapped using the -p or —publish flag. As a result, additional services or customers can connect to the container over the mapped port and access the container port from outside the container.

In conclusion, PUBLISH is a runtime command that translates a container port to a host port, allowing external access to the container service over the specified port. EXPOSE is a statement in the Dockerfile that lists the ports on which the container listens.



Last Updated : 06 May, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads