[英]Connecting Redis localhost from Docker
I have a Node.js application where I use Redis, I am trying to connect the Docker container and the locally running Redis.我有一个 Node.js 应用程序,我在其中使用 Redis,我正在尝试连接 Docker 容器和本地运行的 Redis。
Tried solutions:尝试过的解决方案:
vim /usr/local/etc/redis.conf vim /usr/local/etc/redis.conf
Updated更新
bind 127.0.0.1
To到
bind 0.0.0.0
Stopped the redis and start it again and tried running the docker停止 redis 并重新启动并尝试运行 docker
With the above thing tried running docker run -p 4000:8080 -p 6379:6379 -t node-app
上面的东西尝试运行docker run -p 4000:8080 -p 6379:6379 -t node-app
Both above didn't worked getting the below error以上均无效,出现以下错误
Error: Redis connection to localhost:6379 failed - connect ECONNREFUSED 127.0.0.1:6379
Update: I am checking it on Mac.更新:我正在 Mac 上检查它。
If you use default networking ( --network="bridge"
), you could simply use the IP address of the gateway between the Docker host and the bridge network, ie 172.17.0.1
.如果您使用默认网络( --network="bridge"
),您可以简单地使用 Docker 主机和桥接网络之间网关的 IP 地址,即172.17.0.1
。Here is the documentation.这是文档。 This would work on all platforms, not only on a Mac.这适用于所有平台,而不仅仅是在 Mac 上。
In Dockerfile add this Docker v19.03在 Dockerfile 中添加这个 Docker v19.03
ENV REDIS_HOST "redis://host.docker.internal"
when i using it on node.js当我在 node.js 上使用它时
const REDIS_HOST = process.env.REDIS_HOST ? process.env.REDIS_HOST : ""
const client = redis.createClient(REDIS_HOST)
"docker.for.mac.localhost"
而不是localhost or '127.0.0.1'
会起作用:),它在 mac 机器上对我"docker.for.mac.localhost"
。
You just need to set the docker internal host in your node app's config.json file:您只需要在节点应用程序的 config.json 文件中设置 docker 内部主机:
"redisHost": "host.docker.internal"
You don't need to change any Redis configuration on your local.您无需更改本地的任何 Redis 配置。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.