Mongo: Can not access mongo DB after run a container

Created on 10 May 2017  路  2Comments  路  Source: docker-library/mongo

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

Most helpful comment

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:

  • stopping the container: docker stop my_mongo

    • sends SIGTERM, waits 10 seconds and then SIGKILL if it has not stopped

  • stop the container without any delay: docker kill -sKILL my_mongo

    • same as a kill -9, process has no chance to clean up

  • removing the stopped container: docker rm my_mongo
  • removing the container (even if it is running): docker rm -f my_mongo
  • removing the container and its volume: docker rm -fv my_mongo

All 2 comments

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:

  • stopping the container: docker stop my_mongo

    • sends SIGTERM, waits 10 seconds and then SIGKILL if it has not stopped

  • stop the container without any delay: docker kill -sKILL my_mongo

    • same as a kill -9, process has no chance to clean up

  • removing the stopped container: docker rm my_mongo
  • removing the container (even if it is running): docker rm -f my_mongo
  • removing the container and its volume: docker rm -fv my_mongo

I 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 [more ids...]

Was this page helpful?
0 / 5 - 0 ratings

Related issues

SimonTulettIdeaco picture SimonTulettIdeaco  路  6Comments

troyeagle picture troyeagle  路  6Comments

wayneiny picture wayneiny  路  4Comments

jjelev picture jjelev  路  5Comments

sri85 picture sri85  路  7Comments