
8th July 2024.
Updated 26th May 2025 - Added how to disable IPv4 in a docker compose file
Docker Compose is a tool used to define and run multi-container Docker applications. Instead of running multiple standalone containers and try to make them interact, you create a work directory with a YAML file that contains all the services, volumes and network parameters.
I will be writing some How-To’s on setting up some docker compose applications in the near future. This post will focus on the different network options available for Docker Compose.
Network Options
Source: https://docs.docker.com/compose/compose-file/06-networks/#examples
All drivers described in part 1 except overlay can be used in a docker compose project. In addition, some Docker compose specific options can be used as well.
Example Compose file
These options affects all containers attached to the same network.
services:
nginx1:
image: nginx
networks:
example_net: # optionally set IP addresses underneath:
ipv4_address: 100.110.1.10
ipv6_address: 2001:DB8:0:A01A::A
networks:
example_net:
external: false
internal: false
enable_ipv6: true
attachable: true
driver: bridge
driver_opts:
com.docker.network.enable_ipv4: "false"
ipam:
config:
- subnet: 100.110.1.0/24
gateway: 100.110.1.1
aux_addresses: # exclude addresses already in use
standalone_container: 100.110.1.2
some_thing: 100.110.1.3
- subnet: 2001:DB8:0:A01A::/80
gateway: 2001:DB8:0:A01A::1
aux_addresses:
standalone_container: 2001:DB8:0:A01A::2
some_thing: 2001:DB8:0:A01A::3
external:
When set to
true
, it means that the network has been created outside of compose, meaning that you specify a network that was created with thedocker network create
command. Default isfalse
.Setting external network means that all network settings are made outside docker compose. All other attributes apart from
name
are irrelevant. If you specify any other attribute, it rejects the compose file as invalid.
internal:
When set to
true
, containers will not configure a default gateway, making them un-routable. Not even possible to expose ports. Default isfalse
.
Note: Depending on which driver you use, the docker node will still assign itself an IP address meant for default GW. That seems quite unnecessary.
enable_ipv6:
should be self-explanatory. Default is
false
.
attachable:
When set to
true
, standalone containers and other compose projects should be able to attach to this network. Default isfalse
.
driver:
choose which network driver to use. default is
bridge
.
driver_opts:
com.docker.network.enable_ipv4: false turns off IPv4.
ipam:
With
ipam
you can set your own subnets and gateways, instead of relying on the defaults.Setting IPv4 addresses when the above driver option is set to false will obviously create a conflict but I’m just trying to fit all parameters into one example.
Other Network Settings
There are two more things that can be set under docker compose networks.
name:
Sets a custom name for the network. By default the name of a compose network will prepend the working directory or the COMPOSE_
PROJECT_NAME
variable set before the defined name inside the compose file.name:
is the only parameter that can be configured with theexternal
property. You can also specify the name from an environment variable:
networks:
example_net:
external: true
name: "${NETWORK_ID}"
labels:
With labels you can set descriptions on the networks. It is recommended to use reverse-DNS notation to prevent labels from conflicting with those used by other software. Example:
networks:
example_net:
name: without-project-name
labels:
online.bastuklubben.description: "Test network"
online.bastuklubben.location: "at home in the basement"
$ docker network inspect without-project-name
...
"Labels": {
"com.docker.compose.network": "vyos",
"com.docker.compose.project": "vyos",
"com.docker.compose.version": "2.27.1",
"online.bastuklubben.description": "Test network",
"online.bastuklubben.location": "at home in the basement"
}
}
]
Service Network Options
These options can be set under a specific service:
services:
nginx1:
image: nginx
hostname: nginx1.bastuklubben.online
networks:
example_net:
nginx2:
image: nginx
hostname: nginx2.bastuklubben.online
network_mode: bridge
nginx3:
image: nginx
hostname: nginx3.bastuklubben.online
network_mode: host
Networks:
Specifies which network to use as defined under
networks:.
If no network is specified, it will create a bridge network using the next available default scope with the name of the project directory.
You can optionally assign addresses to containers. However, that require
ipam:
andsubnet:
configuration to be specified.
Network_Mode:
Specifies an alternative driver. This means that you won’t be using a docker compose network.
When set to
bridge
, it will use the default bridge network.When set to
host
, it will use the docker host addresses as explained in part 1.
Be aware that the containers won’t receive any domain names when
bridge
orhost
network mode is set.
$ docker container inspect sauna-nginx1-1
...
"Networks": {
"bridge": {
...
"DNSNames": [
"sauna-nginx1-1",
"nginx1",
"fdeb486c4d87",
"nginx1.bastuklubben.online"
]
$ docker container inspect sauna-nginx2-1
...
"Networks": {
"bridge": {
...
"DNSNames": null
Use-cases
In upcoming How-To’s i will demonstrate use-cases for many of these settings, including the driver options from Part 1. Stay tuned.