0

I am trying to dockerize my server and db with docker compose. However, I cannot make the server connect to db when I try to run migration with Sequelize. Below are my Dockerfile and docker-compose.yml:

Dockerfile:

FROM node:14-alpine
RUN mkdir -p /app
WORKDIR /app
ADD . /app
RUN yarn install
RUN yarn sequelize db:migrate
EXPOSE 9000
CMD [ "yarn", "start" ]

docker-compose.yml:

version: "3"
services:
  db:
    image: postgres:10
    container_name: "postgres"
    environment: 
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: postgres
      POSTGRES_DB: mydatabase
    ports:  
      - '5432:5432'
    volumes: 
      - my-db:/var/lib/postgresql/data
  server:
    build: 
      context: .
    environment: 
      DB_USER: postgres
      DB_PASSWORD: postgres
      DB_NAME: mydatabase
    depends_on:
      - db
    ports: 
      - 8100:8100
volumes:
  my-db:

And here is the error log while doing the migration:

ERROR: connect ECONNREFUSED 127.0.0.1:5432

error Command failed with exit code 1.

I already try the suggestions from here, but change the db url cannot solve my issue. It returns

ERROR: getaddrinfo ENOTFOUND postgres://username:pgpassword@127.0.0.1:5432/mydatabase

(I already change the username, password, and db name).

Could someone figure out what goes wrong and how to fix this?

UPDATE: I try to change DB_URL to db which is same as the service name of db. Then the docker runs successfully. The idea came from the setup in this post.

John Wong
  • 1
  • 2
  • 1
    In your Postgres URL there is the IP `127.0.0.1` which *should not be used* (nor `localhost`) in a multi-container environment. Instead, you should try the DB_URL `postgres://username:pgpassword@db:5432/mydatabase`. Namely, with the `db` hostname. – ErikMD Dec 05 '20 at 12:41
  • 1
    For a minimal working example of a similar use case, see e.g. [that SO question: How to use both Java in Docker and PostgreSQL database](https://stackoverflow.com/a/64759910/9164010). – ErikMD Dec 05 '20 at 12:42
  • Also, note that your `ports: ['5432:5432']` specification is useless, and even a *security flaw* if you use this `docker-compose.yml` config in a production environment. – ErikMD Dec 05 '20 at 12:46
  • @erikmd Thanks for reply. In my case, both `username` and `pgpassword` are postgres. What is the hostname `db`? How can I check it if I run docker containers locally? – John Wong Dec 05 '20 at 14:21
  • @erikmd I change to DB_URL to `postgres://postgres:postgres@db:5432/mydatabase`, but I got `ERROR: getaddrinfo ENOTFOUND postgres://postgres:postgres@db:5432/mydatabase` message. Is that the correct database url? – John Wong Dec 05 '20 at 14:30
  • The URL is correct; the `db` hostname corresponds to the docker-compose service name `db:` (see [ECONNREFUSED for Postgres on nodeJS with dockers](https://stackoverflow.com/a/33363660/9164010) for details). I see 2 possible sources of the error: either a docker `networks:` spec is missing (see [How to use both Java in Docker and PostgreSQL database](https://stackoverflow.com/a/64759910/9164010)), or everything is OK but the db is not immediately ready (see [Bash script command to wait until docker-compose process has finished before moving on](https://stackoverflow.com/a/57530775/9164010)). – ErikMD Dec 05 '20 at 17:32
  • @erikmd Thanks. I also try to add `networks`, but it cannot help. Then I find another discussion from [here](https://www.reddit.com/r/docker/comments/8szjw0/how_to_connect_to_postgresql_using_dockercompose/). After referencing the setup in this post, I change the DB_URL to `db` which is same as the database service. The docker can run properly. – John Wong Dec 05 '20 at 20:38
  • 1
    OK! so the last issue was just that the URL I proposed was too-informative: it was providing redundant information w.r.t. DB_USER, DB_PASSWORD, etc., while the URL required was just the hostname (or hostname:port). This makes full sense. To sum up, the main solution was to replace `127.0.0.1` with `db`, as pointed out in the thread you had mentioned. Don't you believe your quesetion should actually marked as a duplicate of this question? [ECONNREFUSED for Postgres on nodeJS with dockers](https://stackoverflow.com/a/33363660/9164010) – ErikMD Dec 05 '20 at 22:16
  • @erikmd Yes. Despite the solution from [https://stackoverflow.com/a/33363660/9164010](https://stackoverflow.com/a/33363660/9164010) did not work for me, I still mark the question as a duplicate. Thanks so much for help again. – John Wong Dec 06 '20 at 04:31

0 Answers0