Ports

If you need to access internet services since your container will run a web application, the situation is comparable to volumes.

The syntax is ‐‐publish/-p host:container

In this example, we are using the docker image nginx. There is a lot of information about this image of the popular web server at https://hub.docker.com/_/nginx.

docker run --detach --name webserver nginx
curl localhost:80
docker exec webserver curl localhost:80
docker rm -f webserver

By the way: what is the difference between the second and the third command of the above code block. Have look on your own to find out.
In the next example, we specify the port the web server has to run on explicitly via –publish. The first port is the one used on the host and the second the one used in the container.

docker run --detach --name webserver --publish 80:80 nginx
curl localhost:80
docker rm -f webserver

Try this short code example on your own and explain what the difference is of the commands on lines 2 to 5.

docker run --detach --name webserver -p 8080:80 nginx
curl localhost:80
curl localhost:8080
docker exec webserver curl localhost:80
docker exec webserver curl localhost:8080
docker rm -f webserver