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.
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.
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.
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:
Once executed successfully, you have to list the images again by utilizing this command.
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.
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.
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.