2

I try to set up a mongodb in a Dockerfile.
This is from my dockerfile which is based on Debian:

...mongoDB is installed here
#start the mongoDB
RUN mongod --fork --logpath /var/log/mongod.log
RUN mongo --eval "db.getSiblingDB('db')"

When I try to build this Dockerfile I get the following error:

connecting to: mongodb://127.0.0.1:27017
W NETWORK  [thread1] Failed to connect to 127.0.0.1:27017, in(checking socket for error after poll), reason: Connection refused
E QUERY    [thread1] Error: couldn't connect to server 127.0.0.1:27017, connection attempt failed : connect@src/mongo/shell/mongo.js:251:13
@(connect):1:6
exception: connect failed

It would be great, if anyone knew how to fix this issue.

  • Have a look at [this](https://stackoverflow.com/questions/34711642/docker-mongo-image-connection-refused-from-other-container/35987734) SO question. – Jeroen Heier Jan 17 '18 at 19:44

1 Answers1

0

The problem is related to how Docker processes the Dockerfile. Each statement in the Dockerfile is executed in a container, which was created from the image of the previous line. In case of the RUN statement, Docker runs the code in a container and creates a new image based on the current state of the container. The image does not contain any information about running processes.

In your case, this means you have an image, which contains the mongodb installation. Let's call it A (instead of hash values from the docker build log). The first RUN statement

RUN mongod --fork --logpath /var/log/mongod.log

is processed in a container, let's call it a, which is based on image A. The process mongod in a probably appends a few lines to the log file. Once the RUN statement has been processed, the container's file system is frozen and labeled as image B. A Docker image does not contain any information about running processes. In fact, at this point, there is not mongod process running anymore. However, image B contains the log entries from this previous RUN.

The second RUN statement

RUN mongo --eval "use db"

is executed in container b, which is based on image B. This commands fails because there is no running process to which mongo could talk to.

I think there are two solutions to your problem:

  1. In general, you can run both scripts in one step

    RUN mongod --fork --logpath /var/log/mongod.log && mongo --eval "use db"

    or move them to a bash script, such that they are executed in the same container.

  2. Use the official mongo image on Docker hub.

sauerburger
  • 4,569
  • 4
  • 31
  • 42
  • Thanks a lot, the first solution works perfectly. I can run the commands chained so I don't have to use an extra bash-script. – Bernward Weigand Jan 17 '18 at 22:50
  • To finally get a database the full command is `mongod --fork --logpath /var/log/mongod.log && mongo --eval "db.getSiblingDB('db')"`. I was also wrong with second command mongo shell helpers won't work here. – Bernward Weigand Jan 17 '18 at 23:41
  • Have same issue. I have FROM mongo RUN apt-get update && apt-get -y install cron awscli ADD run.sh /run.sh CMD /run.sh within Docker file and when container is up the mongodb does not start and i get the aforementioned error. Can you help? – branko terzic Feb 10 '18 at 09:23
  • @brankoterzic What is in your `run.sh`? Does it start the mongodb daemon? – sauerburger Feb 19 '18 at 16:19
  • @sauerburger No it does not. It is a script which supports mongo backup and cron job specification. – branko terzic Feb 20 '18 at 07:27
  • 1
    The official [Dockerfile](https://github.com/docker-library/mongo/blob/a504b49bb5cf896fbf3640b4b8cb0d09a25b53ae/3.6/Dockerfile) contains `CMD ["mondod"]`. So when you start a container, it starts the mongodb daemon by default. If you overwrite the `CMD` in your Dockerfile, you need to start the database daemon yourself. Or, maybe you can execute `run.sh` at build-time using `RUN /run.sh` and leave the default `CMD` in-place. – sauerburger Feb 20 '18 at 08:28