Docker
Networking
Create networks, attach containers, and expose ports.
By EZ4Code Team
networkbridgeservice-discovery
Code
# Create a custom bridge network
docker network create appnet
docker network ls
# Attach containers to the network
docker run -d --name api --network appnet -p 8080:8080 myapp
docker run -d --name db --network appnet postgres:16
# Containers on the same network resolve by name
# Inside api: psql -h db -U postgres
# Inspect and disconnect
docker network inspect appnet
docker network disconnect appnet db
# Remove unused networks
docker network pruneExplanation
User-defined bridge networks enable automatic DNS resolution between containers by name, so the api container can reach the db container via the hostname db. The default bridge network does not provide this DNS, so custom networks are preferred for multi-container setups. network prune removes unused networks.
More Docker Snippets
Run a Container
Run, list, stop, and remove Docker containers.
Writing a Dockerfile
Build an image from a Dockerfile with multi-instruction layers.
Image Management
Pull, list, tag, push, and prune images.
Volumes & Bind Mounts
Persist data with named volumes, anonymous volumes, and bind mounts.
Docker Compose
Define and run multi-container apps with compose.
Multi-Stage Build
Build artifacts in one stage and copy them into a slim final image.