简体   繁体   中英

nodeJS docker can't connect to redis server

I have a nodeJS-app dockerized. In this, I use a Redis connection. Redis is running on localhost 127.0.0.1:6379. It's not running as a docker container!

In my nodejs file, I have the following:

client = redis.createClient({
  host: '127.0.0.1',
  port: 6379
});

When I run node index , the connection works. But when I build the docker image and run a container, the nodeJS app cannot connect to the redis server (ECONNREFUSED).

I have also tested it with a managed redis server on GCP, it cannot connect either.

Edit:

I can now connect to the localhost redis client by using the following code in nodeJS:

client = redis.createClient({
  host: 'host.docker.internal',
  port: 6379
});

But I still can't connect to an external IP-address, like this:

client = redis.createClient({
  host: EXTERNAL_IP_HERE,
  port: 6379
});

Run your docker container with host.networking :

$ docker run --network=host mycontainer 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM