🍃MongoDB & Docker data volume containers

MongoDB ❤ Docker data volume containers

TL;DR : Use Docker data volume containers for more safety and portable.

Why?

We did mount a host directory as a data volume at dbpath for MongoDB via YAML here…

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.

How?

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…

Nothing break, Yeah!

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!)

Test?

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’)
db.mycollection.find()

And you should see our beloved data is still there! Rad!

TADA!
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!

Bonus

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…

Robomongo

TODO

  1. Figure out why someone reuse old image and someone use busybox or scratch.
  2. Add some more security.
  3. Try shard and maybe docker compose at the same time.
  4. Try Backup, restore, or migrate data volumes.

Where to get more headache?

  1. Data Volumes vs Data-Only Container.
  2. Create Engine Level Volumes and Storage for Transient Containers.
  3. Storage Considerations.
  4. Bed!

Note (ref)

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.

Happy Containing!