Find Docker Which Network is Container Using

Estimated reading time: 5 minutes

Last updated on November 8th, 2024 at 04:52 pm

The Docker network provides essential networking for managing the communication between containers, containers to the Docker host, and outside internet access. Knowing which networks a container uses is crucial for troubleshooting and network management.

Let’s find Docker which network is container using with the help of the Docker CLI and Docker compose.

How to Find Docker Which Network is Container Using?

Docker provides out-of-the-box support for various network drivers such as:

  1. Bridge
  2. Host
  3. Overlay
  4. IPvLAN
  5. macvlan

If you want to know more about the Docker network read the the difference between Docker Network Overlay vs Bridge

Find Docker Which Network is Container Using: Docker CLI

#1. Run a Container:
Bash
docker run -d --name nginx nginx:latest

The above command will run a container named nginx with the NGINX web server in detached mode.

#2. List Available Networks:
Bash
dockeer network ls

This command will display all the available networks. Let’s check the output.

Find Docker Which Network is Container Using
#3. Inspect Container Network Details:
Bash
# Inspect Container Filter Network
docker inspect nginx -f "{{json .NetworkSettings.Networks }}" | jq

This will display which network container is using.

JSON
{  
  "bridge": {
    "IPAMConfig": null,
    "Links": null,
    "Aliases": null,
    "NetworkID": "58d0ed1a37b1c71b01943ac8a853c3a3e61d85af00b971130a585f77f5ec1a77",
    "EndpointID": "c71e269e1c56a9f198792018c487183d4e248100f757735727b4d37929157ff3",
    "Gateway": "172.17.0.1",
    "IPAddress": "172.17.0.2",
    "IPPrefixLen": 16,
    "IPv6Gateway": "",
    "GlobalIPv6Address": "",
    "GlobalIPv6PrefixLen": 0,
    "MacAddress": "02:42:ac:11:00:02",
    "DriverOpts": null
  }
}  

When you the container and don’t specify anything, it will run in bridge mode by default.

#4. Inspect the Network:
Bash
# Inspect Network
docker network inspect bridge

You can directly inspect the specific network to find which containers are connecting to that network

Bash
{
  "Containers": {
    "58d0ed1a37b1c71b01943ac8a853c3a3e61d85af00b971130a585f77f5ec1a77": {
      "Name": "bridge",
      "EndpointID": "c71e269e1c56a9f198792018c487183d4e248100f757735727b4d37929157ff3",
      "MacAddress": "02:42:ac:11:00:02",
      "IPv4Address": "172.17.0.2/16",
      "IPv6Address": ""
    }
  }
}

Here you can see the Docker container ID starting with 85e... us connected to the bridge network and detailed information about the IP and MAC address.

Find Docker Which Network is Container Using: Docker Compose

#1. Run the Docker Compose with the Nginx:
YAML
version: '3.8'

services:
  nginx:
    image: nginx:latest
    ports:
      - 80:80
    networks:
      - devtodevops
  
  database:
    image: mysql:latest
    environment:
      MYSQL_ROOT_PASSWORD: my_password
      MYSQL_DATABASE: my_database
    networks:
      - devtodevops
      
networks:
  devtodevops:
    name: devtodevops

In the above example:

  • The nginx service uses the NGINX image and exposes the port 80 on the host.
  • The database service uses the MYSQL image.
  • Both services are connected to the same bridge network name devtodevops.
  • The devtodevops network is bridge the network is defined under the networks sections

Now, let’s deploy this setup with the Docker Compose:

Bash
$ docker compose up -d

[+] Building 0.0s (0/0)                                               docker:default
[+] Running 3/3

 Network     devtodevops              Created                                 0.1s
 Container   devtodevops-nginx-1      Started                                 0.1s
 Container   devtodevops-database-1   Started                                 0.1s

Let’s check the running containers managed by Docker Compose:

Bash
docker compose ps
#2. Inspect Network Configuration:

Let’s inspect the Docker network configuration:

Bash
# Inspect Network
docker network inspect devtodevops

You can find both in the container in the below configuration

JSON
{
   "Containers":{
      "e0f6605c5c03268458a264ca120e3f11768c4446e1edefbdc4cce8fc97992414":{
         "Name":"devtodevops-database-1",
         "EndpointID":"f3d262b69f2f6482bb53caea27f2da5309897526f5f86b57f845de8b9b4a3746",
         "MacAddress":"02:42:ac:16:00:03",
         "IPv4Address":"172.22.0.3/16",
         "IPv6Address":""
      },
      "f7ee37978681b82f780e3aeb73f59b9bbb6292cd6b2c95e808de1bab7a4264db":{
         "Name":"devtodevops-nginx-1",
         "EndpointID":"154e9dc9f3fd9096736e48e0be0a2a7c75fd662109a4d1d8714bf77baf4cbdc8",
         "MacAddress":"02:42:ac:16:00:02",
         "IPv4Address":"172.22.0.2/16",
         "IPv6Address":""
      }
   }
}

Conclusion

Understanding how to find Docker which network is container using is essential for container management and troubleshooting. You can utilize the Docker CLI and Docker Compose commands to manage the networking.

A container network allows for isolation and network segmentation. With the help of the Docker Network commands you can administrators and troubleshoot connectivity issues.

Check out more about the Docker network and read the difference between Docker Network Overlay vs Bridge

If you’re looking for how to make your Docker container secure, check out the Docker Container Security Cheatsheet and don’t get hacked🔐

Kashyap Merai

Kashyap Merai

Kashyap Merai, a Certified Solution Architect and Public Cloud Specialist with over 7 years in IT. He helped startups in Real Estate, Media Streaming, and On-Demand industries launch successful public cloud projects.

Passionate about Space, Science, and Computers, He also mentors aspiring cloud engineers, shaping the industry's future.

Connect with him on LinkedIn to stay updated on cloud innovations.