skip to Main Content

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


  1. Have you tried to add network b to module a like this:

    version: "3"
    name: "module_a"
    
    networks:
      module_a-network:
        driver: bridge
        external: false
      module_b-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-compose.yml
          service: module_b
        networks:
          - module_a-network
    
    Login or Signup to reply.
  2. Compose automatically creates a network named default for you. Each separate Compose project has its own default 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 to networks: [default]. Now since the default network exists in the file you’re importing into (as a different per-project default network, but that shouldn’t matter) you won’t have this conflict.

    So for example you can trim down the imported project to

    # ./module_b/docker/docker-compose.yml
    version: "4.x"
    name: "module_b"  # NB: not supported in Compose file version 3
    services:
      module_b:
        platform: linux/amd64
        image: hello-world:latest
    # NB: no networks: anywhere in this file
    

    then you should be able to import the file into the other project

    # ./module_a/docker/docker-compose.yml
    version: "4.x"
    name: "module_a"
    services:
      module_a:
        platform: linux/amd64
        image: hello-world:latest
      module_b:
        extends:
          file: ../module_b/docker/docker-compose.yaml
          service: module_b
    

    (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 in docker directories, Compose will think they are "the same". But a module_a/docker-compose.yml will be distinct from a module_b/docker-compose.yml. That will also let you get rid of the name: directive, and you can use the Compose file version 3 format if for some reason you still need the older Python-based Compose implementation.)

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search