简体   繁体   English

Docker 修改的节点容器未连接或在 docker swarm 覆盖网络中看到 mongo db 容器

[英]Docker modified node container not connecting or seeing mongo db container within docker swarm overlay network

I have a docker swarm service that uses mongodb, mongo-express and a custom node image.我有一个使用 mongodb、mongo-express 和自定义节点映像的 docker swarm 服务。 I have created a simple docker yaml file to launch them all together.我创建了一个简单的 docker yaml 文件来一起启动它们。

version: '3.1'

services:
  mongo:
    image: mongo:4.0.6-xenial
    environment:
      MONGO_INITDB_ROOT_USERNAME:
      MONGO_INITDB_ROOT_PASSWORD:

  mongo-express:
    image: mongo-express
    environment:
      MONGO_INITDB_ROOT_USERNAME:
      MONGO_INITDB_ROOT_PASSWORD:
    ports:
    - "8081:8081"

  backend:
    image: backend
    ports:
     - "2222:2222"

Now I know that docker swarm automatically creates a default overlay network among all the nodes.现在我知道 docker swarm 会自动在所有节点之间创建一个默认的覆盖网络。 My docker inspect network displays all the containers within the network.我的 docker inspect 网络显示网络中的所有容器。 I can see the backend connected using localhost:2222 and mongo-express using localhost:8081.我可以看到使用 localhost:2222 连接的后端和使用 localhost:8081 连接的 mongo-express。

My problem is the backend is not connecting, nor can it even see the mongo database.我的问题是后端没有连接,甚至看不到 mongo 数据库。 I tried ssh-ing into the mongo-express container, pinged localhost:27017 successfuly, then pinged localhost:2222 successfuly.我尝试通过 ssh-ing 进入 mongo-express 容器,成功 ping localhost:27017,然后成功 ping localhost:2222。

However, trying to ping/curl mongo-express or mongodb from the backend container wields nothing, its like the ports arent even visible.但是,尝试从后端容器 ping/curl mongo-express 或 mongodb 没有任何作用,就像端口甚至不可见一样。 (localhost:2222 works but localhost:8081 or localhost:27017 don't). (localhost:2222 有效,但 localhost:8081 或 localhost:27017 无效)。

EDIT: I've ssh'd into the backend container again after copying mongo's ip address from docker inspect network, then were able to curl successfully 10.0.5.8:27017.编辑:从 docker inspect 网络复制 mongo 的 IP 地址后,我再次 ssh 进入后端容器,然后能够成功 curl 10.0.5.8:27017。 Why isn't it on the localhost?为什么不在本地主机上?

Each container has it's own IP address.每个容器都有自己的 IP 地址。 This implies that within a docker container, localhost refers to itself.这意味着在 docker 容器中, localhost指的是它自己。 This is why you cannot expect to reach the mongo container IP address from the mongo-express container using localhost .这就是为什么您不能期望使用localhost从 mongo-express 容器访问 mongo 容器 IP 地址的原因。

Docker compose and swarm provide us with a convenient way to address containers by their service name as defined in the docker-compose.yml file. Docker compose 和 swarm 为我们提供了一种方便的方法来通过 docker-compose.yml 文件中定义的服务名称来寻址容器。

So from the mongo-express container, connect to mongo using mongo:27017 .因此,从mongo-express容器中,使用mongo:27017连接到 mongo。

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

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