nodejs app cant connect to redis on docker swarm

Viewed 32

I'm having a tough time connecting my nodejs backend service to the redis service (both running in swarm mode).

The backend server fails to start as it complains that it was not able to reach redis with the following error:

enter image description here

But on the contrary, if I try to reach the redis service from within my backend container using curl redis-dev.localhost:6379, I get the response curl: (52) Empty reply from server. Which implies that the redis service is available to the backend container:

enter image description here

Below is the backend's docker-compose yaml:

version: "3.8"
services:
  backend:
    image: backend-app:latest
    command: node dist/main.js
    ports:
      - 4000:4000
    environment:
      - REDIS_HOST='redis-dev.localhost'
      - APP_PORT=4000
    deploy:
      restart_policy:
        condition: on-failure
      update_config:
        parallelism: 1
        delay: 15s
      labels:
          - "traefik.enable=true"
          - "traefik.http.routers.backend-app.rule=Host(`backend-app.localhost`)"
          - "traefik.http.services.backend-app.loadbalancer.server.port=4000"
          - "traefik.docker.network=traefik-public"
    networks:
      - traefik-public
          
networks:
  traefik-public:
    external: true
    driver: overlay

And following is my redis's compose file:

version: "3.8"
services:
  redis:
    image: redis:7.0.4-alpine3.16
    hostname: "redis-dev.localhost"
    ports:
      - 6379:6379
    deploy:
      restart_policy:
        condition: on-failure
      update_config:
        parallelism: 1
        delay: 15s
    networks:
      - traefik-public
      
networks:
  traefik-public:
    external: true
    driver: overlay
1 Answers

The issue was with the backend's docker compose file. The REDIS_HOST environment variable should have been redis-dev.localhost instead of 'redis-dev.localhost'. That's why the backend app was complaining that it was not able to reach the redis host.

It's funny how elementary these things can be.

Related