Mongo docker image - unable to run on different port - mongodb

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

Related

Connect to Mongo Router inside Docker container from remote host

I have an EC2 instance in AWS with a mongo docker container that init a mongo router such as :
docker run --network mongo-net -p 27051:27017 --expose=27017 -d --name=mongos51 mongo mongos --port 27017 --configdb configserver/configserver41:27017,configserver42:27017 --bind_ip 0.0.0.0
I have opened my SG in AWS to allow any connections and i can reach the public IP on the port 27051 without problems.
My container is in a bidge docker network to allow all of my mongo instances to communicate.
However, if i try to connect to the mongo shell like :
mongosh mongodb://public_ip:27051
I have the error Connection refused instantly. Same if i try to open the mongoshell from inside the container with :
docker exec -it mongos51 bash -c "echo 'sh.addShard(\"shard1/mongodb11\")' | mongosh "
Current Mongosh Log ID: 6149ed68def89bc1907c8988
Connecting to: mongodb://127.0.0.1:27017/?directConnection=true&serverSelectionTimeoutMS=2000
MongoNetworkError: connect ECONNREFUSED 127.0.0.1:27017
I have the same error if i use the private IP instead of 127.0.0.1.
What i am missing here ?
According the documentation try to connect like this :
mongosh --host public_ip --port 27051
It work for me when I follow these steps.

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

Connect to container's localhost interface in Docker

Is it possible to connect to a process running in a Docker container but exactly via container's loopback interface ?
Basically I am looking for this option:
docker run ... -p 12345:127.0.0.1:12345 ...
This is rejected by Docker.
The use case is for example to setup Mongo admin user via Localhost Exception
You have the port mapping slightly wrong. Try it like this:
$ docker run -itd -p 27117:27017 mongo
so 27117 is the port you can connect to outwith the docker container, and 27017 is the port mongod is running on within docker
So when I connect the mongo shell I can connect to the mongod within docker like:
$ mongo --port 27117
Try like this :
Step 1 :
pulling mongo image from docker.
docker pull mongo
Step 2 :
Create new directory for storing mongodb data in you shared location like /home/user/databases/mongo
Step 3:
Run mongodb from docker image using below command
if you want change the mongodb port like 12345 set it in the command. 27017 is globally declared in docker you cannot change it but i want locally change the mongodb port like 12345
docker run -d -p 12345:27017 -v /home/user/databases/mongo:/data/db --name mongodb mongo

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

Can not connect to Mongo docker instance via mono client on mac

I have installed Mongo docker image and run it using those commands (mac boot2docker is installed)
docker pull mongo
and
docker run --name some-mongo -d mongo
but now I want to connect to it via mongo client running:
mongo --port 27017 --host 127.0.0.1
but I get this error message:
MongoDB shell version: 3.0.4
connecting to: 127.0.0.1:27017/test
2015-07-27T14:22:24.088+0300 W NETWORK Failed to connect to 127.0.0.1:27017, reason: errno:61 Connection refused
2015-07-27T14:22:24.094+0300 E QUERY Error: couldn't connect to server 127.0.0.1:27017 (127.0.0.1), connection attempt failed
at connect (src/mongo/shell/mongo.js:181:14)
at (connect):1:6 at src/mongo/shell/mongo.js:181
exception: connect failed
It is clear to me that Docker fails to expose the ports since telnet to 27017 on the localhost fails as well.
What the hack am I doing wrong?
You have 2 problems :
Like h3nrik said you should connect to the boot2docker VMs address. If you don't know it use the following command :
boot2docker ip
And your port isn't open in the first place.
Your Docker run command should look like this :
docker run -p 27017:27017 --name some-mongo -d mongo
Instead of 127.0.0.1 you should use the boot2docker VMs IP address. Usually 192.168.59.103. You can verify to which IP you should connect executing boot2docker ip.
Update: I discovered that you do not export any ports by your containers run statement:
docker run --name some-mongo -d mongo
Without any ports exposed you cannot connect, of course. Try to re-connect after running (depending on your requirements you can add more ports according to the mongodb documentation):
docker run --name some-mongo -d -p 27017:27017 mongo