Cannot connect to mongodb container from linux host - mongodb

I have the following Dockerfile that only installs mongodb 3
# Start with docker's base ubuntu image
FROM ubuntu:14.04.2
# Mongodb prerequisite
RUN apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 7F0CEB10
RUN echo "deb http://repo.mongodb.org/apt/ubuntu "$(lsb_release -sc)"/mongodb-org/3.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.0.list
# Update package sources
RUN apt-get -y update
# Install Mongodb binaries
RUN apt-get -y install mongodb-org
# Copy configuratio file
COPY mongod.conf /etc/mongod.conf
# Create mongo data path (will be mapped to a volume on host machine later on)
RUN mkdir -p /data/db
RUN chown -R mongodb:mongodb /data/db
# Expose MongoDB port
EXPOSE 27017
# Run mongo with mongodb user
USER mongodb
# Run mongod using provided configuration file
ENTRYPOINT ["/usr/bin/mongod"]
CMD ["--config", "/etc/mongod.conf"]
I create the image with
sudo docker build -t mongod .
I run the container with
sudo docker run -d -P mongod
And verify it's started
> sudo docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
43c75e7e44b3 mongod:latest "/usr/bin/mongod --c 3 minutes ago Up 3 minutes 0.0.0.0:49165->27017/tcp silly_mccarthy
When I run a mongo client from the container, it can connect without any error:
> sudo docker exec -ti 43c75e7e44b3 bash
$ mongo
MongoDB shell version: 3.0.1
connecting to: test
Welcome to the MongoDB shell.
....
My host is a Ubuntu 14.04 box and the network interfaces / bridges are
docker0 Link encap:Ethernet HWaddr 63:54:7b:f3:47:33
inet addr:172.17.42.1 Bcast:0.0.0.0 Mask:255.255.0.0
....
em1 Link encap:Ethernet HWaddr c4:2f:e3:64:ae:7c
inet addr:192.168.1.101 Bcast:192.168.1.255 Mask:255.255.255.0
....
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
....
From my host, I cannot connect to the mongod container
> mongo --port 49165
MongoDB shell version: 3.0.1
connecting to: 127.0.0.1:49165/test
2015-03-21T18:14:55.936+0100 I NETWORK Socket recv() errno:104 Connection reset by peer 127.0.0.1:49165
2015-03-21T18:14:55.936+0100 I NETWORK SocketException: remote: 127.0.0.1:49165 error: 9001 socket exception [RECV_ERROR] server [127.0.0.1:49165]
2015-03-21T18:14:55.936+0100 I NETWORK DBClientCursor::init call() failed
2015-03-21T18:14:55.937+0100 E QUERY Error: DBClientBase::findN: transport error: 127.0.0.1:49165 ns: admin.$cmd query: { whatsmyuri: 1 }
at connect (src/mongo/shell/mongo.js:181:14)
at (connect):1:6 at src/mongo/shell/mongo.js:181
exception: connect failed
Is there something I'm missing here ?

MongoDB's default configuration is to bind mongod to the IP of localhost. Search for the following line in your mongod.conf:
bindIp = 127.0.0.1
If you comment this line out using a hash like this
# bindIp = 127.0.0.1
then mongod will bind to all IPs available during startup. If you set a specific IP like this
bindIp = 192.168.0.42
then mongod binds only to this specific IP address and will only be available to hosts which can access that IP on the specified port.

Related

MongoDB in Linux Mint 19

I am trying to use mongoDB in Linux mint 19. But I am getting below error.
What is the solution ?
You're starting the mongo shell client, which by default tries to connect to a mongod server running on localhost and listening on port 27017. However, there is no server listening there so the mongo shell notes that and exits.
You'll need to investigate why there was no mongod server listening there:
The MongoDB Server package isn't installed
You're specifying a non-default port in e.g. /etc/mongod.conf
The mongod process/service is not running
...
First verify that the Server package is installed:
sudo dpkg -l | grep -i ".*mongodb.*server"
Then check the status of the mongod service and go from there. That will tell you if it's running, what port it's listening on, what config file is used, etc.
sudo systemctl status mongod
If it's not running then try to start it:
sudo systemctl start mongod
If it's still not running then look at the log messages:
sudo journalctl -u mongod.service

CoreOS: dockerized mongo instance connection refused from another dockeried mongo

