Docker : How to find the network my container is in?

NetworkingDocker

Networking Problem Overview


I am trying to understand few things about Docker:

  1. How can I find the network my container is in ?

  2. Can I dynamically detach my container and attach on an other network ? How?

  3. If I have two containers running, how to check if the two are in the same network ? Can I ping one from other ?

Networking Solutions


Solution 1 - Networking

To see what network(s) your container is on, assuming your container is called c1:

$ docker inspect c1 -f "{{json .NetworkSettings.Networks }}"

To disconnect your container from the first network (assuming your first network is called test-net):

$ docker network disconnect test-net c1

Then to reconnect it to another network (assuming it's called test-net-2):

$ docker network connect test-net-2 c1

To check if two containers (or more) are on a network together:

$ docker network inspect test-net -f "{{json .Containers }}"

Solution 2 - Networking

  1. The network is visible in the docker container inspect $id output, where $id is the container id or container name. The name is listed under the NetworkSettings -> Networks section.

  2. You can use docker network connect $network_name $container_name to add a network to a container. And similarly, docker network disconnect $network_name $container_name will disconnect a container from a docker network.

  3. Containers can ping each other by IP address if they are on the same docker network and you have not disabled ICC. If you are not on the default network named "bridge" you can use the included DNS discovery to ping and connect to containers by container name or network alias. Any new network you have created with docker network create $network_name has the DNS discovery turned on, even if it's using the bridge driver, it just needs to be separate from the one named "bridge". Containers can also connect over TCP ports, even without exposing or publishing ports in docker, as long as they are on the same docker network.

Here's a low level example of testing a network connection with netcat:

$ docker network create test-net

$ docker run --net test-net --name nc-server -d nicolaka/netshoot nc -vl 8080
17df24cf91d1cb785cfd0ecbe0282a67adbfe725af9a1169f0650a022899d816

$ docker run --net test-net --name nc-client -it --rm nicolaka/netshoot nc -vz nc-server 8080
Connection to nc-server 8080 port [tcp/http-alt] succeeded!

$ docker logs nc-server
Listening on [0.0.0.0] (family 0, port 8080)
Connection from nc-client.test-net 37144 received!

$ docker rm nc-server
nc-server

$ docker network rm test-net

Solution 3 - Networking

I required a quick test to validate what containers were connected to my custom bridge to troubleshoot a connectivity issue between containers. The below test can answer both the 1st & 3rd parts of the OP's question:

docker network inspect <tab complete to show avail. bridges> | grep IPv4Address

Specimen Output:

docker network inspect <tab completion results below>
   bridge   docker-compose_zabbix_zbx_net_backend   host                                       
   docker-compose_zabbix_default  docker-compose_zabbix_zbx_net_frontend  none                                       

ubuntu@myDockerHost:~$ docker network inspect docker-compose_zabbix_zbx_net_backend | grep IPv4Address
            "IPv4Address": "172.16.239.6/24",
            "IPv4Address": "172.16.239.7/24",
            "IPv4Address": "172.16.239.4/24",
            "IPv4Address": "172.16.239.5/24",
            "IPv4Address": "172.16.239.8/24",
            "IPv4Address": "172.16.239.3/24",
            "IPv4Address": "172.16.239.2/24",

or by containerName:

docker network inspect <tab complete to show avail. bridges> | grep Name

Specimen Output:

docker network inspect docker-compose_zabbix_zbx_net_backend | grep Name
    "Name": "docker-compose_zabbix_zbx_net_backend",
            "Name": "docker-compose_zabbix-zabbix-agent",
            "Name": "docker-compose_zabbix-zabbix-server",
            "Name": "docker-compose_zabbix-zabbix-web-service",
            "Name": "docker-compose_zabbix-postgres-server",
            "Name": "docker-compose_zabbix-zabbix-web-nginx-pgsql",
            "Name": "docker-compose_zabbix-zabbix-java-gateway",
            "Name": "docker-compose_zabbix-zabbix-snmptraps",

The results will reveal all the containers joined to the specified bridge by either IP or Name.

NOTE: Do not supply the random bridge names assigned to bridges in the output of ip addr list for the value of the bridge name in the above commands. If you do, the error Error: No such network: br-abtfta2nb624 will be puked.

As for the 2nd part of the OP's question, I refer the reader to @johnharris85 's excellent answer.

Solution 4 - Networking

$ docker inspect my-container-name

Just under the "Network"section you will find the network.

"Networks": {
            "host": {   -------> *this is your network*
                "IPAMConfig": null,
                "Links": null,
                "Aliases": null, 
                "NetworkID":"5de628a521355837083c987789c7193f42507e26fe524a",
                "EndpointID": "",

Solution 5 - Networking

Old question.. but you are probably trying to make sure a cluster of containers are running the right network. There are two easy approaches. Your intuition may want something like:

docker ps --format '{{ .ID }} {{ .Names }} {{ json .Networks }}'

but you might really want:

docker network inspect networkname

which includes a list of all the containers on that network.

Solution 6 - Networking

I Was get this error from the Jenkins machine:

> Error: docker: Error response from daemon: network isolated_docker not > found.

and then I realized the network isolated_docker doesn't exist, then I created using the command:

docker network create isolated_docker

you can check your networks inside the machine with the command:

docker network ls

Solution 7 - Networking

$ docker exec -it {container_name_1} ifconfig
eth0      Link encap:Ethernet  HWaddr 02:42:AC:13:00:03  
          inet addr:172.19.0.3  Bcast:172.19.255.255  Mask:255.255.0.0
$ docker exec -it {container_name_2} ifconfig
eth0      Link encap:Ethernet  HWaddr 02:42:AC:12:00:02  
          inet addr:172.18.0.2  Bcast:172.18.255.255  Mask:255.255.0.0

Here in my case, it means that {container_name_1} and {container_name_2} are not on the same networks. (172.18 and 172.19 are not the same). To make them operate on the same network, on way is to use docker-compose. Follow this l

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestiontheCodeView Question on Stackoverflow
Solution 1 - Networkingjohnharris85View Answer on Stackoverflow
Solution 2 - NetworkingBMitchView Answer on Stackoverflow
Solution 3 - NetworkingF1LinuxView Answer on Stackoverflow
Solution 4 - NetworkingAndrea MoriconiView Answer on Stackoverflow
Solution 5 - NetworkingJason KaneView Answer on Stackoverflow
Solution 6 - NetworkingRegis LimaView Answer on Stackoverflow
Solution 7 - NetworkingAmiay NarayanView Answer on Stackoverflow