0

First of all there, I know would be easier ways to solve my problem but in order to get a better understanding of docker, this is the way I want to solve running a dockerized mongodb.

I am right now trying to build my own mongodb docker image by taking a Dockerfile that's already there and modifying it. The modification: I want to be able to access a mongodb.conf file that the mongodb-server uses.

This is my Dockerfile. I only added one line VOLUME ["/etc"] and I changed the last line (I added the last -f parameter). Building the image and running it in a container worked fine without those changes.

FROM debian:jessie-slim
RUN apt-get update && \
apt-get install -y ca-certificates && \
rm -rf /var/lib/apt/lists/*
RUN gpg --keyserver ha.pool.sks-keyservers.net --recv-keys 0C49F3730359A14518585931BC711F9BA15703C6 && \
gpg --export $GPG_KEYS > /etc/apt/trusted.gpg.d/mongodb.gpg
ARG MONGO_PACKAGE=mongodb-org
ARG MONGO_REPO=repo.mongodb.org
ENV MONGO_PACKAGE=${MONGO_PACKAGE} MONGO_REPO=${MONGO_REPO}
ENV MONGO_MAJOR 3.4
ENV MONGO_VERSION 3.4.18
RUN echo "deb http://$MONGO_REPO/apt/debian jessie/${MONGO_PACKAGE%-unstable}/$MONGO_MAJOR main" | tee "/etc/apt/sources.list.d/${MONGO_PACKAGE%-unstable}.list"
RUN echo "/etc/apt/sources.list.d/${MONGO_PACKAGE%-unstable}.list"
RUN apt-get update
RUN apt-get install -y ${MONGO_PACKAGE}=$MONGO_VERSION
VOLUME ["/data/db"]
VOLUME ["/etc"]
WORKDIR /data
EXPOSE 27017
CMD ["mongod", "--smallfiles", "-f /etc/mongodb.conf"]

With my changes however, the builded image can not start. It immediately exits. docker logs <container-name> gives me

Error reading config file: No such file or directory
try 'mongod --help' for more information

I used the run command docker run -p 37017:27017 -d -v /tmp/mongodb/data:/data/db -v /tmp/mongodb/etc:/etc mongo-image and expected that the file located at /tmp/mongodb/etc/mongodb.conf on my host-system would be found then (yes, it exists there, I placed it there).

What am I doing wrong? I am new to docker and I think I messed up the volumes maybe?

divby0
  • 67
  • 1
  • 10

2 Answers2

4

For anyone looking for a quick guide on how to install dockerized mongo locally in 2019 and ending up with problems like that one mentioned in this question (which just happened to me 1 hour ago), I wrote a quick "guide": https://yosh.ke.mu/mongo_in_docker

Hope it would be useful for somebody.

Muhammad Tariq
  • 3,318
  • 5
  • 38
  • 42
Roman Pietrzak
  • 635
  • 3
  • 15
0

You don't need to specify these lines:

VOLUME ["/data/db"]
VOLUME ["/etc"]

The VOLUME in your dockerfile creates separate directory on your host under /var/lib/docker/volumes For more info check this.

-v option in your docker run command should be enough to mount mongodb.conf on your host inside the container.

Update I made a slight change in your dockerfile and it start working. CMD ["mongod", "--smallfiles", "-f", "/etc/mongodb.conf"]

The -f and /etc/mongodb.conf should be separated by a comma, that is the syntax of CMD.

Please try this at your end and let me know.

mchawre
  • 10,744
  • 4
  • 35
  • 57
  • Why was the VOLUME["/data/db"] then there? I read that it was for persistence. My Config should also be persistent. Can you explain a bit further please? – divby0 Jun 16 '19 at 13:44
  • I updated my answer please check. And if it answers your question then please upvote and accept my answer. :) – mchawre Jun 16 '19 at 13:58
  • Understood. But this does not explain why my config file is not found, does it? – divby0 Jun 16 '19 at 14:07
  • In your case `VOLUME` has come into the picture and the data inside `/var/lib/docker/volumes` dir is the one getting mounted and since there is no mongodb.conf there hence you got `no such file or directory` – mchawre Jun 16 '19 at 14:10
  • Why is `/var/lib/docker/volumes`the one getting mounted and not my specified `/tmp/mongodb/etc`? Because after starting I can see that a few files get generated there. – divby0 Jun 16 '19 at 14:22
  • I guess I was wrong your `-v` option is taking precedence and hence the files are getting generated. I updated my answer there is a syntax problem in your Dockerfile. Please check my answer again. – mchawre Jun 16 '19 at 14:24
  • After your last update it worked! The syntax for CMD was wrong, it works now with the `-f` and `/etc/mongodb.conf` separated! Thank you very much. – divby0 Jun 16 '19 at 14:27