I've set up two services on my CoreOS instance. Briefly, the first one is an official mongo container and the other one is a custom made image that's trying to connect to a "mongo" instance.
- name: living-mongo.service
command: start
enable: true
content: |-
[Unit]
Description=Mongo
Author=Living
After=docker.service
[Service]
Restart=always
RestartSec=10s
ExecStartPre=-/usr/bin/docker stop mongo
ExecStartPre=-/usr/bin/docker rm mongo
ExecStart=/usr/bin/docker run --name mongo -p 27017:27017 --hostname mongo mongo:2.6
ExecStop=/usr/bin/docker stop mongo
ExecStopPost=-/usr/bin/docker rm mongo
- name: living-mongo-seed.service
command: start
enable: true
content: |-
[Unit]
Description=Mongo Seed
Author=Living
Requires=living-mongo.service
After=living-mongo.service
[Service]
User=core
Type=oneshot
ExecStartPre=-/usr/bin/docker stop mongo-seed
ExecStartPre=-/usr/bin/docker rm mongo-seed
ExecStart=/usr/bin/docker run --name mongo-seed --link mongo:mongo registry.living-digital-way.com/mongo-qa:v1
ExecStop=/usr/bin/docker stop mongo-seed
Basiclly, first I start the mongo instance, and then I'm trying to connect to it to feed some data on there:
# docker run --name mongo -p 27017:27017 --hostname mongo mongo:2.6
# docker run --name mongo-seed --link mongo:mongo registry.living-digital-way.com/mongo-qa:v1
When the second service is started, it's telling me:
Sep 12 14:12:21 core-01 docker[1672]: Status: Downloaded newer image for registry.living-digital-way.com/mongo-qa:v1
Sep 12 14:12:21 core-01 docker[1672]: 2016-09-12T14:12:21.704+0000 warning: Failed to connect to 172.17.0.4:27017, reason: errno:111 Connection refused
Sep 12 14:12:21 core-01 docker[1672]: couldn't connect to [mongo] couldn't connect to server mongo:27017 (172.17.0.4), connection attempt failed
Sep 12 14:12:21 core-01 docker[1672]: 2016-09-12T14:12:21.728+0000 warning: Failed to connect to 172.17.0.4:27017, reason: errno:111 Connection refused
Sep 12 14:12:21 core-01 docker[1672]: couldn't connect to [mongo] couldn't connect to server mongo:27017 (172.17.0.4), connection attempt failed
Once the system is started, I manually perform the second service on shell:
docker run --name mongo-seed --link mongo:mongo registry.living-digital-way.com/mongo-qa:v1
and it works fine.
What am I doing worng?
EDIT
Custom docker image Dockerfile:
FROM mongo:2.6
MAINTAINER Living Digital Way
COPY ./clients.init.json .
COPY ./users.init.json .
COPY ./import.sh .
RUN ["chmod", "+x", "./import.sh"] # -> only required, if import.sh is not executable
CMD ["./import.sh"]
and import.sh:
mongoimport --host mongo --db lvdb --collection clients --type json --file ./clients.init.json --jsonArray --upsert --upsertFields client_id
mongoimport --host mongo --db lvdb --collection users --type json --file ./users.init.json --jsonArray --upsert --upsertFields username
You need to figure out where the connection is being dropped.
Start with inspecting the mongo container to ensure the public IP is correct.
docker inspect mongo_container | egrep "IPAddress[^,]+"
This will print the IP address - ensure this is correct.
Stand up the Container as a daemon (if not already)
docker run -it -d mongo
Get container ID
docker ps -a
Log into container
docker exec -it mongo_container_id bash
Install curl
sudo apt-get update
sudo apt-get curl
Ping MongoDB Endpoint
curl localhost:27017
Should print out
It looks like you are trying to access MongoDB over HTTP on the native driver port.
control + d to exit the container.
Run the same commands while container is still up but instead of localhost use the IP from this command.
docker inspect mongo_container | egrep "IPAddress[^,]+"
curl 172.17.4.124:27017
Should see the same output
It looks like you are trying to access MongoDB over HTTP on the native driver port.
Either the IP, the port, the port exposure, the server, or the container is not up.

Mongo Shell couldn't connect to the local server

I'm starting MongoDB using the following command:
sumeet#sumeet-acer:~$ sudo service mongod start
I get a reply as:
mongod start/running, process 7209
sumeet#sumeet-acer:~$
But when I try to enter MongoDB shell by typing mongo I get the following error:
sumeet#sumeet-acer:~$ mongo
MongoDB shell version: 2.0.4
connecting to: test
Fri Jun 12 14:01:59 Error: couldn't connect to server 127.0.0.1 shell/mongo.js:84
exception: connect failed
By default, mongod and mongos use the 27017 port. Without any option, the client will try to connect to a server running on localhost and listening at that port.
A few general advices here:
check if the mongod server is really running (ps -edf | grep mongod);
check if it uses the default port (look in the config file1, or use nmap localhost -p0-65535 or netstat | grep 27017);
if mongod is not running, check in the log2 for clues about what goes wrong.
1 /etc/mongodb.conf (on Debian-like systems at least)
2 /var/log/mongodb (on Debian-like systems at least)
The default IP and Port for MongoDB is 127.0.0.1 & 27017.
If you are using config file for specifying settings than re-verify IP & Port mentioned in it.
If you are going with default option than mongod should function as expected.
try mongo 127.0.0.1:27017
this worked for me
For MongoDB 2.2.2 running on Ubuntu 12.10, it's in /var/lib/mongodb/mongod.lock
and after that running the repair command
sudo -u mongodb mongod --repair --dbpath /var/lib/mongodb/
courtesy: https://stackoverflow.com/a/13700262/4907105

Mongo DB installation on RHEL 5.11 x64

I am trying to install MongoDB 3.0.0 on one of the sandbox which has RHEL Linux 5.11 64bit
I was able to download all the packages using yum command mentioned in the official document here MongoDB Installation.
I am trying to run following command as per the documentation
semanage port -a -t mongod_port_t -p tcp 27017
and it is giving me below error.
/usr/sbin/semanage port -a -t mongod_port_t -p tcp 27017
libsepol.context_from_record: type mongod_port_t is not defined
libsepol.context_from_record: could not create context structure
libsepol.port_from_record: could not create port structure for range 27017:27017 (tcp)
libsepol.sepol_port_modify: could not load port range 27017 - 27017 (tcp)
libsemanage.dbase_policydb_modify: could not modify record value
libsemanage.semanage_base_merge_components: could not merge local modifications into policy
/usr/sbin/semanage: Could not add port tcp/27017
I have root access on this sandbox but I am really clueless what this means. How can I install MongoDB successfully?
Step 1: Configure the package management system(yum)
Sudo vi /etc/yum.repos.d/mongodb-org-3.4.
so that you can install MongoDB directly, using yum.
Use the following repository file:
[mongodb-org-3.4]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-
org/3.4/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-3.4.asc
Step 2: Use the below command to install MongoDB
Sudo yum install -y mongodb-org
Step 3:Start Mongod Service
Sudo service mongod start
Step 4: Then start Mongo with the command below
Mongo
Now Create your database as use
And then start building collections and insert documents into in the collection
For More detail vist https://docs.mongodb.com/master/tutorial/install-mongodb-on-red-hat/
By default this port is already authorized:
#### # semanage port -l | grep mongod_port_t
mongod_port_t tcp **27017**-27019, 28017-28019

docker: show open ports from linked container

If I inspect the official mongo docker image, I can see that it exposes port 27017
$ docker inspect mongo
...
"ExposedPorts": {
"27017/tcp": {}
},
...
I have run the image, binding the internal port to the same on my host:
$ docker run -p 27017:27017 -d --name db mongo
I now run my own image in interactive mode, launching bash
$ docker run -i -t --link db:db_1 cd9b5953b633 /bin/bash
In my dockerized container, if I try to show open ports, nothing is listening.
$ netstat -a
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State
Active UNIX domain sockets (servers and established)
Proto RefCnt Flags Type State I-Node Path
What am I doing wrong here? How can I connect from my dockerized container to the mongo container?
If it is of some use, here is my Dockerfile:
# https://registry.hub.docker.com/u/dockerfile/nodejs/ (builds on ubuntu:14.04)
FROM dockerfile/nodejs
MAINTAINER My Name, me#email.com
ENV HOME /home/web
WORKDIR /home/web/site
RUN useradd web -d /home/web -s /bin/bash -m
RUN npm install -g grunt-cli
RUN npm install -g bower
RUN chown -R web:web /home/web
USER web
RUN git clone https://github.com/repo/site /home/web/site
RUN npm install
RUN bower install --config.interactive=false --allow-root
ENV NODE_ENV development
# Port 9000 for server
# Port 35729 for livereload
EXPOSE 9000 35729
CMD ["grunt"]
Docker create a Network namespace, so within your container, you will not see the exposed port of the host.
In your usecase, you do not need to run mongo with -p if you just need to access it from an other container. The --link will simply "inject" the linked container info as environement variable.
From your new container, you can do env to see the list, and you will have something like DB_1_PORT_27027_TCP_ADDR with the private IP of the mongo container where you can connect.