Docker
Docker Interview Questions
1. How to debug docker a container?
Section titled “1. How to debug docker a container?”Let’s consider both the scenarios when the container is
Running
-
docker logs -f --tail 100 <container_name_or_id> -
Jump inside with a live shell: If the container has a shell (like bash or sh), you can access it interactively to explore files, environment variables, or check running processes (top, ps).
docker exec -it <container_name_or_id> /bin/bash -
Check stats and system metrics: View real-time resource utilization (CPU, memory, network I/O).
docker stats <container_name_or_id>
Not running(Slim or distroless containers)
docker debug <container_name_or_id>docker run -it --entrypoint /bin/sh <image_name>
2. How to check network issues?
Section titled “2. How to check network issues?”When troubleshooting Docker network issues, the problem typically breaks down into one of four areas:
- Port Mappings
- Container-Container connectivity
- DNS Resolution
- External/Host communication.
Debugging steps
-
Check the container’s IP and Network allocation: Find out exactly what network the container is on and its internal IP address:
docker inspect -f '{{json .NetworkSettings.Networks}}' <container_name> -
Verify your mapped ports: Make sure traffic is actually routing from the host port to the internal container port:
docker port <container_name>If it returns
80/tcp -> 0.0.0.0:8080, your host is listening on8080. If it returns nothing, your container isn’t publishing ports. -
Check for host port conflicts: If your container fails to bind to a port, check what host process is stealing it
sudo lsof -i :<port_number>
Test Container-to-Container Connectivity
-
List existing networks
docker network ls -
Inspect the network to see attached containers
docker network inspect <network_name>.
Look at the “Containers”: block. If your two target containers aren’t listed in the exact same network block, they are isolated from one another. -
Fix it by joining them to the same custom network:
Terminal window # Create a custom bridge networkdocker network create my_app_net# Connect both containersdocker network connect my_app_net <container_1>docker network connect my_app_net <container_2>
Jump Inside for In-Container Network Tests
To isolate why an application cannot communicate, fire up an interactive shell inside the container:
docker exec -it <container_name> /bin/sh
Once inside, run these diagnostic tests:
-
Is the application actually listening internally? Ensure your app binds to
0.0.0.0(all interfaces) and not rigidly to127.0.0.1(localhost internal to itself):netstat -tlnp # or use 'ss -tlnp' -
Test Internal DNS: See if the container can parse domain names:
Terminal window cat /etc/resolv.conf# Test lookupnslookup google.com # or 'ping -c 3 google.com' -
Can it talk to the default Gateway? Find the bridge’s IP route and ping it to check if your packets are reaching the Docker engine engine
Terminal window ip route | awk '/default/ {print $3}'ping <gateway_ip>