1

When I try to connect my backend (using Sequelize) I get this following error:

error ConnectionRefusedError [SequelizeConnectionRefusedError]: connect ECONNREFUSED 127.0.0.1:5432

docker-compose.yml:

version: "3.7"
services:
  frontend:
    build:
      context: ./client
      dockerfile: Dockerfile
    image: client
    ports:
      - "3000:3000"
    volumes:
      - ./client:/usr/src/app
  backend:
    build:
      context: ./server
      dockerfile: Dockerfile
    image: server
    ports:
      - "8000:8000"
    volumes:
      - ./server:/usr/src/app
  db:
    image: postgres
    environment:
      POSTGRES_DB: ckl
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: docker
    ports:
      - "5432:5432"

What am I doing wrong ?

Thanks in advance

1
  • The 127.0.0.1 implies to me that it is attempting to connect to itself, instead of anther container. Commented Dec 17, 2019 at 16:35

1 Answer 1

2

Assuming your backend is connecting to the db you should add a depends_on:

  backend:
    build:
      context: ./server
      dockerfile: Dockerfile
    image: server
    depends_on:
      - db
    ports:
      - "8000:8000"
    volumes:
      - ./server:/usr/src/app

The db will now be accessible at the host db:5432 if your application is configured to connect to localhost:5432 or 172.0.0.1:5432 you'll need to replace the hostname localhost with db. Your postgres connection string might also not have a host and might be trying to connect to localhost by default. Should be able to look at sequelize to figure out how to pass a host.

Sign up to request clarification or add additional context in comments.

2 Comments

Nice, please make sure to accept the answer as correct :)
Thank you so much, had been breaking my head on this issue for the past few hours. :))

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.