0

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?

2
  • You could access the database from the node container by accessing db.
    – 030
    Commented Dec 5, 2019 at 15:59
  • 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.
    – 030
    Commented Dec 5, 2019 at 16:00

1 Answer 1

0

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 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.

1
  • 1
    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. Commented Dec 6, 2019 at 0:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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