📖 Scenario: You are working with Docker containers and want to understand how they connect to networks. This helps you see which IP addresses and ports your containers use, just like checking the address and phone number of a house.
🎯 Goal: You will create a Docker container, check its network settings, and display the IP address assigned to it.
📋 What You'll Learn
Create a Docker container running the nginx image with the name mynginx
Use the docker inspect command to get network details of the container
Extract the IP address from the network settings
Print the IP address to the terminal
💡 Why This Matters
🌍 Real World
Knowing container network settings helps when you want to connect containers to each other or to your computer, like knowing the address to send mail or call a friend.
💼 Career
DevOps engineers often inspect container networks to troubleshoot connectivity issues and configure services correctly.
Progress0 / 4 steps
1
Create a Docker container named mynginx
Run the command docker run -d --name mynginx nginx to create and start a container named mynginx using the nginx image.
Docker
Hint
This command downloads the nginx image if needed and runs it in the background with the name mynginx.
2
Inspect the container mynginx to get network details
Run the command docker inspect mynginx to see detailed information about the container, including its network settings.
Docker
Hint
This command shows a lot of details in JSON format about the container.
3
Extract the IP address from the container's network settings
Run the command docker inspect -f '{{ .NetworkSettings.Networks.bridge.IPAddress }}' mynginx to get only the IP address of the container.
Docker
Hint
The -f option formats the output to show only the IP address.
4
Display the IP address of the container
Run the command docker inspect -f '{{ .NetworkSettings.Networks.bridge.IPAddress }}' mynginx and observe the output which shows the container's IP address.
Docker
Hint
The output should be an IP address starting with 172. or similar, showing the container's network address.
Practice
(1/5)
1. Which Docker command shows detailed information about a specific network, including connected containers?
easy
A. docker ps
B. docker container inspect <container_id>
C. docker network inspect <network_name>
D. docker network ls
Solution
Step 1: Understand the purpose of docker network inspect
This command provides detailed info about a Docker network, including which containers are connected.
Step 2: Compare with other commands
docker container inspect shows container details, docker ps lists running containers, and docker network ls lists networks without details.
Final Answer:
docker network inspect <network_name> -> Option C
Quick Check:
Network details = docker network inspect [OK]
Hint: Use docker network inspect for network details [OK]
Common Mistakes:
Confusing container inspect with network inspect
Using docker ps to get network info
Using docker network ls for detailed info
2. Which of the following commands correctly shows the IP address of a running container named webapp using docker container inspect with formatting?
easy
A. docker container inspect --format '{{.NetworkSettings.IPAddress}}' webapp
B. docker container inspect --format '{{.Network.IP}}' webapp
C. docker inspect --format '{{.NetworkSettings.IP}}' webapp
D. docker container inspect --format '{{.IPAddress}}' webapp
Solution
Step 1: Identify correct JSON path for IP address
The IP address is under .NetworkSettings.IPAddress in container inspect output.
Step 2: Check command syntax
docker container inspect --format '{{.NetworkSettings.IPAddress}}' webapp uses correct command and format string. Others use incorrect paths or omit 'container'.
Final Answer:
docker container inspect --format '{{.NetworkSettings.IPAddress}}' webapp -> Option A
Quick Check:
IP address path = .NetworkSettings.IPAddress [OK]
Hint: Use .NetworkSettings.IPAddress to get container IP [OK]
Common Mistakes:
Using wrong JSON path like .Network.IP
Omitting 'container' in the command
Using '{{.IPAddress}}' which is invalid
3. Given a container named db connected to a network backend_net, what will the command docker network inspect backend_net show about the container?
medium
A. It lists the container ID and its IPv4 address inside the network.
B. It shows only the container name without IP details.
C. It shows the container's CPU and memory usage.
D. It shows the container's logs.
Solution
Step 1: Understand output of docker network inspect
This command shows network details including connected containers with their IDs and IP addresses.
Step 2: Eliminate incorrect options
It does not show CPU/memory usage or logs, and it includes IP info, not just names.
Final Answer:
It lists the container ID and its IPv4 address inside the network. -> Option A
Quick Check:
Network inspect shows container IDs and IPs [OK]
Hint: Network inspect shows container IDs and IPs connected [OK]
Common Mistakes:
Expecting resource usage info from network inspect
Thinking it shows logs
Assuming only container names appear
4. You run docker container inspect --format '{{.NetworkSettings.IPAddress}}' myapp but get an empty output. What is the most likely reason?
medium
A. The container myapp is not connected to the default bridge network.
B. Docker daemon is not running.
C. The command syntax is incorrect.
D. The container myapp is stopped.
Solution
Step 1: Understand when IPAddress is empty
If a container is stopped, it has no active network interface, so IPAddress is empty.
Step 2: Check other options
Not connected to default network still shows IP if connected elsewhere; syntax is correct; daemon not running causes errors, not empty output.
Final Answer:
The container myapp is stopped. -> Option D
Quick Check:
Stopped container IP is empty [OK]
Hint: Empty IP means container likely stopped [OK]
Common Mistakes:
Assuming wrong syntax causes empty output
Thinking network choice causes empty IP
Ignoring container running state
5. You want to list all containers connected to a custom network mynet and show their names and IP addresses in a simple table. Which approach is best?
hard
A. Run docker ps and guess which containers belong to mynet.
B. Use docker network inspect mynet and parse the JSON to extract container names and IPs.
C. Use docker container inspect --format '{{.Name}} {{.NetworkSettings.IPAddress}}' on all containers and filter manually.
D. Restart Docker daemon to refresh network info.
Solution
Step 1: Identify command that shows network container info
docker network inspect mynet lists all containers connected with their IPs.
Step 2: Understand why other options are less effective
docker ps doesn't show network info; inspecting all containers is manual and error-prone; restarting daemon is unrelated.
Final Answer:
Use docker network inspect mynet and parse the JSON to extract container names and IPs. -> Option B
Quick Check:
Network inspect shows connected containers best [OK]
Hint: Use docker network inspect to get connected containers easily [OK]
Common Mistakes:
Using docker ps to find network info
Inspecting containers individually without filtering
Restarting daemon unnecessarily
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.