0

Good morning guys.

I'm having a problem connecting a nodejs application, in a container, to another container that contains a redis server. On my local machine I can connect the application to this redis container without any problem. However, when trying to upload this application in a container, a timeout error is returned.

I'm new to docker and I don't understand why I can connect to this docker container in the application running locally on my machine but that same connection doesn't work when I upload the application in a container.

I tried using docker-compose, but from what I understand it will upload in another container to the redis server, instead of using the redis container that is already in docker.

To connect to redis I'm using the following code:

createClient({
      socket: {
        host: process.env.REDIS_HOST,
        port: Number(process.env.REDIS_PORT)
      }
    });

Where REDIS_HOST is the address of my container running on the server and REDIS_PORT is the port where this container is running on my server.

To run redis on docker I used the following guide: https://redis.io/docs/stack/get-started/install/docker/

I apologize if my problem was not very clear, I'm still studying docker.

  • please provide the values of REDIS_HOST and REDIS_PORT and how did you run redis through docker – godo57 Jan 30 '23 at 14:59
  • @godo57 I used the following command to run redis: docker run -d --name redis-stack -p 8032:6379 redis/redis-stack-server:latest – John Cunha Jan 30 '23 at 15:11
  • Do you have a `docker run --net` option? Without that it will be hard to make connections from another container. Also see for example [How to communicate between Docker containers via "hostname"](https://stackoverflow.com/questions/30545023/how-to-communicate-between-docker-containers-via-hostname). – David Maze Jan 30 '23 at 15:18

1 Answers1

0

You mentioned you are using Docker Compose. Here's an example showing how to start Redis in a container, and make your Node application wait for that container then use an environment variable in your Node application to specify the name of the host to connect to Redis on. In this example it connects to the container running Redis that I've called "redis":

version: "3.9"
services:
  redis:
    container_name: redis_kaboom
    image: "redislabs/redismod"
    ports:
      - 6379:6379
    volumes:
      - ./redisdata:/data
    entrypoint:
      redis-server
        --loadmodule /usr/lib/redis/modules/rejson.so
        --appendonly yes
    deploy:
      replicas: 1
      restart_policy:
        condition: on-failure
  node:
    container_name: node_kaboom
    build: .
    volumes:
      - .:/app
      - /app/node_modules
    command: sh -c "npm run load && npm run dev"
    depends_on:
      - redis
    ports:
      - 8080:8080
    environment:
      - REDIS_HOST=redis

So in your Node code you'd then use the value of process.env.REDIS_HOST to connect to the right Redis host. Here, I'm not using a password or a non-standard port, you could also supply those as environment variables that match the configuration of the Redis container in Docker Compose too if you needed to.

Disclosure: I work for Redis.

Simon Prickett
  • 3,838
  • 1
  • 13
  • 26