DevOpsDockerDockersHow to

How to use Docker run command

Using the optional publish parameter allows you to specify what ports will be opened related to the run container.
The –publish or -p parameter includes pairs of port numbers separated by a colon. For example:

# create an NGINX Web server that redirects host traffic from port 8181 to
port 80 in the container
docker container run --detach --name web-nginx1 --publish 8181:80 nginx
# orĀ 
docker container run --detach --name web-nginx1 --p 8181:80 nginx

The first port number is associated with the host running the container for example 8181 is exposed on the Host that running the Docker engine, in our case that would be: or http://localhost:8181.
The second port number is the port that is open on the running In this case, it would be 80. “web-nginx1 –publish 8181:80 nginx” or “web-nginx1 –publish 8181:80 nginx” parameters you would say something like, the traffic sent to port 8080 on the host is redirected to port 80 on the running container:

It is an important distinction to make between the host ports and the container ports.
I can run several containers on the same system that all expose port 80, but only one container can have traffic from each port on the host.
examples :

# all of these can be running at the same time on the same host
docker container run --detach --name web-server1 --publish 80:80 nginx
docker container run --detach --name web-server2 --publish 8080:80 nginx
docker container run --detach --name web-server3 --publish 8181:80 nginx
docker container run --detach --name web-server4 --publish 8282:80 nginx
# however if you tried to run this one too, it would fail to run
# because the host already has port 80 assigned to web-server1
docker container run --detach --name web-server5 --publish 80:80 nginx

Please note: That this is a networking limitation not a limitation of Docker or containers.
You can do that I mean to assign many docker with the same port like upper example: docker container run –detach –name web-server5 –publish 80:80 nginx, just if you assign many IP address to same Docker host and then assign dedicated IP address for each running container.

Notice the ports and names, and how the use of a port already used as an endpoint fails:

That was a lot of data on various options parameters for the container run command. It’s not all the options parameters, but it should be enough to get you off to a great start.

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *


Please type the text above: