Mykola Yashchenko
Mykola Yashchenko

Reputation: 5371

Can't connect to docker container with mongo

I have 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"

When I run docker-compose.yml, my application is able to connect to Mongo instance. Also, I can connect to this instance via terminal.

But, by default, mongo container starts without --replSet argument. But I need to run mongo with this option since mongo-connector expects instance with enabled replica set.

So I've changed docker-compose.yml:

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

In this case, mongo container starts normally and I see the following message in the console:

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

Despite that, my application is not able to connect to this instance. The connection from the terminal doesn't work too. If I remove entrypoint: ["mongod", "--replSet", "rs0"], application connects as usual.

Could you please help me, what's wrong?

UPD I've executed docker inspect mongo:latest and that's the output (a little bit cleaned up):

[
    {
        "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
    }
]

Still can't figure out how to modify default args and pass --replSet. Originally I overwrote default entrypoint and looks like that's the wrong way.

Upvotes: 2

Views: 1085

Answers (1)

Oleg Sklyar
Oleg Sklyar

Reputation: 10082

Leave the entrypoint intact and use command instead:

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

If you check the Dockerfile you will see that it reads:

ENTRYPOINT ["docker-entrypoint.sh"]

EXPOSE 27017
CMD ["mongod"]

By replacing the entrypoint you are replacing a 300-line long bash script that is supposedly doing some setup with what the actual command is, while the command is initially the same as yours just without extra arguments

Upvotes: 2

Related Questions