无法使用mongo连接到docker容器

时间:2018-03-03 12:47:34

标签: mongodb docker docker-compose

我有docker-compose.yml

version: '3.3'

services:
  java-app:
    image: java-app:latest
    depends_on:
      - elasticsearch
      - mongo
    environment:
      - WAIT_HOSTS=elasticsearch:9200,mongo:27017
    ports:
      - "8080:8080"

  elasticsearch:
    image: elasticsearch:latest
    command: elasticsearch

  mongo_connector:
    build:
      ./mongo-connector
    depends_on:
      - mongo
      - elasticsearch
    environment:
      - WAIT_HOSTS=elasticsearch:9200,mongo:27017

  mongo:
    image: mongo:latest
    ports:
      - "27017:27017"

当我运行docker-compose.yml时,我的应用程序能够连接到Mongo实例。另外,我可以通过终端连接到这个实例。

但是,默认情况下,mongo容器在没有--replSet参数的情况下启动。但我需要使用此选项运行mongo,因为mongo-connector需要启用副本集的实例。

所以我改变了docker-compose.yml:

mongo:
  image: mongo:latest
  entrypoint: ["mongod", "--replSet", "rs0"]
  ports:
    - "27017:27017"

在这种情况下,mongo容器正常启动,我在控制台中看到以下消息:

mongo_1 | 2018-03-03T12:35:27.106+0000 I NETWORK  [initandlisten] waiting for connections on port 27017

尽管如此,我的应用程序无法连接到此实例。来自终端的连接也不起作用。 如果我删除entrypoint: ["mongod", "--replSet", "rs0"],应用程序将照常连接。

你能帮帮我吗,有什么不对吗?

UPD 我执行了docker inspect mongo:latest这是输出(稍微清理一下):

[
    {
        "Id": "sha256:43099507792a5214bd452a0eba3482e8da488b0008525578f309f040c2f7704e",
        "RepoTags": [
            "mongo:latest"
        ],
        "ContainerConfig": {
            "Hostname": "a630f7a68640",
            "Domainname": "",
            "User": "",
            "AttachStdin": false,
            "AttachStdout": false,
            "AttachStderr": false,
            "ExposedPorts": {
                "27017/tcp": {}
            },
            "Tty": false,
            "OpenStdin": false,
            "StdinOnce": false,
            "Env": [
                "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
                "GOSU_VERSION=1.10",
                "JSYAML_VERSION=3.10.0",
                "GPG_KEYS=2930ADAE8CAF5059EE73BB4B58712A2291FA4AD5",
                "MONGO_PACKAGE=mongodb-org",
                "MONGO_REPO=repo.mongodb.org",
                "MONGO_MAJOR=3.6",
                "MONGO_VERSION=3.6.3"
            ],
            "Cmd": [
                "/bin/sh",
                "-c",
                "#(nop) ",
                "CMD [\"mongod\"]"
            ],
            "ArgsEscaped": true,
            "Volumes": {
                "/data/configdb": {},
                "/data/db": {}
            },
            "WorkingDir": "",
            "Entrypoint": [
                "docker-entrypoint.sh"
            ],
            "OnBuild": [],
            "Labels": {}
        },
        "DockerVersion": "17.06.2-ce",
        "Author": "",
        "Config": {
            "Hostname": "",
            "Domainname": "",
            "User": "",
            "AttachStdin": false,
            "AttachStdout": false,
            "AttachStderr": false,
            "ExposedPorts": {
                "27017/tcp": {}
            },
            "Tty": false,
            "OpenStdin": false,
            "StdinOnce": false,
            "Env": [
                "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
                "GOSU_VERSION=1.10",
                "JSYAML_VERSION=3.10.0",
                "GPG_KEYS=2930ADAE8CAF5059EE73BB4B58712A2291FA4AD5",
                "MONGO_PACKAGE=mongodb-org",
                "MONGO_REPO=repo.mongodb.org",
                "MONGO_MAJOR=3.6",
                "MONGO_VERSION=3.6.3"
            ],
            "Cmd": [
                "mongod"
            ],
            "ArgsEscaped": true,
            "Volumes": {
                "/data/configdb": {},
                "/data/db": {}
            },
            "WorkingDir": "",
            "Entrypoint": [
                "docker-entrypoint.sh"
            ]
        },
        "Architecture": "amd64",
        "Os": "linux",
        "Size": 365987009,
        "VirtualSize": 365987009
    }
]

仍然无法弄清楚如何修改默认args并传递--replSet。最初我覆盖了默认入口点,看起来错误的方式。

1 个答案:

答案 0 :(得分:2)

保持入口点不变并改为使用命令:

mongo:
  image: mongo:latest
  command: ["mongod", "--replSet", "rs0"]
  ports:
    - "27017:27017"

如果您查看Dockerfile,您会看到它显示为:

ENTRYPOINT ["docker-entrypoint.sh"]

EXPOSE 27017
CMD ["mongod"]

通过替换entrypoint,你正在替换一个300行长的bash脚本,该脚本应该用实际命令进行一些设置,而命令最初与你的相同,只是没有额外的参数