Search code examples
mongodbcontainersdocker-composeexternal-links

Link docker-compose container to an exist running container


I'm trying to run a docker-compose with an exist mongodb container.

In my docker-compose I made this:

version: '2'
services:
  web:
    volumes:
      - .:/app
    external_links:
      - mongo
volumes:
  data:
    external: true

I'm running mongodb with this command:

docker run -d -p 27017:27017 -v /docker/mongo/data:/data/db --name mongo mongo

In normal links in docker-compose, when you run curl mongo, I can connect the mongodb. But I want to use the same mongodb for two different projects.

Using external_links, if I cannot connect mongodb. How can I use external_links in correct way?

Thanks for this.


Solution

  • You are using version 2 of docker-compose, which means linking is handled differently than in older versions. All services started using docker-compose up will be part of a common network. You can specify that network, or docker-compose will create it automatically for you. In order to link to your mongodb container, you need to pass in the --network flag when you start it.

    References: