Docker Cheatsheet
Networking
Use this Docker reference while you build software engineering projects, review code for technical interview prep, or polish examples for a software engineer resume.
Network Drivers
| Driver | When to use |
|---|---|
bridge | Default; isolated network on a single host |
host | Container shares host network stack (Linux only) |
none | No networking |
overlay | Multi-host (Swarm / Kubernetes) |
macvlan | Container gets its own MAC/IP on physical network |
ipvlan | Like macvlan but shares host MAC |
Managing Networks
docker network create mynet # bridge by default docker network create --driver bridge mynet docker network create --subnet 172.20.0.0/16 --gateway 172.20.0.1 mynet docker network create --driver overlay --attachable swarmnet docker network ls # list networks docker network inspect mynet # full JSON docker network rm mynet docker network prune # remove all unused
Connecting Containers
# Attach at run time docker run -d --network mynet --name api myapi docker run -d --network mynet --name db postgres:17 # From inside the network, containers reach each other by name: # api can connect to "db:5432" # Connect / disconnect a running container docker network connect mynet mycontainer docker network disconnect mynet mycontainer # Attach to multiple networks docker run -d --network frontend --network backend myapp
Port Publishing
docker run -p 8080:80 nginx # all interfaces docker run -p 127.0.0.1:8080:80 nginx # localhost only docker run -p 8080:80/udp nginx # UDP docker run -P nginx # all EXPOSE ports → random host ports docker port mycontainer # show published mappings
host and none Modes
docker run --network host nginx # port 80 on the host directly (Linux) docker run --network none alpine # completely isolated
DNS and Service Discovery
- Containers on a user-defined bridge resolve each other by container name.
- The default
bridgenetwork does NOT provide automatic DNS — use named networks. - Custom DNS:
docker run --dns 8.8.8.8 ...or set in daemon config.
# Verify DNS from inside a container
docker exec mycontainer nslookup db
docker exec mycontainer cat /etc/resolv.confCompose Networking
services:
web:
image: nginx
networks:
- frontend
api:
image: myapi
networks:
- frontend
- backend
db:
image: postgres:17
networks:
- backend
networks:
frontend:
backend:
internal: true # no external accessIn Compose, each service is reachable by its service name on any shared network (e.g.,
apican reachdb:5432).
Inspecting Network Traffic
# Show container IP docker inspect -f '{{.NetworkSettings.Networks.mynet.IPAddress}}' mycontainer # Live packet capture (requires tcpdump in image) docker run --rm --network container:mycontainer \ nicolaka/netshoot tcpdump -i eth0 port 80 # Expose network namespace to host tools pid=$(docker inspect -f '{{.State.Pid}}' mycontainer) sudo nsenter -t $pid -n tcpdump
Common Network Patterns
# Sidecar shares network namespace docker run -d --name app myapp docker run -d --network container:app envoy # Link containers (legacy — prefer named networks) docker run --link db:database myapp # deprecated