Answers: <p>docker-compose contains an internal DNS. In the example the service names are also the DNS names. In this case, the DNS of the database is <code>db</code>. If one configure this as a hostname in the node app then one should be able to access the database.</p>
  
  <p>Back in the day, <a href="https://docs.docker.com/compose/wordpress/" rel="nofollow noreferrer">the wordpress example of docker-compose</a> helped me understanding this. When I changed <code>WORDPRESS_DB_HOST: db:3306</code> to <code>WORDPRESS_DB_HOST: db2:3306</code>, the wordpress was not able to access the mysql <code>db</code> anymore as depicted in the log. When it was changed back and forth to <code>db</code> and to <code>db2</code> it was not able to access it and with <code>db</code> it was. Then I realized that the service names are actually added to an internal DNS that can be resolved by other services that are defined in the docker-compose.yml.</p>
  
  <p>Recently I realized that running docker-compose up and specifying multiple files using <code>-f</code> also ensures that all services get added to the internal DNS. Initially I thought that the resolving was restricted to the same docker-compose.yml, but probably the docker network is responsible. If multiple apps reside in the same network, in this case the default, then they can resolve each other.</p>
  
Answers: CC BY-SA 4.0
Answers: 2019-12-06T00:08:29.913Z
Answers: This is working fine, now the problem that I've got was that the app container was starting before the other services containers. I fixed that adding the depends_on tag on the docker-compose file but anyways just Rabbit keeps starting after the app.
Answers: Alessandro Rodríguez
Answers: CC BY-SA 4.0
Answers: 2019-12-05T16:02:59.277Z
Answers: 2019-12-05T16:07:59.387Z
Answers: 2019-12-05T16:07:59.387Z
Answers: 030
Body: <p>I'm trying to connect from a node app container to a postgresql container using docker-compose but I'm not able to do it.
  This is my docker-compose.yml file:</p>
  
  <pre><code>version: '3.7'
  services:
    db:
      image: postgres:12.1
      ports:
        - "5432:5432"
      env_file:
        - "./.env"
      networks:
        - tcp-modbus
      volumes:
        - ./modbustcp.sql:/docker-entrypoint-initdb.d/init.sql
    redis:
      image: redis:5.0.6
      ports:
        - "6379:6379"
      networks:
        - tcp-modbus
    rabbit:
      image: "rabbitmq"
      environment:
        RABBITMQ_ERLANG_COOKIE: "SWQOKODSQALRPCLNMEQG"
        RABBITMQ_DEFAULT_USER: "*"
        RABBITMQ_DEFAULT_PASS: "*"
        RABBITMQ_DEFAULT_VHOST: "/"
      ports:
      - "15672:15672"
      - "5672:5672"
      volumes:
        - "./enabled_plugins:/etc/rabbitmq/enabled_plugins"
      networks:
        - tcp-modbus
    modbus-tcp:
      image: customImage
      ports:
        - "8080:3000"
      links:
        - "db"
      volumes:
        - "./modbus.env:/usr/src/app/.env"
      networks:
        - tcp-modbus
  networks:
    tcp-modbus:
      driver: bridge
  </code></pre>
  
  <p>I'm using typeorm to set the connection from the node app and was working fine before dockerizing the app, maybe has something to do with the host name of the container, cause I'm using localhost to connect to the postgresql container. Can I use somehow the container network as a hostname to connect to postgres?</p>
  
Comments: CC BY-SA 4.0
Comments: 2019-12-05T15:59:19.020Z
Comments: You could access the database from the node container by accessing `db`.
Comments: 030
Comments: CC BY-SA 4.0
Comments: 2019-12-05T16:00:11.230Z
Comments: `I'm trying to connect from a node app container to a postgresql container using docker-compose`. Please clarify what app in the docker-compose.yml represents the node app you are referring to.
Comments: 030
Tags: <docker><networking><postgresql>
Title: How to connect to postgres container from a node container using docker-compose?
UserDisplayName: Alessandro Rodríguez
ThreadText: # How to connect to postgres container from a node container using docker-compose?
  
  (Asked by: username_0 on 2019-12-05)
  
  I'm trying to connect from a node app container to a postgresql container using docker-compose but I'm not able to do it.
  This is my docker-compose.yml file:
  
  ```
  version: '3.7'
  services:
    db:
      image: postgres:12.1
      ports:
        - "5432:5432"
      env_file:
        - "./.env"
      networks:
        - tcp-modbus
      volumes:
        - ./modbustcp.sql:/docker-entrypoint-initdb.d/init.sql
    redis:
      image: redis:5.0.6
      ports:
        - "6379:6379"
      networks:
        - tcp-modbus
    rabbit:
      image: "rabbitmq"
      environment:
        RABBITMQ_ERLANG_COOKIE: "SWQOKODSQALRPCLNMEQG"
        RABBITMQ_DEFAULT_USER: "*"
        RABBITMQ_DEFAULT_PASS: "*"
        RABBITMQ_DEFAULT_VHOST: "/"
      ports:
      - "15672:15672"
      - "5672:5672"
      volumes:
        - "./enabled_plugins:/etc/rabbitmq/enabled_plugins"
      networks:
        - tcp-modbus
    modbus-tcp:
      image: customImage
      ports:
        - "8080:3000"
      links:
        - "db"
      volumes:
        - "./modbus.env:/usr/src/app/.env"
      networks:
        - tcp-modbus
  networks:
    tcp-modbus:
      driver: bridge
  
  ```
  
  I'm using typeorm to set the connection from the node app and was working fine before dockerizing the app, maybe has something to do with the host name of the container, cause I'm using localhost to connect to the postgresql container. Can I use somehow the container network as a hostname to connect to postgres?
  
  **Tags:** docker, networking, postgresql
  
  > **Comments:**
  > **username_1**: You could access the database from the node container by accessing `db`.  
  > **username_1**: `I'm trying to connect from a node app container to a postgresql container using docker-compose`. Please clarify what app in the docker-compose.yml represents the node app you are referring to.  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2019-12-05. Score: 0 | Accepted answer
  
  docker-compose contains an internal DNS. In the example the service names are also the DNS names. In this case, the DNS of the database is `db`. If one configure this as a hostname in the node app then one should be able to access the database.
  
  Back in the day, [the wordpress example of docker-compose](https://docs.docker.com/compose/wordpress/) helped me understanding this. When I changed `WORDPRESS_DB_HOST: db:3306` to `WORDPRESS_DB_HOST: db2:3306`, the wordpress was not able to access the mysql `db` anymore as depicted in the log. When it was changed back and forth to `db` and to `db2` it was not able to access it and with `db` it was. Then I realized that the service names are actually added to an internal DNS that can be resolved by other services that are defined in the docker-compose.yml.
  
  Recently I realized that running docker-compose up and specifying multiple files using `-f` also ensures that all services get added to the internal DNS. Initially I thought that the resolving was restricted to the same docker-compose.yml, but probably the docker network is responsible. If multiple apps reside in the same network, in this case the default, then they can resolve each other.
  
  > **Comments:**
  > **username_0**: This is working fine, now the problem that I've got was that the app container was starting before the other services containers. I fixed that adding the depends_on tag on the docker-compose file but anyways just Rabbit keeps starting after the app.  
  
  ---
  
  