简体   繁体   English

为什么docker-compose不会为mongo打开端口?

[英]Why docker-compose won't open ports for mongo?

I can't get docker-compose to open port 27017 or at least allow rest to make a connection with mongo. 我无法让docker-compose打开端口27017或至少不让rest与mongo建立连接。

Anyway, the following works just fine: docker run -p 27017:27017 mongo:latest 无论如何,以下工作正常: docker run -p 27017:27017 mongo:latest

This is my docker-compose.yml : 这是我docker-compose.yml

version: '3.5'
services:
  mongo:
    container_name: mongo
    image: mongo:latest
    ports:
      - "27017:27017"

  web:
    container_name: web
    build:
      context: ./src/main/ui/
    ports:
      - "80:80"

  rest:
    container_name: rest
    links:
      - mongo
    build:
      context: .
    ports:
      - "8080:8080"
    entrypoint: [ "java", "-jar", "/rest/build/libs/pBlog.jar" ]

Output of docker ps (as you can see, port 27017 is closed in mongo): docker ps输出(如您所见,端口27017在mongo中已关闭):

▶ docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
08f5dfb7acb8        2d8ccd24817e        "java -jar /rest/bui…"   About an hour ago   Up 11 minutes       0.0.0.0:8080->8080/tcp   pblog_rest_1
4929257802c5        mongo               "docker-entrypoint.s…"   2 hours ago         Up 11 minutes       27017/tcp                pblog_mongo_1
816a60eb9c7f        pblog_web           "nginx -g 'daemon of…"   3 days ago          Up 11 minutes       0.0.0.0:80->80/tcp       pblog_web_1

Output of docker logs <mongoContainerId> : https://pastebin.com/raw/XZGUh4UC docker logs <mongoContainerId>输出: https : docker logs <mongoContainerId>

Output of docker logs <restContainerId> : https://pastebin.com/raw/F7rwKMCe (it isn't able to connect to the database) docker logs <restContainerId>输出: https : docker logs <restContainerId> (它无法连接到数据库)


I've tried making shell script to run mongo inside container. 我试着制作shell脚本在容器内运行mongo。 Also tried to edit ports section in mongo service to "1-65535:1-65535" (this should open all ports) - won't work either. 还尝试将mongo服务中的端口部分编辑为"1-65535:1-65535" (这应该打开所有端口)-也不起作用。 Deleting ports section and adding expose: - 27017 gives the same result. 删除ports部分并添加expose: - 27017得到相同的结果。

It seems like mongo image somehow forces docker to close ports. 似乎mongo image以某种方式迫使docker关闭端口。

You need to open ports only if you want to connect from the outside, which can be useful but for your scenario it is not necessary. 仅当您要从外部连接时才需要打开端口,这可能很有用,但对于您的情况则没有必要。 Your rest container should be able to find mongo under this URL mongo:27017. 您的rest容器应该可以在此URL mongo:27017下找到mongo。 Since each container sees itself as localhost, it won't see other containers on localhast at all. 由于每个容器都将自己视为本地主机,因此它根本不会在localhast上看到其他容器。

Note: You have opened the port sucessfully, and you should be able to ping it from your PC, but from the inside of containers, the network looks differently. 注意:您已成功打开端口,应该可以从PC ping通该端口,但是从容器内部看,网络看起来有所不同。

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

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