mongodb: unrecognized service in Docker - mongodb

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/

Related

MongoDB connection failed in ubuntu container

I create a docker container with the base image ubuntu:18.04 and installed MongoDB using the shell in this container. Now, I commit and export this container and use this container image as a base image in a new container in which I need the MongoDB status as running for further process. I am using Dockerfile for both containers.
First Dockerfileis :
#getting base image ubuntu
FROM ubuntu:18.04
RUN apt-get update && apt-get install -y apt-utils wget gnupg gnupg2 curl
ENTRYPOINT ["tail", "-f", "/dev/null"]
CMD ["echo","Create base image"]
After building this Dockerfile, I accessed shell installed MongoDB, created a new db and collection, commit and export in the tar file.
Next, I import this tar file as image mongoubuntu:1.0
Now, I create another Dockerfile in which I want to run some commands which need MongoDB status as running.
The second Dockerfile is as follows:
FROM mongoubuntu:1.0
RUN apt-get update && apt-get -y install sudo && apt-get -y install curl
RUN apt-get install -y wget #install wget lib
COPY . /
RUN chmod +x /genesis.sh
RUN /genesis.sh
RUN chmod +x /wallet.sh
RUN /wallet.sh >> /config.sh
ENTRYPOINT ["tail", "-f", "/dev/null"]
CMD ["echo","Install EOS Complete"]
When I run this as docker build -t eossample:1.5 . It gets build successful. When I check the bash shell for the newly created container using this image, MongoDB is not running. If I manually start MongoDB it shows running status. Please help. How could I start MongoDB from DockerFile?

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

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.

Docker Entrypoint for Postgres 9.3

This is my Dockerfile for installing Postgres.
# Set the base image to Ubuntu
FROM ubuntu:14.04
# Update the repository sources list
RUN apt-get update -y
################## BEGIN INSTALLATION ######################
# Install wget
RUN apt-get install wget -y
# Setup Postgres repository
RUN wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc
| sudo apt-key add -
# Add Postgres repository
RUN sh -c "echo "deb http://apt.postgresql.org/pub/repos/apt/ trusty-pgdg main" >> /etc/apt/sources.list.d/postgresql.list"
# Update repository
RUN apt-get update -y
# Install Postgres with Postgis
RUN apt-get install postgresql-9.3-postgis-2.1 -y
How can i add an Entrypoint for Postgres so that Postgres is automatically started in a Docker-container
My solution to start Postgres automatic:
RUN chmod +x /etc/init.d/postgresql
CMD service postgresql start && tail -F /var/lib/postgresql/data/serverlog
You can take ideas from the official docker-library/postgres Dockerfile:
ENTRYPOINT ["/docker-entrypoint.sh"]
EXPOSE 5432
CMD ["postgres"]
They use a docker-entrypoint.sh script which will, at the end, launch postgres
exec gosu postgres "$#"

Mongo DB installation issue

I installed mongo DB by using docs of mongo DB. But when I tried this command in ubuntu terminal, I got this error:
Command used:
sudo mongod
Error:
*********************************************************************
ERROR: dbpath (/data/db) does not exist.
Create this directory or give existing directory in --dbpath.
See http://dochub.mongodb.org/core/startingandstoppingmongo
*********************************************************************
How can I fix this error?
Run this command to create a directory -
sudo mkdir /data/db
and then run
sudo mongod
If you installed mongo as a service (for example, using the following docs: http://docs.mongodb.org/manual/tutorial/install-mongodb-on-ubuntu/), simply execute:
sudo service mongod start
Otherwise, if you simply wish to try mongo without any specific configuration, you can supply a directory for the db files by executing:
mongod --dbpath /path/to/db &
Simple Mongo Setup on AWS Unbuntu 14.0 - By Hrushikesh Kulkarni
sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 7F0CEB10
echo 'deb http://downloads-distro.mongodb.org/repo/ubuntu-upstart dist 10gen' | sudo tee /etc/apt/sources.list.d/mongodb.list
sudo apt-get update sudo apt-get install -y mongodb-org sudo apt-get install php5 sudo apt-get install apache2 sudo apt-get install libsasl2-dev sudo apt-get install php-pear php5-dev sudo pecl install mongo
Add Below Stuff in /etc/php5/apache2/php.ini
extension_dir = "/usr/lib/php5/20121212" extension=mongo.so sudo service apache2 restart
Create php File in /var/www/html Run file in browser and find Extension "Mongo"
If you have created Test123 folder then
Run following command to restart the mongodb service
sudo service mongodb restart
Then change the db for the mongodb
Use following command
sudo mongod --dbpath Test123
Here Test123 is directory that you have created on specific location.