Healthcheck for MongoDB in Dockerfile - mongodb

I am trying to create a Healthcheck for my MongoDB container configured in my Dockerfile:
FROM ubuntu
RUN apt-get update && apt-get install -y gnupg2
RUN apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 7F0CEB10
RUN echo 'deb http://downloads-distro.mongodb.org/repo/ubuntu-upstart dist 10gen' > tee /etc/apt/sources.list.d/mongodb.list
RUN apt-get update
RUN apt-get install -y mongodb
RUN mkdir -p /data/db
EXPOSE 27017
**HEALTHCHECK --interval=5s --timeout=3s CMD /etc/init.d/mongodb status || exit 1**
CMD ["usr/bin/mongod", "--smallfiles"]
But when I build the image and run a container, after running docker ps it shows Up 20 seconds (unhealthy) in the status column.
Going into the container with bash, when I try running service mongodb start it fails.
In the log file (/var/log/mongodb/mongodb.log) it says Failed to set up listener: SocketException: Address already in use
But there is no other container with MongoDB running.
What could be causing this?

Maybe a mistake on last line
Change this line CMD ["usr/bin/mongod", "--smallfiles"]
to
CMD ["/usr/bin/mongod", "--smallfiles"]
Update1:
change this line
**HEALTHCHECK --interval=5s --timeout=3s CMD /etc/init.d/mongodb status || exit 1**
to
HEALTHCHECK --interval=5s --timeout=3s CMD /etc/init.d/mongodb status || exit 1

Related

mongodb: unrecognized service in Docker

I created my own Docker container that includes the latest version of ubuntu, python3.7 and mongodb.
Dockerfile
FROM ubuntu:latest
MAINTAINER Docker
# Update apt-get sources AND install MongoDB
RUN apt-get update && apt-get upgrade -y
RUN apt-get install -y software-properties-common
RUN apt install -y gnupg2
RUN gpg2 --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys F3B1AA8B
# Installation:
RUN add-apt-repository ppa:deadsnakes/ppa
RUN apt-get install -y python3.7
#Mongodb
RUN apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 0C49F3730359A14518585931BC711F9BA15703C6
RUN apt-add-repository 'deb [ arch=amd64,arm64 ] http://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/3.4 multiverse'
RUN apt-get update
RUN apt-get install -y mongodb-org
# Create the MongoDB data directory
RUN mkdir -p /data/db
# Create the MongoDB data directory
RUN mkdir -p /data/code
RUN mongod --version
RUN mongod --dbpath /data/db --fork --logpath /data/db/log
# COPY some Code to Container
COPY dev /data/code
# Installing pip for python modules
RUN apt-get install -y python3-pip
# Install modules
WORKDIR /data/code/
RUN pip3 install -r requirements.txt
RUN service mongodb start
RUN python3 main.py
RUN python3 server.py
EXPOSE 80
# Set /bin/bash as the dockerized entry-point application
ENTRYPOINT ["/bin/bash"]
when I run the build command:
docker build -t myContainer --no-cache .
it runs successfully till to the point where mongodb should start as a service
.
.
.
Removing intermediate container 3d43e1d1cd96
---> 62f10ce67e07
Step 21/25 : RUN service mongodb start
---> Running in 42e08e7d7638
mongodb: unrecognized service
How do I start the service? I'm trying to start the service with the command: service mongodb start. Isn't that correct? And what does the line:
Removing intermediate container 3d43e1d1cd96
means?
Firstly, it should be service mongod start i guess. But this is not going to solve your problem.
While using Docker, your process has to be a foreground process service mongod start will go into background & your container will exit immediately.
You should use mongod foreground process as below -
CMD ["mongod"]
Put the above CMD at the end of Dockerfile to make sure your container runs mongod.
Official Dockerfile -
https://github.com/docker-library/mongo/blob/40056ae591c1caca88ffbec2a426e4da07e02d57/3.4/Dockerfile
If you want to run multiple processes, use docker ENTRYPOINT in conjunction with supervisord or use a wrapper script.
Ref - https://docs.docker.com/config/containers/multi-service_container/

Cannot connect to MongoDB in Docker container from the host machine.

I have following image:
FROM ubuntu:16.04
RUN apt-get update
RUN apt-get install apt-transport-https -y
RUN apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 2930ADAE8CAF5059EE73BB4B58712A2291FA4AD5
RUN echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/3.6 multiverse" | tee /etc/apt/sources.list.d/mongodb-org-3.6.list
RUN apt-get update
RUN apt-get install -y mongodb-org=3.6.2 mongodb-org-server=3.6.2 mongodb-org-shell=3.6.2 mongodb-org-mongos=3.6.2 mongodb-org-tools=3.6.2
RUN mkdir -p /data/db
EXPOSE 27017
CMD ["--port 27017", "--smallfiles"]
ENTRYPOINT usr/bin/mongod
Run it with:
docker run --rm -ti --security-opt=seccomp:unconfined -p27017:27017 -p28017:28017 --name mong --rm mong
I see following Warning after run it:
WARNING: This server is bound to localhost.
Remote systems will be unable to connect to this server.
Start the server with --bind_ip <address> to specify which IP
addresses it should serve responses from, or with --bind_ip_all to
bind to all interfaces. If this behavior is desired, start the
server with --bind_ip 127.0.0.1 to disable this warning.
I tried to add CMD ["--bind_ip_all"] right before entrypoint but it didn't help. How to expose MongoDB to hostmachine?
Try with: --bind_ip 0.0.0.0
Your CMD becomes:
CMD ["--port 27017", "--smallfiles", "--bind_ip", "0.0.0.0"]

Mongodump with authenticationMechanism SCRAM-SHA-1

