I run a Docker container with the cmd:
$ docker run --name my_mongo -d mongo:3.5
It can successfully create a container.
$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ede5e98eb958 mongo:3.5 "docker-entrypoint.sh" 14 minutes ago Up 14 minutes 27017/tcp my_mongo
When I try to connect to this DB, it goes wrong.
root@ubuntu:~# mongo
MongoDB shell version: 2.4.9
connecting to: test
Wed May 10 04:02:56.530 Error: couldn't connect to server 127.0.0.1:27017 at src/mongo/shell/mongo.js:145
exception: connect failed
When I enter this container, use cmd "mongo" to connect the mongo DB it will stuck...
root@ubuntu:~# sudo docker exec -it my_mongo bash
root@411e98f22781:/# mongo
MongoDB shell version v3.5.6
connecting to: mongodb://127.0.0.1:27017
What's worse, this container can not been stopped or removed.
my docker version
root@ubuntu:~# docker version
Client:
Version: 1.12.3
API version: 1.24
Go version: go1.6.3
Git commit: 6b644ec
Built: Wed Oct 26 21:44:32 2016
OS/Arch: linux/amd64
Server:
Version: 1.12.3
API version: 1.24
Go version: go1.6.3
Git commit: 6b644ec
Built: Wed Oct 26 21:44:32 2016
OS/Arch: linux/amd64
my OS:
root@ubuntu:~# uname -a
Linux ubuntu 4.2.0-27-generic #32~14.04.1-Ubuntu SMP Fri Jan 22 15:32:26 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux
Using the mongo client from the host will require you to specify the IP address of the container to connect to.
$ myMongo="$(docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' my_mongo)"
$ mongo "$myMongo"
Or you need to run the MongoDB container and expose its port as a port on your host:
$ # this will allow you access via localhost on your host, but will prevent external connections
$ # but does not prevent access from other containers on your host
$ docker run --name my_mongo -d -p 127.0.0.1:27017:27017 mongo:3.5
$ mongo
> db
test
$ # this exposes it to anyone that can access port 27017 on your host from your network
$ # can be a security concern if host IP is accessible publicly
$ docker run --name my_mongo -d -p 27017:27017 mongo:3.5
I have never seen the MongoDB container as unstoppable or non-removable. Maybe these commands will help out:
docker stop my_mongoSIGTERM, waits 10 seconds and then SIGKILL if it has not stoppeddocker kill -sKILL my_mongokill -9, process has no chance to clean updocker rm my_mongodocker rm -f my_mongodocker rm -fv my_mongoI just come up with a solution to kill the invincible:
If you deployed the mongo using the stack way, you need to remove the stack first:
docker stack rm mongo
where mongo is the stack name (docker stack ls)
Then you should be able to do it the normal way:
docker container stop $(docker container ls -a -q); docker rmi
Most helpful comment
Using the
mongoclient from the host will require you to specify the IP address of the container to connect to.Or you need to run the MongoDB container and expose its port as a port on your host:
I have never seen the MongoDB container as unstoppable or non-removable. Maybe these commands will help out:
docker stop my_mongoSIGTERM, waits 10 seconds and thenSIGKILLif it has not stoppeddocker kill -sKILL my_mongokill -9, process has no chance to clean updocker rm my_mongodocker rm -f my_mongodocker rm -fv my_mongo