简体   繁体   English

Docker:当使用 docker 运行而不是 ZBAEDB53E845AE71F1394AE63BCE4Z 时,无法连接到 Redis

[英]Docker: Not able to connect to Redis when using docker run instead of docker-compose up

I'm using docker tool belt on windows home edition.我在 windows 家庭版上使用 docker 工具带。 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. 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 ,我的 Node 应用程序无法连接到 Redis。

throwing:投掷:

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'
}

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, ie localhost:3000.我相信这是因为我的节点应用程序无法找到 Redis,即使当我使用docker-compose up时应用程序正在运行,我也无法在相应的端口上访问它,即 localhost:3000。

this is my docker-compose.yml这是我的 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.我面临两个问题,我相信它们都是相互关联的。

EDIT编辑

could this be because of virtualization issue of windows home edition?这可能是因为 windows 家庭版的虚拟化问题吗? 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.因为它不使用 Hyper V,我刚刚尝试过 docker 所以我不太了解,但大卫的回答很有意义,可能是因为各种网络,我需要连接到有效的网桥或者。

here is what I get when I do docker network ls这是我做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

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-compose.yml文件中运行整个堆栈时, Compose 会自动为您创建一个 Docker 网络,这使得跨服务 DNS 请求工作。

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.如果您尝试手动docker run容器,并且根本没有指定--net选项,那么您会得到 Docker 调用默认桥接网络的东西,这显然不太有用。 You need to make sure your container is attached to the same Docker-internal network as the Redis server.您需要确保您的容器连接到与 Redis 服务器相同的 Docker 内部网络。

You can run docker network ls to get a listing of Docker networks;您可以运行docker network ls以获取 Docker 网络列表; given that docker-compose.yml file there will probably be one named something like source_directory_default .鉴于docker-compose.yml文件可能会有一个名为source_directory_default的文件。 Take that name and pass it to your docker run command (before the image name)取该名称并将其传递给您的docker run命令(在图像名称之前)

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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