Let’s say I have two independent/standalone docker compose projects?
At some point I realized that I also need a service from the other project
the first project looks like this:
// ./module_a/docker/docker-compose.yml
version: "3"
name: "module_a"
networks:
module_a-network:
driver: bridge
external: false
services:
module_a:
platform: linux/amd64
image: hello-world:latest
networks:
- module_a-network
module_b:
extends:
file: ../module_b/docker/docker-compose.yaml
service: module_b
networks:
- module_a-network
The second file looks like this:
// ./module_b/docker/docker-compose.yml
version: "3"
name: "module_b"
networks:
module_b-network:
driver: bridge
external: false
services:
module_b:
platform: linux/amd64
image: hello-world:latest
networks:
- module_b-network
when I start module_a, I get this error:
service "module_b" refers to undefined network module_b-network: invalid compose project
I can’t see what I am doing wrong, any help would be really appreciated! 🙂
I tried to add – module_a-network to the module_b service but I got a similar erro in module_b
2
Answers
Have you tried to add network b to module a like this:
Compose automatically creates a network named
default
for you. Each separate Compose project has its owndefault
network, which is basically configured exactly the way you show the manual network configuration. Also see Networking in Compose in the Docker documentation for more details on this.This means you can safely delete all of the
networks:
blocks in both files. The containers will be automatically set tonetworks: [default]
. Now since thedefault
network exists in the file you’re importing into (as a different per-projectdefault
network, but that shouldn’t matter) you won’t have this conflict.So for example you can trim down the imported project to
then you should be able to import the file into the other project
(Also consider moving these Compose files out of directories named
docker
. Compose uses the innermost directory name as its default project name, so if you have two projects that both have Compose files indocker
directories, Compose will think they are "the same". But amodule_a/docker-compose.yml
will be distinct from amodule_b/docker-compose.yml
. That will also let you get rid of thename:
directive, and you can use the Compose file version 3 format if for some reason you still need the older Python-based Compose implementation.)