Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does it mean that `docker run --network=container:CONTAINERID`?

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?

like image 947
flyer Avatar asked Jun 18 '17 10:06

flyer


People also ask

What is Docker container network?

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.

Which network will the container be attached to when running it with Docker run?

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.


1 Answers

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
like image 195
johnharris85 Avatar answered Oct 03 '22 21:10

johnharris85