Answers: <p>You've got a lot of unneeded settings in the compose file, here's a stripped down version that would work just as well:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>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.</p>
  
  <hr>
  
  <p>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.</p>
  
  <p>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 <a href="https://github.com/vishnubob/wait-for-it" rel="nofollow noreferrer">wait-for-it</a> that is written as a bash shell script.</p>
  
Answers: CC BY-SA 4.0
Answers: 2018-07-24T14:18:28.630Z
Answers: 2018-07-24T14:18:28.630Z
Answers: BMitch
Body: <p>I have the following configuration in my <code>docker-composer.yml</code> file.</p>
  
  <pre><code>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
  </code></pre>
  
  <p>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.</p>
  
  <pre><code>Error connecting to mongo : no reachable servers
  </code></pre>
  
  <p>Please help me to solve the problem, I got stuck.</p>
  
  <p>Thanks.</p>
  
Comments: CC BY-SA 4.0
Comments: 2018-07-24T14:19:05.623Z
Comments: Please do not cross post. Pick the appropriate site and ask the question in one location only. [Reference](https://stackoverflow.com/q/51499833/596285)
Comments: BMitch
Tags: <docker><docker-compose><dockerfile><networking>
Title: Docker hostnames are not resolved in a custom network
UserDisplayName: solderingiron
ThreadText: # Docker hostnames are not resolved in a custom network
  
  (Asked by: username_0 on 2018-07-24)
  
  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.
  
  **Tags:** docker, docker-compose, dockerfile, networking
  
  > **Comments:**
  > **username_1**: Please do not cross post. Pick the appropriate site and ask the question in one location only. [Reference](https://stackoverflow.com/q/51499833/596285)  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2018-07-24. Score: 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](https://github.com/vishnubob/wait-for-it) that is written as a bash shell script.
  
  
  ---
  
  