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

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"]

Related

Healthcheck for MongoDB in Dockerfile

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

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/

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

Mapping a Volume in a docker

I am trying to map a Volume to the docker image using -v "/Users//data:/data/"
> docker run -p 27017:27017 --name mongo2_001 -d mongo -v "/Users/<user>/data:/data"
6cf25618d...
> docker ps -a
CONTAINER ID IMAGE COMMAND STATUS PORTS NAMES
6cf25618d.. mongo "/usr/bin/mongod -v /" Exited (1) mongoMG_001
It fails with the following error
> docker logs 6cf25618d..
2016-08-30T01:35:28.197+0000 F CONTROL [main] Failed global
initialization: BadValue: The "verbose" option string cannot
contain any characters other than "v"
When ran with out "-v ..." seems to work properly.
Not sure what could be wrong.
PS: Dockerfile used to create the image used above
FROM ubuntu:16.04
MAINTAINER Docker
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
RUN apt-get update && apt-get install -y mongodb-org
RUN mkdir -p /data/db
WORKDIR /data
EXPOSE 27017
ENTRYPOINT ["/usr/bin/mongod"]
The order of "-v" parameter passed was incorrect.
Instead of going to docker command "-v" options it going to mongo container. Hence reorder the command as shown below
docker run -p 27018:27017 -v /Users/<user>/data:/data --name mongo2_001 -d mongo
On windows
docker run -p 27017:27017 -v /c/Users/<user>/data:/data --name mongo2_001 -d mongo
Mike Tung answer was not working for me. I was able to make it work with following command
docker run --name mongodb_win -v mongoWinData:/data/db -d -p 37017:27017 mongo

Docker doesn't start MONGODB, and IPAddress doesn't appear, when started with other services

I have already asked this question on serverfault.com. I am asking it here too as I see different set of questions in these 2 sites (it appears like they have different databases).
I have been trying to build an OS image from Fedora unsuccessfully to start the following:
Systemd
SSHD
RabbitMQ
MongoDB
I can get the first 3 (Systemd, SSHD and RabbitMQ-Server) to work. I can also get MongoDB to work within the container. However, I cannot get MongoDB to work along with other 3 services.
In addition, IP address doesn't show up when I try to "dockerize" MongoDB.
Am I missing something in the Dockerfile?
Here is my dockerfile:
FROM fedora:20
MAINTAINER “Ashfaque” <ashfaque#email.com>
ENV container docker
RUN yum -y update; yum clean all
RUN yum -y install systemd; yum clean all; \
(cd /lib/systemd/system/sysinit.target.wants/; for i in *; do [ $i == systemd-tmpfiles-setup.service ] || rm -f $i; done); \
rm -f /lib/systemd/system/multi-user.target.wants/*;\
rm -f /etc/systemd/system/*.wants/*;\
rm -f /lib/systemd/system/local-fs.target.wants/*; \
rm -f /lib/systemd/system/sockets.target.wants/*udev*; \
rm -f /lib/systemd/system/sockets.target.wants/*initctl*; \
rm -f /lib/systemd/system/basic.target.wants/*;\
rm -f /lib/systemd/system/anaconda.target.wants/*;
# Dockerizing SSH - is working
RUN yum -y install openssh-server
RUN yum -y install openssh-clients
RUN mkdir /var/run/sshd
RUN systemctl enable sshd.service
RUN echo 'root:mypassword' |chpasswd
EXPOSE 22
# Dockerizing RabbitMQ - is working
RUN yum -y install rabbitmq-server
EXPOSE 5672 15672
RUN systemctl enable rabbitmq-server
# Dockerizing MongoDB - is NOT WORKING
RUN yum -y install mongodb-server
RUN yum -y install boost
RUN yum -y install scons
# Create the MongoDB data directory
RUN mkdir -p /data/db /var/log/mongodb /var/run/mongodb
RUN sed -i 's/dbpath =\/var\/lib\/mongodb/dbpath =\/data\/db/' /etc/mongodb.conf
# 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"]
#CMD ["--port", "27017", "--dbpath", "/data/db", "--smallfiles", "--fork", "--syslog"]
#RUN /usr/bin/mongod --smallfiles --port 27017 --dbpath /data/db --fork --syslog
VOLUME ["/sys/fs/cgroup", "/data/db", "/var/log/mongodb", "/usr/bin"]
CMD ["/usr/sbin/init"]
Docker Commands used to build are:
(1) docker build -t rabbitmq_mongo_heisenbug .
(2) docker run --privileged -d -e 'container=docker' -v /sys/fs/cgroup:/sys/fs/cgroup:ro -p 29022:22 -p 29672:15672 -p 29017:27017 rabbitmq_mongo_heisenbug
or.. (3) docker run --privileged -ti -e 'container=docker' -v /sys/fs/cgroup:/sys/fs/cgroup:ro -p 29022:22 -p 29672:15672 -p 29017:27017 rabbitmq_mongo_heisenbug
You are using both ENTRYPOINT and CMD in your Dockerfile. This means, that docker will run /usr/bin/mongod with default parameter /usr/sbin/init. I'm pretty sure this is not what you want.
Docker will run as long as the command you specified is running. I'm not sure about /usr/bin/mongod, but if it runs in daemon mode (that is, spawn a process and return), then the container will stop running right away. The spawned processes will be terminated. The same is true for /usr/sbin/init or for any other command you specify. You can write a small shell script, which spawns the processes and runs one in the foreground, or you can use runit, or some similar tool.
Also, you probably don't need to run sshd in your container. See here why.