Reputation: 2347
I am trying to understand few things about Docker:
Upvotes: 151
Views: 258275
Reputation: 1207
2024 answer
First list out your available docker networks:
docker network ls
E.g.
docker network ls
NETWORK ID NAME DRIVER SCOPE
362awwd28a8f6 bridge bridge local
0aawdawd4e07c host host local
5b0awd2adba73 none null local
90a6awdwdwa6c titan-utilities_my_network bridge local
In my case, the containers I made created their own isolated network (that's best practice as you gain network isolation. You don't want your containers able to use your host's networking adapters or see what else is there, that kind of defeats the purpose of docker)
In my case the containers are in: utilities_my_network
Now you can inspect it:
docker network inspect titan-utilities_my_network
Towards the bottom of the output I can see: .
...
"Containers": {
"58b8791d8c8fec279aaaaaaaaaaaaaaaaaaaaaaaaafc377c3a49be20e7a360b96aef": {
"Name": "te-utilities-mosquitto-1",
"EndpointID": "zzzzzzzzz____redacted",
"MacAddress": "02:ff:ff:ff:ff:02",
"IPv4Address": "172.18.0.2/16",
"IPv6Address": ""
},
...
Hope that helps!
Upvotes: 6
Reputation: 263469
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. That can be output with a format string:
docker container inspect \
--format '{{range $net,$v := .NetworkSettings.Networks}}{{printf "%s\n" $net}}{{end}}' \
$container_name
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.
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
Upvotes: 60
Reputation: 4373
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.
Upvotes: 3
Reputation: 590
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.
Upvotes: 30
Reputation: 11
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
Upvotes: 1
Reputation: 31
$ 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": "",
Upvotes: 3
Reputation: 529
$ 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
Upvotes: -2
Reputation: 18916
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 }}"
Upvotes: 211