I know that when running a container, I could set the --network
argument whose value could be any from the results of docker network ls
.
However, I have seen that some run containers like this:
$ docker run --network=container:CONTAINERID IMAGE
I have searched this usage but got no docs to explain it.
I have done some experiments and find that the container using another container's network shares the same network stack and it seems that the two containers are on the same host
and they could call each other using localhost
.
So when running a container by setting --network=container:CONTAINERID
, does it mean that the two containers share the same network stack?
Docker networking allows you to attach a container to as many networks as you like. You can also attach an already running container. Go ahead and attach your running web app to the my_bridge . $ docker network connect my_bridge web. Open a shell into the db application again and try the ping command.
When you start a container use the --network flag to connect it to a network. This adds the busybox container to the my-net network. You can also choose the IP addresses for the container with --ip and --ip6 flags when you start the container on a user-defined network.
Exactly what you thought, the new container is given the same network namespace as CONTAINERID
. So yes, same network stack. As you identified, this means that containers can contact each other via localhost, it also means that you need to be careful with port mappings, as each container will need a unique port within the namespace.
It is documented in the docker run
reference here.
--network="bridge" : Connect a container to a network
'bridge': create a network stack on the default
Docker bridge
'none': no networking
# -----> 'container:<name|id>': reuse another container's
network stack
'host': use the Docker host network stack
'<network-name>|<network-id>': connect to a
user-defined network
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With