I have following mongo docker image:
FROM ubuntu
RUN apt-get update
RUN apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 7F0CEB10
RUN echo 'deb http://downloads-distro.mongodb.org/repo/ubuntu-upstart dist 10gen' | tee /etc/apt/sources.list.d/mongodb.list
RUN apt-get update
RUN apt-get install -y mongodb-10gen
RUN mkdir -p /data/db
EXPOSE 27017
CMD ["--port 27017", "--smallfiles"]
ENTRYPOINT usr/bin/mongod
After launching container and attaching to it using bash I try to dump remote db with following command.
mongodump --host *.*.*.* -port 27017 -u user -p pass --authenticationDatabase admin --authenticationMechanism SCRAM-SHA-1 --out /tmp/backup/mongodump.json
I get exception:
connected to: *.*.*.*:27017
assertion: 2 SASL authentication support not compiled into client library.
Should I add some libraries into my image?
Changed Dockerfile to install latest Mongo version, since SCRAM-SHA-1 was introduced in v3.0
FROM ubuntu:16.04
RUN apt-get update
RUN apt-get install apt-transport-https -y
RUN apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 2930ADAE8CAF5059EE73BB4B58712A2291FA4AD5
RUN echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/3.6 multiverse" | tee /etc/apt/sources.list.d/mongodb-org-3.6.list
RUN apt-get update
RUN apt-get install -y mongodb-org=3.6.2 mongodb-org-server=3.6.2 mongodb-org-shell=3.6.2 mongodb-org-mongos=3.6.2 mongodb-org-tools=3.6.2
RUN mkdir -p /data/db
EXPOSE 27017
CMD ["--port 27017", "--smallfiles"]
ENTRYPOINT usr/bin/mongod

syntax error near unexpected token "(" while executing shell script for mongo db

I'm trying to create a Dockerfile for managing mongo db and its default content.
here is docker file and related shell scripts -
DockerFile -
# Dockerizing MongoDB: Dockerfile for building MongoDB images
# Based on ubuntu:16.04, installs MongoDB following the instructions from:
# http://docs.mongodb.org/manual/tutorial/install-mongodb-on-ubuntu/
FROM ubuntu:16.04
# Installation:
# Import MongoDB public GPG key AND create a MongoDB list file
RUN apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv EA312927
RUN echo "deb http://repo.mongodb.org/apt/ubuntu $(cat /etc/lsb-release | grep DISTRIB_CODENAME | cut -d= -f2)/mongodb-org/3.2 multiverse" | tee /etc/apt/sources.list.d/mongodb-org-3.2.list
# Update apt-get sources AND install MongoDB
RUN apt-get update && apt-get install -y mongodb-org
# Create the MongoDB data directory
RUN mkdir -p /data/db
COPY ./scripts /data/scripts
COPY ./run.sh /data/init/run.sh
RUN chmod +x /data/init/run.sh
RUN chmod +x /data/scripts/*
RUN /data/init/run.sh
# Expose port #27017 from the container to the host
EXPOSE 27017
# Set /usr/bin/mongod as the dockerized entry-point application
ENTRYPOINT [ "/usr/bin/mongod", "--rest"]
Run.sh -
# Start mongo db
/usr/bin/mongod --fork --logpath /data/mongodb.log
# Run all Create scripts
# FILES= data/scripts/*-create.js
# for f in $FILES; do /usr/bin/mongo 127.0.0.1:27017/demo $f; done
cd data/scripts
# Run all Insert scripts
FILES= ./*-insert.js
for f in $FILES; do /usr/bin/mongo 127.0.0.1:27017 $f; done
#Stop mongo db
/usr/bin/mongod --shutdown
and default-insert.js -
var db = db.getSiblingDB('demo')
db.names.insert({'name' : 'Manoj Shevate', 'id':1});
when I'm building docker image, I'm getting following error on first line of default-insert.js
syntax error near unexpected token "("
Not sure what exactly going wrong here.. need some expert advice!
Space--the final frontier. At least this looks fishy:
# Run all Insert scripts
FILES= ./*-insert.js
It makes FILES empty and tries to run the js file as a shell script, causing the error message (it's a shell one-shot variable assignment command like EDITOR=vi crontab -e). Use this instead:
for f in *-insert.js; do /usr/bin/mongo 127.0.0.1:27017 "$f"; done

Unable to start mongodb using supervisor file

I've got a docker container, in which I'm installing mongo db. After installing,
I'm trying to start mongo and restore a mongo db dump. However, when I start the docker instance, I see that the user has been switched to root (as per supervisor instruction) but mongo is not started.
This is the supervisor snippet:
[supervisord]
nodaemon=true
[program:mongodb]
user=root
command=/usr/bin/mongod
This is my setup in the dockerfile:
RUN apt-get update && sudo apt-get install -y supervisor
RUN mkdir -p /var/log/supervisor
COPY supervisor.conf /etc/supervisor/conf.d/supervisor.conf
# Install MongoDB.
RUN \
apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 7F0CEB10 && \
echo 'deb http://downloads-distro.mongodb.org/repo/ubuntu-upstart dist 10gen'
| tee /etc/apt/sources.list.d/mongodb.list && \
apt-get update && \
apt-get install -y mongodb-org && \
rm -rf /var/lib/apt/lists/*
# Define mountable directories.
VOLUME ["/data/db"]
# Define working directory.
WORKDIR /data
# Define default command.
CMD ["mongod"]
EXPOSE 27017
EXPOSE 28017
CMD ["/usr/bin/supervisord", "-c", "/etc/supervisor/supervisord.conf"]
Am I missing any configuration setting? Any help is appreciated.
You're not able to run mongodb because while installing mongodb some files need authentication, so you just have to replace apt-get install -y mongodb-org with apt-get install -y --no-authentication mongodb-org and you'll be able to install mongodb without any problem.