Docker:使用docker run而不是docker-compose up时无法连接到Redis [英] Docker: Not able to connect to Redis when using docker run instead of docker-compose up

查看:167
本文介绍了Docker:使用docker run而不是docker-compose up时无法连接到Redis的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Windows Home Edition上使用docker工具带。
我正在尝试使用docker-compose将Node与Redis结合使用,当我使用 docker-compose up (在相同的源目录),但是当我尝试使用 docker run -it myusername / myimage 运行它时,我的Node应用程序无法连接到Redis。

I'm using docker tool belt on windows home edition. I'm trying to use Node with Redis using docker-compose, it is working well when I'm running the image using docker-compose up (in the same source directory), but when I try to run it using docker run -it myusername/myimage, my Node app is not isn't able to connect to Redis.

投掷:

Error: Redis connection to redis-server:6379 failed - getaddrinfo ENOTFOUND redis-server
    at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:60:26) {
  errno: 'ENOTFOUND',
  code: 'ENOTFOUND',
  syscall: 'getaddrinfo',
  hostname: 'redis-server'
}

我相信这是因为我的节点应用程序无法找到Redis,即使当我使用 docker-compose up 运行该应用程序时,我也我无法在相应的端口(即localhost:3000)上访问它。

which I believe is because my node app is not able to find Redis, also even though the app is running when I use docker-compose up, i'm not able to access it on the respective port, i.e. localhost:3000.

这是我的docker-compose.yml

this is my docker-compose.yml

version: '3'
services:
  my_api:
    build: .  
    ports:
     - "3000:3000"
    image: my_username/myimage
    links: 
     - redis-server:redis-server 
  redis-server:
    image: "redis:alpine"

我面临两个问题,我相信它们都是相互关联的。

there are two issues i'm facing and I believe both of them are interrelated.

编辑

这可能是因为Windows家庭版的虚拟化问题?因为它不使用Hyper V,所以我只是尝试使用docker,所以我对此不太了解,但是David的回答很有道理,可能是由于各种网络,我需要连接到有效的网桥等等。

could this be because of virtualization issue of windows home edition? because it doesn't uses Hyper V, I've just try my hands on docker so I don't know about it much, but David's answer makes much sense that it maybe because of various networks and I need to connect to the valid bridge or so.

当我执行 docker network ls

NETWORK ID          NAME                      DRIVER              SCOPE
5802daa117b1        bridge                    bridge              local
7329d018df1b        collect_api_mod_default   bridge              local
5491bfee5551        host                      host                local
be1353789426        none                      null                local


推荐答案

在相同的 docker-compose.yml 文件,自动撰写为您创建一个Docker网络, es跨服务DNS请求正常工作。

When you run the whole stack in the same docker-compose.yml file, Compose automatically creates a Docker network for you, and this makes cross-service DNS requests work.

如果您尝试手动 docker运行容器,而您没有根本不指定-net 选项,您会得到Docker称之为默认网桥网络,它的用处明显不足。您需要确保容器与Redis服务器连接到相同的Docker内部网络。

If you are trying to manually docker run a container, and you don't specify a --net option at all, you get a thing Docker calls the default bridge network, which is distinctly less useful. You need to make sure your container is attached to the same Docker-internal network as the Redis server.

您可以运行 docker network ls 以获得Docker网络列表;鉴于 docker-compose.yml 文件,可能会有一个名为 source_directory_default 的文件。使用该名称并将其传递给您的 docker run 命令(在映像名称之前)

You can run docker network ls to get a listing of Docker networks; given that docker-compose.yml file there will probably be one named something like source_directory_default. Take that name and pass it to your docker run command (before the image name)

docker run --net source_directory_default -p 3000:3000 my_username/my_api

这篇关于Docker:使用docker run而不是docker-compose up时无法连接到Redis的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