I have a database shared by multiple applications, each application and related services is defined in a separate compose file. Currently I have to create the external network by hand or have an additional script which makes sure the network exists and then calls docker-compose up. I'd like the option within the compose file to create the external network for me if it doesn't exist.
If the network was created by Compose it wouldn't be external!
What about something like this:
Create a project to start the database and a network for the database, let's say the project name is shareddb
. To start the database (and create the network) you would run docker-compose -p shareddb up d
.
Now in any project that uses the db you can join the network on any services that use it:
version: '2'
networks:
database:
external:
name: shareddb_default
services:
web:
...
networks:
- default
- database
That way you don't have do create anything by hand or write any new scripts, and the distinction between external and internal is maintained.
If you don't want a separate project for it, you could pick one project to be the "canonical" one that "owns" the database, and use the network for that project instead.
Ah, thanks! I learn something new everyday.
Your solution works, but it requires remembering to create the database first, at least the first time around. I'm not sure that's unreasonable, but given that's something I do infrequently, it is something I'm likely to forget.
I have the same use case but I find it weird to define the shareddb_default
network as external in your docker-compose config example: it would feel more natural to me if I could also define the same network in both docker-compose files (use the same network name but do not declare it as external), and docker-compose could then create the network only if it does not exist already.
What do you think?
I have a similar use case. I would like two service on different stacks to share a network without me remembering to start one stack before the other, otherwise external network raises an error.
Is this issue fixed? If yes, would external network be created if it does not exist? And, is there any syntax change to dockore-compose to make that happen?
No change was made. External networks are working as intended.
That said, managed networks with custom names were implemented a few versions ago and are probably closer to what you actually want.
It's not an explicit feature, but starting 2 docker-compose configs with the same project name will have the desired effect, because docker-compose names the network that it creates after the project name.
export COMPOSE_PROJECT_NAME={some_name}
docker-compose -f {compose_cfg_1} up -d
docker-compose -f {compose_cfg_2} up -d
# OR
docker-compose -p {some_name} -f {compose_cfg_1} up -d
docker-compose -p {some_name} -f {compose_cfg_2} up -d
Ah @leoyu037 your solution made my day! I have 8 containers and a reverse proxy container. Using the new syntax, the proxy didn't see the other containers until I found this. With your suggestion, they all get created under the same network. And so I don't forget, I created a script deploy.sh
to make sure to setup the COMPOSE_PROJECT_NAME
variable. Thanks!
Most helpful comment
I have the same use case but I find it weird to define the
shareddb_default
network as external in your docker-compose config example: it would feel more natural to me if I could also define the same network in both docker-compose files (use the same network name but do not declare it as external), and docker-compose could then create the network only if it does not exist already.What do you think?