I'm trying to build an easy Dockerfile to Copy files from current directory into container then run a mongorestore command to seed the data. I've looked at many different websites and I'm still getting the following error.
2016-08-17T03:03:22.639+0000 Failed: error connecting to db server: no reachable servers
The command '/bin/sh -c mongorestore --drop /mongo-seed/mongo-seed-data/mongo-dump --host 127.0.0.1:27017' returned a non-zero code: 1
When I "bash" into the container and run the mongorestore command with the same parameters it populates database. I'm at a loss, please help.
Below is the Dockerfile
FROM mongo
COPY . /mongo-seed
EXPOSE 27017
CMD ["mongod"]
RUN mongorestore --drop /mongo-seed/mongo-seed-data/mongo-dump --host 127.0.0.1:27017
CMD is run when you start up the container.
So mongod is not running when docker executes the last RUN instruction of your dockerfile while building the image.
FROM mongo
COPY . /mongo-seed
# EXPOSE 27017 //not necessary, the mongo base image already has that instruction
ENTRYPOINT mongod
Build and run: docker build -t foo . && docker run -d --name bar foo
Execute the mongorestore command:
docker exec bar mongorestore --drop ...
Related
I am running a container with image mongo:latest. Starting the container:
sudo docker run -it -v /tmp/adhock-container/mongo_latest:/tmp/ mongo:latest /bin/bash
I want to take the backup of Mongo database so I run the following:
# mongodump -vvvv --host MY_HOSTNAME --port 27017 --username MY_USERNAME --password MY_PASSWORD --gzip --archive=ARCHIVE_PATH
In the console log I am getting only the following output:
2020-04-06T08:00:08.007+0000 done dumping ******** (1104 documents)
2020-04-06T08:00:08.007+0000 writing ************ to archive 'standalone.gzip'
2020-04-06T08:00:08.007+0000 MuxIn open ************_log
Killed
#
Server memory stats
$ free -m
total used free shared buff/cache available
Mem: 459 328 6 0 124 123
Swap: 0 0 0
Not sure why my mongodump process is getting killed. I think it may be because of memory issues but I am not sure how to fine tune those customizations.
I would start with consulting the MongoDB server log. See "Container shell access and viewing MongoDB logs" here if this is the image you are using.
To determine whether your container is running out of memory, get a shell on it and run top while dumping.
I faced the same issue, it happened when I was executing mongodump on the database host system. However, I did small workaround, for me the the infra looked like:
Source Mongo <---> My PC <---> Dest Mongo (K8S)
I did run mongorestore on my PC, in container, connected to remote machine and dumped to another remote instance. This was slower but didn't kill the process.
Run mongorestore on your PC and ensure you have access to remote DB or do a port-forwarding thingy.
$ docker run --network host --entrypoint bash -it mongo:4.2
Dump the archive
$ mongodump --archive=test.archive --db db --username username --host <address>
Restore the archive from inside the container
$ mongorestore --archive=test.archive --db db --username username --host <address>
This is the only way I managed to not crash the mongodump. If someone runs into this issue, I would consider having mongo container on your OS/some different machine, do a dump from this point and then restore as mentioned.
As a newbie of Docker, I tried to build a mongoDB Docker container containing some sample data by means of docker-compose.yml in the working directory.
My mongo/Dockerfile contains following code:
FROM mongo:latest
ADD shops.json /home/
RUN mongoimport --db masterdata --collection shops --file /home/shops.json
In the last line, sample data would be imported to mongoDB.
My docker-compose.yml contains the following code:
version: '3'
services:
mongo:
build: mongo
ports:
- "27017:27017"
With the configuration above, I tried to build the MongoDB docker container and got error:
Failed: error connecting to db server: no reachable servers
This problem looks similar with mongoimport Docker Failed: error connecting to the db server: no reachable servers, but I really tried every suggestion given in mongoimport Docker Failed: error connecting to the db server: no reachable servers and am never able to resolve this error.
So anyone can help on this?
RUN mkdir -p /opt/acrc/config
COPY *.json /opt/acrc/config/
RUN mongod --fork --logpath=/tmp/mongodb.log && sleep 20 && \
mongoimport -c=users -d=acrc --mode=upsert --file=/opt/acrc/config/users.json && \
mongoimport -c=tasks -d=acrc --mode=upsert --file=/opt/acrc/config/tasks.json && \
mongoimport -c=configs -d=acrc --mode=upsert --file=/opt/acrc/config/configs.json && \
mongoimport -c=agentGroupConfigs -d=acrc --mode=upsert --file=/opt/acrc/config/agentGroupConfigs.json
The mongo database is not started until you start a container of your image. The Dockerfile just takes care of the files and folders within the image but doesn't start any services.
To import a json into the database, you can write a bash script which will start the mongo services and import the json. You can set this script as entry script for your image.
Now I built an image(userdb) with the usersInfo.js data file by the Dockerfile:
FROM mongo
COPY usersInfo.js /data/db
COPY script.sh .
RUN chmod +x script.sh
CMD [ "./script.sh"]
and the script.sh file as:
$ mongoimport --host 127.0.0.1 --db users --collection usersInfo --drop
--file /data/db/usersInfo.js
when I run the container as:
docker run --name test -it userdb
it gives me the warn:
2017-11-10T23:14:39.781+0000 [........................]
users.usersInfo 0B/10.9KB (0.0%)
2017-11-10T23:14:40.286+0000 [........................]
users.usersInfo 0B/10.9KB (0.0%)
2017-11-10T23:14:40.286+0000 Failed: error connecting to db server:
no reachable servers
And I run the server on the local computer, it still not works.
What I am trying to do is to import the usersInfo.js file from the image (userdb)itself, why does it ask me to connect the server here? How to fix it?
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.
To start the container, I am typing the following command:
sudo docker run -i -t -p 28000:27017 mongo:latest /usr/bin/mongod --smallfiles
But I want to open the shell in this container to type the mongo commands.
What command should I run to do the same?
You can run the interactive mongo shell by running the following command:
docker run -it -p 28000:27017 --name mongoContainer mongo:latest mongo
Otherwise, if your container is already running, you can use the exec command:
docker exec -it mongoContainer mongo
The thing that I struggled too but found a solution:
docker pull mongo
docker run --name CONTAINERNAME --restart=always -d -p 8080:8080 mongo mongod --auth
sudo docker exec -i -t CONTAINERNAME bash
mongo
use admin
db.createUser({user:"user", pwd:"password", roles:[{role:"root", db:"admin"}]})
exit && exit
Now you have created a running Docker container with everything you need. Now if you want to connect from any client with an admin user, just run this
mongo -u "user" -p "password" HOSTIP --authenticationDatabase "admin"
Extending and updating #vladzam answer and if your container is already running in docker, you can use the exec mongosh command with login and pass options like this:
docker exec -it database-dev mongosh -u "myLogin" -p "myPass"
Download the latest MongoDB Docker image from Docker Hub
sudo docker pull mongo
Now set up MongoDB container
docker run --name containername mongo
Interact with the database through the bash shell client
docker exec -it containername bash
Launch the MongoDB shell client
mongosh #now it is mongosh to access shell
It depends which version of MongoDB you're running.
Please see the differences here : The MongoDB Shell versus the Legacy mongo Shell.
For example, with Mongo 3 the executable was mongo :
$ docker run --rm mongo:3 which mongo mongosh mongod
/usr/bin/mongo
/usr/bin/mongod
With Mongo 5 both mongo and mongosh are present :
$ docker run --rm mongo:5 which mongo mongosh mongod
/usr/bin/mongo
/usr/bin/mongosh
/usr/bin/mongod
With Mongo 6 you can only use the newest mongosh :
$ docker run --rm mongo:6 which mongo mongosh mongod
/usr/bin/mongosh
/usr/bin/mongod
Now if you want to try it, run a MongoDB instance :
$ docker run -d -p 29000:27017 --name mongodb-6 mongo:6
Then connect a shell :
$ docker exec -it mongodb-6 mongosh
You'll get something like :
Current Mongosh Log ID: 632456e42dbc88aa0bfe612f
Connecting to: mongodb://127.0.0.1:27017/?directConnection=true&serverSelectionTimeoutMS=2000&appName=mongosh+1.5.4
Using MongoDB: 6.0.1
Using Mongosh: 1.5.4
...
You can also pass options like :
$ docker exec -it mongodb-6 mongosh --version
docker exec -it mongoContainer mongosh
i tried mongo, mongod, and mongodb but failed. Then i changed it to mongosh and it works!
Assuming you have mongo installed on your host computer, which was in my case when I asked this question years ago. This is an alternate way I tried: Open a new terminal
mongo 127.0.0.1:28000
Your mongo shell starts in this terminal now.