MongoDB docker image connection issue - mongodb

Im try to run a local docker container running MongoDB but I got some issues.
Im using MacOS and this is my docker file:
FROM mongo:4.4.10
RUN apt-get update && \
apt-get install -y mongodb-database-tools
ENV MONGO_INITDB_ROOT_USERNAME admin
ENV MONGO_INITDB_ROOT_PASSWORD admin
EXPOSE 27017
CMD ["mongod", "--bind_ip_all"]
But when I try to connect in mongo inside the container I got:
root#1820984d904c:/# mongo
MongoDB shell version v4.4.10
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Error: couldn't connect to server 127.0.0.1:27017, connection attempt failed: SocketException: Error connecting to 127.0.0.1:27017 :: caused by :: Connection refused :
connect#src/mongo/shell/mongo.js:374:17
#(connect):2:6
exception: connect failed
exiting with code 1
Im running the container like this:
docker run -it -p 27017:27017 --rm --name containername imagename bash
Any ideas?
Thanks

You are not starting mongo with your docker run command. You are starting bash instead.
From https://docs.docker.com/engine/reference/run/#overriding-dockerfile-image-defaults:
CMD (default command or options)
Recall the optional COMMAND in the Docker commandline:
docker run [OPTIONS] IMAGE[:TAG|#DIGEST] [COMMAND] [ARG...]
This command is optional because the person who created the IMAGE may have already provided a default COMMAND using the Dockerfile CMD instruction. As the operator (the person running a container from the image), you can override that CMD instruction just by specifying a new COMMAND.
So start your docker with
docker run -p 27017:27017 --rm --name containername imagename
Then bash in to the container with
docker exec -it containername bash

Related

Unable to open Mongo shell using Docker run but exec

What I want to is open mongo shell in terminal.
When I run mongo container using run with mongo command,
I got an error.
$ docker run -it --name mongodb mongo:latest mongo
MongoDB shell version v4.2.1
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
2019-12-01T10:01:18.524+0000 E QUERY [js] Error: couldn't connect to server 127.0.0.1:27017, connection attempt failed: SocketException: Error connecting to 127.0.0.1:27017 :: caused by :: Connection refused :
connect#src/mongo/shell/mongo.js:341:17
#(connect):2:6
2019-12-01T10:01:18.526+0000 F - [main] exception: connect failed
2019-12-01T10:01:18.526+0000 E - [main] exiting with code 1
But when I run mongo first and exec command later, I can open mongo shell nicely.
$ docker run -d --name mongodb mongo:latest
0296856a6e614667ad7cb81cac104d2704369d8d98f9c5dfdc8724dd5c74591a
$ docker exec -it mongodb mongo
MongoDB shell version v4.2.1
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("02a0086f-35f8-4c8c-a615-9769743b22d4") }
MongoDB server version: 4.2.1
Welcome to the MongoDB shell.
(...)
> show dbs
admin 0.000GB
config 0.000GB
local 0.000GB
Why I can't open mongo shell using single line run command?
I guess that command try to mongo shell start before mongod ready. But I saw few docuements start mongo shell like this. Please let me know why this happend.
When you docker run imagename command, that command runs instead of the normal command the image would run (the CMD from its Dockerfile). When you docker exec containername command, that launches an additional command in the same container. So your docker run example launches the mongo shell instead of running a new database, but by default it tries to connect to a database on localhost, that is, in the same container.
For interacting with databases, my general recommendation would be to install the client tools you need locally and use them directly: run mongo on your host pointing at whatever port you published. You don't need Docker at all (or the root-level privileges it implies) just to make simple client calls. docker exec is in many ways equivalent to ssh'ing as root into the container, and it wouldn't be my preferred path to interacting with a database.
If you do want to docker run a client, you'd have to make it communicate with a server container, using the normal mechanisms for this.
docker network create mongo
docker run -d --net mongo -p 27017:27017 --name mongodb mongo:latest
docker run -it --net mongo mongo:latest \
mongo --host mongodb

