0
0
Dockerdevops~5 mins

Inspecting container network settings in Docker - Commands & Configuration

Choose your learning style9 modes available
Introduction
Sometimes you need to see how your Docker containers connect to networks. This helps you understand IP addresses, ports, and network modes used by containers.
When you want to find the IP address assigned to a running container to connect to it.
When you need to check which network a container is connected to for troubleshooting.
When you want to verify port mappings between the host and the container.
When you want to confirm if a container is using the default bridge network or a custom network.
When you want to inspect network settings before connecting containers together.
Commands
This command lists all Docker networks on your system so you can see available networks.
Terminal
docker network ls
Expected OutputExpected
NETWORK ID NAME DRIVER SCOPE b1f3a2c4d5e6 bridge bridge local c7d8e9f0a1b2 host host local f3e4d5c6b7a8 none null local
This shows all running containers so you can find the container ID or name to inspect.
Terminal
docker ps
Expected OutputExpected
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 7a8b9c0d1e2f nginx:1.23 "/docker-entrypoint.…" 2 minutes ago Up 2 minutes 0.0.0.0:8080->80/tcp my-nginx
This command shows detailed network settings of the container named 'my-nginx' in JSON format for easy reading.
Terminal
docker inspect my-nginx --format '{{json .NetworkSettings}}'
Expected OutputExpected
{"Bridge":"bridge","SandboxID":"a1b2c3d4e5f6g7h8i9j0","HairpinMode":false,"LinkLocalIPv6Address":"","LinkLocalIPv6PrefixLen":0,"Ports":{"80/tcp":[{"HostIp":"0.0.0.0","HostPort":"8080"}]},"SandboxKey":"/var/run/docker/netns/a1b2c3d4e5f6","SecondaryIPAddresses":null,"SecondaryIPv6Addresses":null,"EndpointID":"1234567890abcdef","Gateway":"172.17.0.1","GlobalIPv6Address":"","GlobalIPv6PrefixLen":0,"IPAddress":"172.17.0.2","IPPrefixLen":16,"IPv6Gateway":"","MacAddress":"02:42:ac:11:00:02","Networks":{"bridge":{"IPAMConfig":null,"Links":null,"Aliases":null,"NetworkID":"b1f3a2c4d5e6","EndpointID":"1234567890abcdef","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}}}
--format - Formats the output to show only network settings in JSON for clarity
This command shows details about the 'bridge' network including connected containers and their IP addresses.
Terminal
docker network inspect bridge
Expected OutputExpected
[ { "Name": "bridge", "Id": "b1f3a2c4d5e6", "Created": "2024-06-01T12:00:00.000000000Z", "Scope": "local", "Driver": "bridge", "EnableIPv6": false, "IPAM": { "Driver": "default", "Options": null, "Config": [ { "Subnet": "172.17.0.0/16", "Gateway": "172.17.0.1" } ] }, "Containers": { "7a8b9c0d1e2f": { "Name": "my-nginx", "EndpointID": "1234567890abcdef", "MacAddress": "02:42:ac:11:00:02", "IPv4Address": "172.17.0.2/16", "IPv6Address": "" } }, "Options": {}, "Labels": {} } ]
Key Concept

If you remember nothing else from this pattern, remember: docker inspect lets you see exactly how a container connects to networks including IPs and ports.

Common Mistakes
Using 'docker inspect' without specifying the container name or ID.
The command will fail or show too much unrelated information making it hard to find network details.
Always specify the container name or ID after 'docker inspect' to focus on the container you want.
Not using the --format flag to filter output.
The full inspect output is very long and hard to read, especially for beginners.
Use --format '{{json .NetworkSettings}}' to get clear, focused network info.
Summary
Use 'docker network ls' to list all Docker networks on your system.
Use 'docker ps' to find running containers and their names or IDs.
Use 'docker inspect <container>' with --format to see detailed network settings like IP address and port mappings.
Use 'docker network inspect <network>' to see which containers are connected and their network details.