How to recovery mongodb data after a sudden server shutdown in docker under Linux

I suppose you have mounted the mongo /data/db into a local folder with this command: 

    volumes:
      - "./configs/mongodb_data:/data/db"

Then you should run this command to recovery the data:

chmod 777 /root/it_has_alternatives/configs/mongodb_data
docker container run --rm -v /root/it_has_alternatives/configs/mongodb_data:/data/db mongo:6.0.4-jammy mongod --repair

If your docker has some serious problem, you could run the following command to repair it:

  • Make sure all docker containers are down: docker compose down
  • Remove problematic overlay2: sudo rm -R /var/lib/docker/overlay2
  • Remove images: sudo rm -R /var/lib/docker/image
  • Clear any other cached data: sudo docker system prune -f
  • Restart docker service: sudo systemctl restart docker