The top level networks
section creates networks in compose (or specifies external user created networks) that can be used by various services. Each service then defines a networks
section to identify one or more user created networks to join. That service gets automatically configured with a network alias for DNS based service discovery. This is the preferred method to setup networking with services, and by default each of these networks is a bridge network.
To skip that entire process, you can use a network_mode
that changes from the compose managed networking to switch to host
, bridge
, none
, service:$name
, or container:$name
. Each of these has unique properties.
host
has no network namespacing at all, similar to starting a process outside of a container.bridge
is the legacy bridge network docker always creates, but disables some features like DNS based service discovery.none
is no networking at all, only the loopback interface is defined.service:$name
and container:$name
are special ways of connecting multiple containers into the same network namespace. The same thing is done by pods in kubernetes, and it can be useful for network troubleshooting or when creating sidecars that require access to localhost. This should only be used as a last resort when building microservices since it breaks the ability to independently scale and deploy the containers.