🍃MongoDB & Docker data volume containers
TL;DR : Use Docker data volume containers for more safety and portable.
We did mount a host directory as a data volume at dbpath for MongoDB via YAML here…
We already did replicate (+arbiter) with Mongo only, now it’s time to let Docker getting in so we can pack all leaf in…medium.com
but it seem like not a good idea to let data naked like that! So we have to keep it save by mounting a data volume container instead.
Just like previously! Download this…
Note : I did curious about line #24 at /bin/true which is somehow required (Many examples use it for some reason) but seem to be fine when I remove it.
And also this in same folder.
Then run it! What you waiting for!
$ . setup.sh
And you should see…
What different is we didn’t map volume folder -v for dbPath as external physical path anymore but we use — volumes-from container instead.
Also at config file, we have to remove storage: dbPath and related there, default dpPath is match what we create in data volume so it should just work out of the box! (I mean container!)
Na, you aren’t done until some test! How can we prove that data volume container is safe and still persist our data? Here’s how we can ensure…
After you run once, at second time do comment out setup-container.sh at line #22 and #24 like this.
# Remove old data volume container if has.
# docker rm $DATA_VOLUME -f
# Create data volume.
# docker create — name $DATA_VOLUME -v /data/db mongo /bin/true
So it won’t remove data volume container as usual and won’t create a new one which mean the old one will be use. Now run setup.sh again and find old data.
db = (new Mongo(‘mongo0:27017’)).getDB(‘test’)
And you should see our beloved data is still there! Rad!
Hint : If you’re on my-mongo-set:SECONDARY>, you’ll need db.setSlaveOk() or wait for awhile for PRIMARY get elected.
Phew! Seem like we’ve plenty of thing to learn, we don’t even use docker-compose or dab yet but we will!
Do follow me ‘til the end of my Docker orchestration journey, I should finally produce some micro-service with minimum stack as possible on my tiny DigitalOcean pretty soon!
If you hate terminal and miss GUI. You can access your database with Robomongo by just create new connection to localhost:30000 and connect without touching other config and you should get…
- Figure out why someone reuse old image and someone use busybox or scratch.
- Add some more security.
- Try shard and maybe docker compose at the same time.
- Try Backup, restore, or migrate data volumes.
Where to get more headache?
- Data Volumes vs Data-Only Container.
- Create Engine Level Volumes and Storage for Transient Containers.
- Storage Considerations.
Don’t miss a chance to get free $10 for DigitalOcean ← 2 months free hosting!
And do claim your cheapest .io name before it’s gone at NameCheap.