Skip to content

Docker Network

Sean Sy edited this page Oct 12, 2021 · 1 revision

The Default Network

By default, Docker Compose creates a new network when you build and run an application without defining a network in the docker-compose.yml file. The default network takes on the name [directory]_default where directory is the name of the directory that contains your docker-compose.yml file. Inside it, services defined in the docker-compose.yml can communicate with each other by name rather than IP address.

Potential Issues

Normally the creation of this network does not cause any issues, but occasionally, Docker might mistakenly assign a subnet that is being used by your server for other purposes to this default network. This overlap will break connections.

By default, the IP range for the docker0 bridge adapter is 172.17.0.0/16. When creating a new network without defining the network range yourself, Docker will then use 172.18.0.0/16, then 172.18.0.0/16 the next time, and so on. If for instance your server uses 172.19.0.0/16 to connect to some other VPC, connections to and from there will fail. (See this StackOverflow answer for more details.)

Workarounds

If you do not have access to network configuration files on your server, the proposed solution in the StackOverflow response above might not be possible for you to implement. Other workarounds to try:

  1. Get a list of network ranges your server is using for other purposes and define your Docker networks explicitly in the docker-compose.yml so that you don't accidentally use any of the network ranges from that list. I do this in this project (see docker-compose.yml).
  2. For simple applications where you don't need communication between your containers or where you only have one container, it may be much easier to just use Docker's default bridge network. Just add network_mode: bridge to the service definition like so:
services:
  app:
    image: ubuntu:latest
    network_mode: bridge

Clone this wiki locally