Back to the Docker run command

Now, let's return to our discussion of the container run command. Earlier, you saw an example of using the run command with the --publish parameter. Using the optional publish parameter allows you to specify what ports will be opened related to the run container. The --publish parameter includes pairs of port numbers separated by a colon. For example:

# create an nginx web-server that redirects host traffic from port 8080 to port 80 in the container
docker container run --detach --name web-server1 --publish 8080:80 nginx

The first port number is associated with the host running the container. In the nginx example, 8080 is exposed on the host; in our case that would be http://localhost:8080. The second port number is the port that is open on the running container. In this case, it would be 80. Speaking out the description of the --publish 8080:80 parameter, 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. Look at the following examples to better understand:

# all of these can be running at the same time
docker container run --detach --name web-server1 --publish 80:80 nginx
docker container run --detach --name web-server2 --publish 8000:80 nginx
docker container run --detach --name web-server3 --publish 8080:80 nginx
docker container run --detach --name web-server4 --publish 8888: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

Know that this is a limitation of networking in general, not a limitation of Docker or containers. Here we can see these commands and their output. 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. If you want to learn more about the optional parameters we explored, or find out about the ones we didn't cover here, be sure to visit the docker documents page for the container run command, which can be found at https://docs.docker.com/engine/reference/run/.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
13.59.8.162