Mongo docker image - unable to run on different port

There are a tons of question here on SO citing how this command alone
docker run --name mymongo --network bridge -p 27117:27117 -v "$PWD/db":/data/db -d mongo
should run mongo on port 27117.
However this doesn't work for me. The container runs, but the mongo is run on its default port alone (see output from container itself):
# mongo
MongoDB shell version v4.0.4
connecting to: mongodb://127.0.0.1:27017
# mongo --port 27117
MongoDB shell version v4.0.4
connecting to: mongodb://127.0.0.1:27117/
2018-11-20T17:26:09.345+0000 E QUERY [js] Error: couldn't connect to server 127.0.0.1:27117, connection attempt failed: SocketException: Error connecting to 127.0.0.1:27117 :: caused by :: Connection refused :
What is going on?
Thanks a lot!
Inside your container mongo runs on its default port, which is 27017
So, you should modify your command and specify the port mapping like this: -p 27117:27017
The full command would be this:
docker run --name mymongo --network bridge -p 27117:27017 -v "$PWD/db":/data/db -d mongo
With that command you are telling docker that the port is 27117, but you also need to start mongo with that port.
To do so, just add --port 27117 at the end of your command:
docker run --name mymongo --network bridge -p 27117:27117 -v "$PWD/db":/data/db -d mongo --port 27117

How to start a mongodb shell in docker container?

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.

Docker - how to run mongodb process as daemon

I am running docker on windows with the docker-machine and Docker Toolbox installation package.
Boot2Docker is now deprecated, btw.
The docs here: https://docs.docker.com/examples/mongodb/ told me to connect to the running mongodb container like:
$ mongo --port 27017 --host 192.168.99.100
But I get this error:
$ mongo --host 192.168.99.100
sh: mongo: command not found
Any ideas?
EDIT 1: I run the container like this:
$ docker run -p 27017:27017 --name mongodb -d myname/repo
I guess instead of building your own myname/repo image of mongo you could have an easier start with the official mongo image: https://hub.docker.com/_/mongo/
Update as for the error you see, it looks like the mongo client is not installed at the wherever you execute your test. You could install it or use the mongo container: docker run -it --rm --link <id of the running mongo container>:mongo mongo mongo --host mongo

Node / Express app can't connect to docker mongodb

I want to run a node app that uses express and connects to a (boot2docker) docker mongo container.
When I first wrote the app, I was using a locally installed instance of mongodb, and the following config worked:
module.exports = {
env: 'development',
mongo: {
uri: 'mongodb://localhost/fullstack-dev'
}
};
And it ran as expected.
Now I'm trying to swap that out for a docker instance of mongo.
So, I've done the following steps to get mongo running:
$ docker pull mongo:latest
$ docker run -v "$(pwd)":/data --name mongo -d mongo mongod --smallfiles
$ docker ps
#my mongo instance is 442c2541fe1a
$ docker exec -it 442c2541fe1a bash
$ mongo
At this point, appears to be running in my command prompt.
Then, I tried to get the IP of boot2docker vm that runs docker on osx:
$ boot2docker ip
# the IP returned: 192.168.59.103
So, then, I went to swap out the old mongodb path in the nodejs config to the following:
module.exports = {
env: 'development',
mongo: {
uri: 'mongodb://192.168.59.103:27017/fullstack-dev'
}
};
and when I run the application, I get a connection error.
events.js:72
throw er; // Unhandled 'error' event
^
Error: failed to connect to [192.168.59.103:27017]
What is the proper config for connecting my MEAN stack application with a docker'ified mongodb instance?
You need to publish the appropriate port on the container with the -p argument i.e:
docker run -p 27017:27017 -v "$(pwd)":/data --name mongo -d mongo mongod --smallfiles
The will make port 27017 accessible on the host (the VM in your case) and forward traffic to port 27017 on the container.