Docker Compose has only --x-networking
and --x-network-driver
options. --x-networking
creates a new bridge or overlay network depending upon where the application is targeted to.
It would be useful to have an option, similar to --net
that would allow to target the entire Compose file to a pre-created network. Otherwise existing Compose files will have to be changed.
You can set the project name with -p
or COMPOSE_PROJECT_NAME
, which is used as the network name as well.
I think we'd like to supposed a way to set the network name to a different default, but I'm not sure what that will look like yet. Some discussion in #745
Here is the flow I was thinking:
docker network create
docker-compose -n <NETWORK> up -d
. docker-compose -p <PROJECT> up -d
will create the network based upon project name. Specifying -n
will give an option to override network name.
Have started a tangentially related discussion of specifying networks to join in https://github.com/docker/compose/issues/2478.
As far as I remember from my tests you can also create a network and use:
docker network create mynet
app:
net: 'mynet'
But you need it for every service.
@schmunk42 yes, aware of that but generally all services would be running in the same overlay network. For example, testing can be be done in a bridge network, then in overlay network using Machines, and finally in overlay network on Cloud. So retargeting to different networks would be useful as opposed to baking it in the Compose file.
@aanand will contribute to #2478.
@arun-gupta Compose 1.5 supports variable substitution. E.g.
app:
net: ${CUSTOM_NET}
CUSTOM_NET=<NETWORK> docker-compose -p <PROJECT>
should do the trick.
I've updated the title to reflect the difference between this issue and #2478
@kevstigneev that worked, scheduled a blog on that.
One interesting thing what I mentioned. When is used network name with underscore
docker network create test_net
docker network inspect test_net
[
{
"Name": "test_net",
"Id": "92529edbf2f104e174d97c3b4da5e39ca0faca5043004b694fb7a3de728d25db",
"Scope": "local",
"Driver": "bridge",
"IPAM": {
"Driver": "default",
"Config": [
{}
]
},
"Containers": {},
"Options": {}
}
]
docker-compose create network ignoring underscore
docker-compose -f projects/docker/compose/development/definitions/convert.yml --x-networking -p test_net run --rm convert_console bin/rails console
Creating network "testnet" with driver "None"
If used COMPOSE_PROJECT_NAME
variable behaviour is the same
-bash-4.2$ docker network ls
NETWORK ID NAME DRIVER
92529edbf2f1 test_net bridge
d9b56ef52ec4 testnet bridge
This is a side-effect of using the project name. We normalize the project name, but #2119 is an open issue to consider dropping the normalization.
Either way, allowing a way to set the network name separately fixes it for networks.
Given how we've now implemented networking and network configuration, I think the original stated use case ("target the entire Compose file to a pre-created network") can be served by doing this:
networks:
default:
external:
name: pre-created-network
@arun-gupta: if you're happy with that, I'm going to close this issue.
LGTM
Perhaps it's worth adding the default network name override to the documentation (the syntax to specify external: true
also changed)?
I see a PR for this was created already in #9660 , could we review and merge it to save everyone time in the future?
Most helpful comment
Given how we've now implemented networking and network configuration, I think the original stated use case ("target the entire Compose file to a pre-created network") can be served by doing this:
@arun-gupta: if you're happy with that, I'm going to close this issue.