When creating a container using the docker run command, the -p or --publish option can be used to declare the ports that will be used between the Docker server and the container, which adds a rule to iptables to allow the port.
In this example, a container is created using the foo:latest image, and the Docker server will listen on port 8080 and the container will listen on port 1337.
docker run --publish 8080:1337 foo:latest
If you do you include the IPv4 or IPv6 address, and you only declare the ports, the container will listen on both IPv4 and IPv6, which can be seen with the docker ls command.
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS
d937372c09ab9 b939aa938add9913 "/docker-entrypoin..." 6 minutes ago Created 0.0.0.0:9000->9000/tcp, ::8080->1337/tcp my-container
By simply include 0.0.0.0 or an the the IP address or hostname on the Docker system, the container will only listed on IPv4.
docker run --publish 0.0.0.0:8080:1337 foo:latest
Which can be seen with the docker ls command.
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS
d937372c09ab9 b939aa938add9913 "/docker-entrypoin..." 6 minutes ago Created 0.0.0.0:9000->9000/tcp my-container
Did you find this article helpful?
If so, consider buying me a coffee over at