0

I've dockerize nestjs app with postgres and redis. How can I fix this issue? Postgres and redis are refusing tcp connections. This is docker-compose.yml and console result. I am using typeorm and @nestjs/bull for redis.

docker-compose.yml docker-compose.yml-2

result

Hope much help. Thanks

kometen
  • 6,536
  • 6
  • 41
  • 51
  • Assuming the nextjs app is running on the host machine, changing both service's network mode to "host" should work. Readup more [here](https://docs.docker.com/network/). – Yash Nag Mar 22 '21 at 12:32

1 Answers1

0

When using docker-compose, the containers do not necessarily need to specify the network they are in since they can reach each other from the container name (e.g. postgres, redis in your case) because they are in the same network. See Networking in Compose for more info.

Also, expose doesn't do any operation and it is redundant. Since you specify ports, it should be more than enough to tell Docker which ports of the container are exposed and bound to which ports of the host.

For redis-alpine, the startup command is redis-server and it is not necessary to specify it again. Also, the environment you specified is redundant in this case.

Try the following docker-compose.yaml with all of the above suggestions added:

version: "3"

services:
  postgres:
    image: postgres:alpine
    restart: always
    ports:
      - 5432:5432
    volumes:
      - ./db_data:/var/lib/postgresql/data/
    environment:
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=root
      - POSTGRES_DB=lighthouse

  redis:
    image: redis:alpine
    restart: always
    ports:
     - 6379:6379

Hope this helps. Cheers!

Eranga Heshan
  • 5,133
  • 4
  • 25
  • 48
  • Hi, Eranga Thanks for your kind reply. I've tried following your answer but still same issue. Could you help me more? Thank you. – Yamata Tadashi Mar 23 '21 at 09:10
  • I believe your nest application is running in your machine locally. Not in a docker container? Also, could you check whether `postgres` and `redis` are running as containers in your machine? What is the output of `docker container ls`? – Eranga Heshan Mar 23 '21 at 09:27
  • I added a small change to the file. I made `db_data:/var/lib/postgresql/data/` --> `./db_data:/var/lib/postgresql/data/`. Could you try now? – Eranga Heshan Mar 23 '21 at 09:42
  • I have tried with `./db_data:/var/lib/postgresql/data/` but same. – Yamata Tadashi Mar 23 '21 at 10:25
  • Are you running the NestJS app in your local machine inside a docker container? – Eranga Heshan Mar 23 '21 at 10:32
  • yes, I am running nestjs app on local and will deploy aws fargate – Yamata Tadashi Mar 23 '21 at 10:34
  • I am not familiar with AWS Fargate. But the problem you're having is you have your NestJS inside a docker container that is not in the same network as `redis` or `postgres`. To make it work in your local, you need to change where you specify the host of `redis` and `postgres` from `127.0.0.1` (or `localhost`) to `host.docker.internal`. Then it will work for you in your machine. – Eranga Heshan Mar 23 '21 at 10:38
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/230257/discussion-between-yamata-tadashi-and-eranga-heshan). – Yamata Tadashi Mar 23 '21 at 11:15