1

I am trying to use the official MongoDB docker image to create a 3-set replication cluster inside a single container.

following this official guide I created the following dockerfile:

  FROM mongo:3.2
    RUN mkdir -p /srv/mongodb/rs0-0 /srv/mongodb/rs0-1 /srv/mongodb/rs0-2
    CMD mongod --port 27017 --dbpath /srv/mongodb/rs0-0 --replSet rs0 --smallfiles --oplogSize 128 &
    CMD mongod --port 27018 --dbpath /srv/mongodb/rs0-1 --replSet rs0 --smallfiles --oplogSize 128 &
    CMD mongod --port 27019 --dbpath /srv/mongodb/rs0-2 --replSet rs0 --smallfiles --oplogSize 128 &
    CMD mongo --port 27017 --eval 'rs.initiate({_id: "rs0",members: [{_id: 0,host: "localhost:27017"}]})'
    CMD mongo --port 27017 --eval "printjson(rs.conf())"
    CMD mongo --port 27017 --eval 'rs.add("localhost:27018")'
    CMD mongo --port 27017 --eval 'rs.add("localhost:27019")'
    CMD mongo --port 27017 --eval "printjson(rs.status())"

but when I run it, I get the following error:

  MongoDB shell version: 3.2.9
    connecting to: 127.0.0.1:27017/test
    2016-09-04T06:53:06.664+0000 W NETWORK  [thread1] Failed to connect to 127.0.0.1:27017, reason: errno:111 Connection refused
    2016-09-04T06:53:06.664+0000 E QUERY    [thread1] Error: couldn't connect to server 127.0.0.1:27017, connection attempt failed :
    connect@src/mongo/shell/mongo.js:231:14
    @(connect):1:6
    exception: connect failed

When I run the original mongo image and try to run the same CMDs I don't get the same.

I tried adding USER root to the Dockerfile but that didn't help at all.

What can I do to make this work?

UPDATES

  • Also tried to create an image starting from ubuntu, same result
  • Well, this seems to be some sort of docker file permission issues. When I moved the code to a bash script and run it from there, everything worked fine. this is not an answer to why this happens but a solution if you need it.
Vince Bowdren
  • 8,326
  • 3
  • 31
  • 56
Gleeb
  • 10,773
  • 26
  • 92
  • 135

1 Answers1

0

Your dockerfile isn't working because you've put in more than one CMD, which is invalid.

The documentation says:

There can only be one CMD instruction in a Dockerfile. If you list more than one CMD then only the last CMD will take effect.

Vince Bowdren
  • 8,326
  • 3
  • 31
  • 56