Alp
Alp

Reputation: 29739

Link containers that are configured in two different docker-compose files

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.

Upvotes: 8

Views: 5630

Answers (1)

Julian Pieles
Julian Pieles

Reputation: 4016

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

Upvotes: 3

Related Questions