6

Let's say i have two different docker-compose configurations:

db.yml:

version: '3'
networks:
    default:
        external:
            name: backend

volumes:
    mongo_data:

services:
    mongodb:
        image: mongodb
        container_name: mongodb
        restart: always
        ports:
            - 27017:27017
        networks:
            - backend
        volumes:
            - mongo_data:/data/db

admin.yml:

version: '3'
networks:
    default:
        external:
            name: backend

volumes:
    mongo_data:

services:
    mongoclient:
        image: mongoclient/mongoclient
        restart: always
        ports:
            - 3000:3000
        networks:
            - backend
        depends_on:
            - mongodb
        links:
            - mongodb

This won't work, because the linked container is not configured in the same file. But is there a way to achieve something similar?

I'd like to have a cleaner setup for setting up my production environment so that i am able to restart only the relevant bits that changed and not everything at once.

Alp
  • 29,274
  • 27
  • 120
  • 198
  • 1
    Possible duplicate of [Communication between multiple docker-compose projects](https://stackoverflow.com/questions/38088279/communication-between-multiple-docker-compose-projects) – tgogos Sep 21 '18 at 08:28
  • 1
    The containers are already in the same network and should be able to communicate. What exaclty are you trying to achieve? (startup order or connection) And is the backend network already present? – Ohmen Sep 21 '18 at 08:34
  • Thanks, it's indeed a duplicate. – Alp Sep 21 '18 at 13:32
  • Possible duplicate of [Communication between multiple docker-compose projects](https://stackoverflow.com/questions/38088279/communication-between-multiple-docker-compose-projects) – Alp Sep 21 '18 at 13:32

1 Answers1

2

To link to mongodb you need to link "external":

...
external_links:
        - project_mongodb_1:mongodb
...

Mind that you need to replace project_mongodb_1 with the correct name that docker-compose ps gives you. You need to remove the depends_on section. This won't work. See here: https://github.com/docker/compose/issues/3951

However you shouldn't use links at all because they are deprecated. See here for more info: https://docs.docker.com/compose/compose-file/#external_links

See this on how to do it: https://stackoverflow.com/a/38089080/1029251

Julian Pieles
  • 3,880
  • 2
  • 23
  • 33