PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, September 5, 2022

[FIXED] Why does my Redis container refused connection to localhost port?

 September 05, 2022     docker, nestjs, redis     No comments   

Issue

When I run docker-compose -f docker-compose.yaml up my containers for Nestjs, Redis and Postgres all run however at the end I get this error

api_1       | Error: connect ECONNREFUSED 127.0.0.1:6379
api_1       |     at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1300:16)

This is my first time using Docker outside of any courses and I'm very stuck. I've been following the instructions from this source:

https://www.tomray.dev/nestjs-docker-compose-postgres#add-postgres-to-docker-compose

How do I get my Redis container to stop refusing the connection?

Dockerfile

FROM node:18-alpine As development

# Create app directory
WORKDIR /usr/src/app

# Copy application dependency manifests to the container image.
# A wildcard is used to ensure copying both package.json AND package-lock.json (when available).
# Copying this first prevents re-running npm install on every code change.
COPY --chown=node:node package*.json ./

# Install app dependencies using the `npm ci` command instead of `npm install`
RUN npm ci

# Bundle app source
COPY --chown=node:node . .

# Use the node user from the image (instead of the root user)
USER node

docker-compose.yaml

services:
  api:
    build:
      dockerfile: Dockerfile
      context: .
      # Only will build development stage from our dockerfile
      target: development
    env_file:
        - .env
    volumes:
      - .:/usr/src/app
    # Run in dev Mode: npm run start:dev 
    command: npm run start:dev
    ports:
      - 3000:3000
    depends_on:
      - redis
      - postgres
  redis: # Name of container
    image: redis
    ports:
      - 6379:6379
    volumes: 
      - redis:/data
  postgres:
    image: postgres
    restart: always
    environment:
      POSTGRES_DB: ${POSTGRES_DB}
      POSTGRES_USER: ${POSTGRES_USER}
      POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
    ports: 
      - '5432:5432'
    volumes:
      - docker-nest-postgres:/var/lib/postgresql/data

volumes:
  docker-nest-postgres:
  redis:
    driver: local

Solution

localhost from one of your containers will target the container itself, not your host machine.

Your end target is the Redis service, you should replace localhost with its name: redis. Docker provides such hostnames within its own networks from its embedded DNS server.



Answered By - AymDev
Answer Checked By - Terry (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing