How does docker-compose link work?

I have three containers, web, rest and db. I want rest (nodejs) to be able to access db (mongodb).

My docker-compose.yml:

  • How to check the docker-compose file version?
  • docker host rebooted automatically when running docker build/run
  • HOWTO deploy a docker container on aws without using elastic beanstalk or ec2 container service
  • Dockerfile created for JBoss with MySQL image
  • Mount non-existing host directory into non-root container
  • Profile my app with Blackfire container in Docker environment
  • version: '2'
      build: front
       - db
       - rest
      build: backend/mongodb
       - /src/docker/mongodb:/var/lib/mongodb
      restart: always
      build: backend/rest
      restart: always
        - db:database

    In my nodejs program:

    MongoClient.connect("mongodb://database:27017/mytest", function (err, db) {

    However, my mongo client can’t reach the database and keeps restarting…

    What am I doing wrong?

  • Need help on docker scaling of micro-services
  • docker mounted volume data getting wiped after restart the server
  • Docker on OSX with Certificate Authentication Proxy
  • How to restore a mongo Docker container on the Mac
  • Django channels and docker-compose error
  • How to check if a Docker image with a specific tag exist locally?
  • 2 Solutions collect form web for “How does docker-compose link work?”

    Containers will be reachable by their names. So, in your case, you have to use the name of the db service, which is db:

    MongoClient.connect("mongodb://db:27017/mytest", function (err, db) {

    Assuming your node.js application is your web service? Web can only access the db service by using the hostname db. However the rest service can access it using db or database because you have given it a link alias.

    You can either replicate that links: alias in the web service definition, or change your connect string to use db instead of database.

    This should work right now:

    MongoClient.connect("mongodb://db:27017/mytest", function (err, ndb) {

    Or with the link added:

    MongoClient.connect("mongodb://database:27017/mytest", function (err, db) {
    Docker will be the best open platform for developers and sysadmins to build, ship, and run distributed applications.