
Introduction
When you run docker-compose up, Docker Compose creates a default network for your app. It’s named after your project (usually the directory with your docker-compose.yml file) with the suffix _default. This network lets all the services in your docker-compose.yml talk to each other.
But you can also set up custom networks to have more control over how your services interact. By doing this, you can separate services, control how they connect, and set up specific network configurations.
1. What Are Docker Networks?
In Docker, networks are virtual layers that allow containers to communicate with each other and with external systems. They are crucial for orchestrating complex applications that consist of multiple interconnected services. Docker provides several network drivers to suit different use cases:
- Bridge Network: The default network type for containers on a single Docker host. It allows containers connected to the same bridge network to communicate.
- Host Network: The container shares the host’s networking stack, removing network isolation between the container and the host.
- Overlay Network: Enables networking between multiple Docker hosts, suitable for swarm services.
- Macvlan Network: Assigns a MAC address to each container, making it appear as a physical device on the network.
- None: Disables networking for the container.
2. Networks in Docker Compose
Docker Compose simplifies multi-container Docker applications by allowing you to define services, networks, and volumes in a single docker-compose.yml file. Networks in Docker Compose are used to control how your services communicate with each other.
- Default Network: If you don’t specify any networks, Docker Compose creates a default network where all services are connected. This network uses the bridge driver.
- Custom Networks: You can define custom networks to have fine-grained control over the communication between services.
3. Why Define Custom Networks?
- Isolation: By placing services on different networks, you can control which services can communicate, enhancing security.
- Custom Configuration: Specify network drivers, IP address ranges, subnets, gateways, and other advanced options.
- Service Discovery: Services on the same network can resolve each other by service name, simplifying inter-service communication.
- Scalability: Better network management as your application grows in complexity.
How to Define Networks in Docker Compose
1. Defining Networks
In your docker-compose.yml, you can define networks under the networks key:
services:
web:
image: nginx
networks:
- frontend
app:
image: myapp
networks:
- frontend
- backend
db:
image: postgres
networks:
- backend
networks:
frontend:
backend:
frontendNetwork: Bothwebandappservices are connected here.backendNetwork:appanddbservices communicate over this network.- Isolation: The
webservice cannot directly communicate with thedbservice.
2. Specifying Network Drivers and Options
You can specify the network driver and additional configurations:
networks:
frontend:
driver: bridge
driver_opts:
com.docker.network.bridge.enable_icc: 'true'
ipam:
driver: default
config:
- subnet: 172.16.238.0/24
backend:
driver: overlay
driver: Specifies the network driver (bridge,overlay, etc.).driver_opts: Passes options to the driver.ipam: Configures IP address management.
3. Using Network Aliases
Network aliases allow a service to be reachable by additional names:
services:
app:
image: myapp
networks:
frontend:
aliases:
- myapp.local
Now, other services on the frontend network can reach app at myapp.local.
Communication Between Services
When services are on the same network, they can communicate using the service name as the hostname. For example, in the app service, you can connect to the db service using the hostname db if they share a network.
Example:
services:
app:
image: myapp
networks:
- backend
db:
image: postgres
networks:
- backend
In the app service’s code, you can connect to the database at db:5432.
Best Practices
- Limit Scope: Only connect services that need to communicate on the same network.
- Use Overlay Networks: For multi-host networking in swarm mode.
- Explicit Definitions: Define networks explicitly for clarity and better management.
- Monitor Networks: Use Docker commands to inspect and monitor network configurations.
Conclusion
While Docker Compose automatically creates a default network, defining custom networks is beneficial for complex applications. It provides better control over service communication, enhances security through isolation, and allows for customized network configurations. Understanding Docker networks enables you to design scalable and maintainable applications.
Additional Resources


