How to Map a Host Port to a Container Port?

The Docker open-source platform has completely changed how we build, deploy, and handle containerized apps. You will need to construct a Dockerfile to containerize an application, which contains the instructions Docker needs to generate and run images. To allow seamless communication among containers in multi-container applications and making your Docker ports available by services with the outside world, you may have to set certain networking rules.

When you start a container with Docker, all of the applications in the container run on specific ports. If you wish to use a port number to access a specific application, you must first map the container’s port number to the docker host’s port number. Containers can use port mappings to send and receive traffic through ports on the host container instance. The container’s port mappings are defined as part of the container definition. The port number on the container that is connected to the host port was either chosen by the user or allocated automatically.

Containers in Docker can have their applications running on ports. When running a container, you must map the container’s port number to the Docker host’s port number if you would like to connect the container’s application via a port number. In this tutorial, we will explain you about mapping a host port to a container port in docker.


To map a host port to a container port, you have to install Ubuntu 20.04 operating system. Also, make sure that the docker shall be installed on it. Otherwise, this whole tutorial will not be constructive for you. If you have not currently installed docker, you can do it by simply executing the below-written command in the command line terminal.

$ sudo apt install

We have started the command with the “Sudo” keyword that means you must have root user access rights for installation purposes.

Method to Map a Host Port to a Container Port in Docker

To map a host port to a container port in docker, initially, you have to open the command line shell in your operating system. You can open it by searching in the application menu by typing the keyword “terminal” in the search bar or by utilizing the “Ctrl+Alt+T” shortcut key. Once it is opened, you have to write the below-listed command to run the image named ‘nginx’ with the –P flag.

$ sudo docker run –itd –P nginx

As we have used “sudo” keyword at the start of the command, the system will inquire for the password. After providing the password, the required image will be created. Now, it is the time to display the images of docker. Use the appended command below to complete your task.

$ sudo docker ps

As displayed in the image attached above, you can see the container id, image, command, its creation time, and status along with the port number. We have highlighted the port number of ‘nginx’ that is: 49153->80.

Now, we will run the command that is mentioned below:

$ sudo docker run –itd –P nginx

Once executed successfully, you have to list the images again by utilizing this command.

$ sudo docker ps

As highlighted in the image, you can check that the value has been incremented by 1 which is 49154->80. Now, you should check the image with the Docker inspect command to see what ports the container exposes. You have to execute the following appended command in the terminal window.

$ sudo docker inspect nginx

The inspect command returns a JSON object as a result. We can see a chunk of “ExposedPorts” in the output if we look closely at the attached image displayed above. You must update the Docker run command to include the ‘-p’ option, which indicates the port mapping, to execute nginx and map the ports. As a result, you must implement the following appended command.

$ sudo docker run -p 80:80 -p 50:50 nginx

The Docker host port to map to is mostly on the left-hand side of the port number mapping, while the Docker container port number would be on the right-hand side. You will notice nginx up and operating when you launch the browser and go to the Docker host on port 80.


In this guide, we have learned about mapping a host port to a container port in docker. I believe now you will be able to easily map a host port to a container port in docker by looking into this tutorial.

About the author

Kalsoom Bibi

Hello, I am a freelance writer and usually write for Linux and other technology related content