-2

I have the following configuration in my docker-composer.yml file.

version: '3.3'
services:
  service-1:
    container_name: 'service-1'
    build: './service-1'
    depends_on:
      - 'mongo'
      - 'consul'
    networks:
      backend:
        aliases:
          - service-1
  service-2:
    build: './service-2'
    ports:
      - '8825:8825'
      - '8835:8835'
    networks:
      frontend:
      backend:
        aliases:
          - service-2
    depends_on:
      - 'mongo'
      - 'consul'
  consul:
    image: 'consul:latest'
    networks:
      backend:
        aliases:
          - consul
  mongo:
    image: 'mongo:latest'
    networks:
      backend:
        aliases:
          - mongo

networks:
  frontend:
  backend:
    internal: true

When my containers start they are not able to communicate between each other using host names. Most of containers use the mongo db container, but they are not able even reach it and I am getting the following error.

Error connecting to mongo : no reachable servers

Please help me to solve the problem, I got stuck.

Thanks.

1
  • Please do not cross post. Pick the appropriate site and ask the question in one location only. Reference
    – BMitch
    Jul 24 '18 at 14:19
1

You've got a lot of unneeded settings in the compose file, here's a stripped down version that would work just as well:

version: '3.3'
services:
  service-1:
    build: './service-1'
    networks:
      - backend
  service-2:
    build: './service-2'
    ports:
      - '8825:8825'
      - '8835:8835'
    networks:
      - frontend
      - backend
  consul:
    image: 'consul:latest'
    networks:
      - backend
  mongo:
    image: 'mongo:latest'
    networks:
      - backend

networks:
  frontend:
  backend:
    internal: true

You automatically get the alias of the service name for each container, no need to duplicate that. You also lose the ability to scale a service if you give it a container name. I'd also recommend moving the build step out of the compose file and use an image name for the apps you're building locally.


Now for the likely issue, you have a depends_on in your compose file. At best, this will not do what you're looking for. All it checks that the other container has been created and started, but not that the application inside is ready to serve traffic, and a DB may take time to become available. At worst, you'll get an error that it's unsupported if you try to move this into swarm mode.

Instead of depending on docker for this, update your application entrypoint to check for the external dependencies and wait a minute or two for them to become available before failing. A very simple example tool for this is wait-for-it that is written as a bash shell script.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.