如果我运行后端的单个docker容器,它运行良好并连接到在主机上运行的mongodb。但是当我使用docker-compose运行我的后端时,它没有连接到mongodb并打印到控制台:
MongoError: failed to connect to server [12.345.678.912:27017] on first connect [MongoError: connection 0 to 12.345.678.912:27017 timed out]
docker-compose.yml内容:
version: "3"
services:
web:
image: __BE-IMAGE__
deploy:
replicas: 1
restart_policy:
condition: on-failure
resources:
limits:
cpus: "0.1"
memory: 2048M
ports:
- "1337:8080"
networks:
- webnet
visualizer:
image: dockersamples/visualizer:stable
ports:
- "1340:8080"
volumes:
- "/var/run/docker.sock:/var/run/docker.sock"
deploy:
placement:
constraints: [node.role == manager]
networks:
- webnet
networks:
webnet:
我如何运行单个docker容器:
docker run -p 1337:8080 BE-IMAGE
答案 0 :(得分:0)
您需要链接mongo端口,因为localhost与容器内部和容器外部不同
ports:
- "1337:8080"
- "27017:27017"
在端口定义上,左侧是在外面,右侧是在您的容器内部...您的错误说您的容器内部它无法看到端口27017 ...上面只是链接该mongo端口所以容器可以访问码头工人以外的港口