ThomasReggi
ThomasReggi

Reputation: 59345

Redis and docker-compose connect 127.0.0.1:6379

I have an existing node application that connects to redis at 127.0.0.1:6379. I am unable to change this.

I understand that with docker compose it connects the two services redis and node and allows them to connect using the redis hostname, however I can't change this.

How can I make it so that redis is accessible from the node application at the 127.0.0.1:6379 host and port?

Here's my docker-compose.yml file:

version: '3'

services:
  redis:
    image: redis
    hostname: "127.0.0.1"
  redis-cli:
    image: redis
    links:
      - redis
    command: redis-cli -h 127.0.0.1

Here's the output:

$ docker-compose run redis-cli
Starting install_redis_1 ... done
Could not connect to Redis at 127.0.0.1:6379: Connection refused
Could not connect to Redis at 127.0.0.1:6379: Connection refused
not connected>

Upvotes: 1

Views: 4311

Answers (1)

Sergey Lapin
Sergey Lapin

Reputation: 2693

You can use network_mode: host in both containers to make redis expose its 6379 on localhost, and make localhost available to redis-cli:

version: '3'

services:
  redis:
    image: redis
    network_mode: host
  redis-cli:
    depends_on:
      - redis
    image: redis
    network_mode: host
    command: redis-cli -h 127.0.0.1 ping

And running it:

> docker-compose run redis-cli
Starting dockerredis_redis_1 ... done
PONG

Upvotes: 2

Related Questions