If you have MongoDB 3.x running on docker and you want to upgrade to version 4.x, you cannot re-use the old data volume.
Please follow these steps to achieve upgrading MongoDB=
1. I will assume that you want to upgrade MongoDB from version 3.2 to 4.2
2. You need to incremental upgrade from 3.2 to 3.4, 3.6, 4.0 and then finally 4.2
3. run the following command to upgrade from version 3.2 o 3.4
docker run --name mongo -p 27017:27017 -v /volume:/data/db -d mongo:3.4
4. run the following command to set feature compatibility
docker exec -it mongo bash -c 'mongo --eval "db.adminCommand( { setFeatureCompatibilityVersion: \"3.4\" } )"'
5. repeat steps 3 and 4 three times
- upgrade from 3.4 to 3.6
- upgrade from 3.6 to 4.0
- upgrade from 4.0 to 4.2
6. verify the MongoDB version by running
docker exec -it mongo bash -c 'mongo --version'
credit:
https://forums.rocket.chat/t/solved-trying-to-update-mongodb-from-3-2-to-4-0-in-docker-but-errors/3200