Anda di halaman 1dari 8

🍃 MongoDB & Docker data volume containers – katopz – Medium https://medium.com/@katopz/mongodb-docker-data-volume-con...

katopz Follow
Blockchain Debugger
Jul 24, 2016 · 3 min read

4.
3.
2.
1.
🍃 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…

1.12
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

1 of 8 28/10/18, 9:17 PM
🍃 MongoDB & Docker data volume containers – katopz – Medium https://medium.com/@katopz/mongodb-docker-data-volume-con...

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…

1 #!/bin/bash
2 CONTAINER_INDEX=$1
3 CONTAINER_NAME="mongo"$1
4 DATA_VOLUME="mongo-data-volume"$1
5 mkdir $CONTAINER_NAME
6 DB_PORT=$((30000+$CONTAINER_INDEX))
7 HTTP_PORT=$((28017+$CONTAINER_INDEX))
8 NETWORK_NAME=$2
9 REPLICASET_NAME=$3
10
11 # Set oplog size.
12 oplogSizeMB=128
13
14 # Remove old container if has.
15 docker rm $CONTAINER_NAME -f
16 # Remove old folder if has.
17 rm -rf $CONTAINER_NAME
18 # Create log container.
19 mkdir -p $CONTAINER_NAME/var/log/mongodb
20
21 # Remove old data volume container if has.
22 docker rm $DATA_VOLUME -f
23 # Create data volume.
24 docker create --name $DATA_VOLUME -v /data/db mongo /bin/true
25
26 # Export config.
27 cat <<EOF > $CONTAINER_NAME/mongo.conf
28 systemLog:
29 destination: file
30 path: "/var/log/mongodb/mongod.log"
31 logAppend: true
32 storage:
33 mmapv1:
34 smallFiles: true
35 net:
36 http:

2 of 8 28/10/18, 9:17 PM
🍃 MongoDB & Docker data volume containers – katopz – Medium https://medium.com/@katopz/mongodb-docker-data-volume-con...

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 =ne when I
remove it.

And also this in same folder.

1 ## Setup Docker
2 NETWORK_NAME=${1:-my-mongo-cluster}
3 REPLICASET_NAME=${2:-my-mongo-set}
4
5 # Disconnect old container if has.
6 docker network disconnect -f $NETWORK_NAME mongo0
7 docker network disconnect -f $NETWORK_NAME mongo1
8 docker network disconnect -f $NETWORK_NAME mongo2
9 # Remove old network if has.
10 docker network rm $NETWORK_NAME
11 # Remove dangling.
12 docker volume rm $(docker volume ls -qf dangling=true)
13
14 # Create docker network name.
15 docker network create $NETWORK_NAME
16 # List docker network.
17 docker network ls
18
19 ## Setup Mongo
20 # List existing mongo process.
21 pgrep mongo
22 # Kill existing process if has.
23 pkill mongo
24
25 # Create mongo container 0.
26 . setup-container.sh 0 $NETWORK_NAME $REPLICASET_NAME
27 # Create mongo container 1.
28 . setup-container.sh 1 $NETWORK_NAME $REPLICASET_NAME

Then run it! What you waiting for!

$ . setup.sh

3 of 8 28/10/18, 9:17 PM
🍃 MongoDB & Docker data volume containers – katopz – Medium https://medium.com/@katopz/mongodb-docker-data-volume-con...

And you should see…

Nothing break, Yeah!

What diKerent is we didn’t map volume folder -v for dbPath as external


physical path anymore but we use — volumes-from container instead.

Also at conNg Nle, 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.

4 of 8 28/10/18, 9:17 PM
🍃 MongoDB & Docker data volume containers – katopz – Medium https://medium.com/@katopz/mongodb-docker-data-volume-con...

# 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 Nnd 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


Nnally 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 conNg and you should get…

5 of 8 28/10/18, 9:17 PM
🍃 MongoDB & Docker data volume containers – katopz – Medium https://medium.com/@katopz/mongodb-docker-data-volume-con...

Robomongo

TODO
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.

Bed!

Note (re@)
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!

6 of 8 28/10/18, 9:17 PM
🍃 MongoDB & Docker data volume containers – katopz – Medium https://medium.com/@katopz/mongodb-docker-data-volume-con...

7 of 8 28/10/18, 9:17 PM
🍃 MongoDB & Docker data volume containers – katopz – Medium https://medium.com/@katopz/mongodb-docker-data-volume-con...

8 of 8 28/10/18, 9:17 PM