Skip to content
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 prune

Explanation

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