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:

  • docker shows no memory release of SQLite database
  • docker - change storage folder dynamically
  • Apache Kafka IO errors
  • Trouble communicating between docker containers
  • Automate service (couchbase) configuration of a docker container
  • Not able to restrict the storage space to container
  • 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?

  • How to achieve consistency of re-baking an AMI
  • Explanation about Supervisor and tail over Apache's error.log
  • Haproxy in docker container unstable
  • run multiple instances in the same server
  • something confused with docker
  • Docker: clean up all stopped containers except data-only containers
  • 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.