My client has mongodb running in docker container, one morning the db crashed and logs says "unclean shutdown detected". I tried for repairing mongodb but not able to do as it is running under docker container. The reason is that as i have to shutdown mongodb for running repair command and once i shutdown the mongodb then docker throws me out of the container because only one process was running and as it was stopped container was also stopped, now i am not able to run any mongod commands as i am out of container and container is stopped along with mongod instance.
So, can anyone help me out of this, how can i run repair command from outside of docker container ?
Can i run repair command without stopping mongod ?
Please help !!!
Thanks Irshad
For connecting to your local MongoDB instance from a Container you must first allow to accept connections from the Docker bridge gateway. To do so, simply add the respective gateway IP in the MongoDB config file /etc/mongod. conf under bindIp in the network interface section.
Can MongoDB Run in a Docker Container? MongoDB can run in a container. The official image available on Docker Hub contains the community edition of MongoDB and is maintained by the Docker team. This image can be used in a development environment.
Go to the “mongodb” folder and run the docker-compose up command to start the MongoDB container. The -d operator runs the detached container as a background process. The up command will pull the mongo image from the docker registry and create the container using the given parameters in the docker-compose. yml file.
The example command below fixed my issue with corrupted Wt files.
docker run -it -v <path>:/data/db <image-name> mongod --repair
Just replace path and image-name with yours
I had this same thing when my Mac decided to restart and install an update this morning, grr.
Normally my mongo container is started with a docker-compose up
command, which uses a docker-compose.yml
file for all the settings, something like...
version: "3"
services:
mongo:
image: mongo:3.6.12
container_name: mongo
ports:
- 27017:27017
volumes:
- ~/mongo/data/db:/data/db
- ~/mongo/db/configdb:/data/configdb
To run a basic docker
command with those settings I did...
# Kill any existing mongo containers
docker rm mongo
# Repair the db
docker run -it -v ~/mongo/data/db:/data/db -v ~/mongo/db/configdb:/data/configdb --name mongo -d mongo:3.6.12 mongod --repair
# Has it finished the repair yet?
docker ps -a
After about a minute the container exited, the database was repaired and I was able to run mongo using docker-compose
again.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With