The context
I am learning how to use docker. I am using the image mongodb/mongodb-community-server
which is mentioned in this guide at the MongoDB’s official website.
I want the container that runs that image to have persistent data. That is, if the container gets deleted, I can create a new container and continue working with the data from the database in the previous container. I noticed that when I use --volume /tmp/data:/data/db
in docker run
, create some simple data and then delete the container; new containers can access the same data from the previous container provided that I use --volume /tmp/data:/data/db
(see experiment 1 below). However, when I use --volume /tmp/data:/data
, the directory /tmp/data
only contains two empty directories upon the deletion of the first container (see experiment 2 below).
Experiment 1: using --volume /tmp/data:/data/db
(the data persists)
$ sudo rm -rf /tmp/data
$ mkdir /tmp/data
$ sudo chmod 777 /tmp/data
$ docker run --detach --name my-container --publish 27017:27017 --volume /tmp/data:/data/db mongodb/mongodb-community-server
9a0dd5fb17cd83369132badf201f6c58dedd1308b99b9878b108427fc840e7cd
$ docker exec --interactive --tty my-container mongosh
Current Mongosh Log ID: 66427030756a7dd113c934dc
Connecting to: mongodb://127.0.0.1:27017/?directConnection=true&serverSelectionTimeoutMS=2000&appName=mongosh+2.2.4
Using MongoDB: 7.0.8
Using Mongosh: 2.2.4
For mongosh info see: https://docs.mongodb.com/mongodb-shell/
To help improve our products, anonymous usage data is collected and sent to MongoDB periodically (https://www.mongodb.com/legal/privacy-policy).
You can opt-out by running the disableTelemetry() command.
------
The server generated these startup warnings when booting
2024-05-13T19:55:24.677+00:00: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine. See http://dochub.mongodb.org/core/prodnotes-filesystem
2024-05-13T19:55:24.857+00:00: Access control is not enabled for the database. Read and write access to data and configuration is unrestricted
2024-05-13T19:55:24.857+00:00: vm.max_map_count is too low
------
test> db.createCollection('myCollection')
{ ok: 1 }
test> db.myCollection.insertOne({'name': 'John'})
{
acknowledged: true,
insertedId: ObjectId('66427036756a7dd113c934dd')
}
test> db.myCollection.find({})
[ { _id: ObjectId('66427036756a7dd113c934dd'), name: 'John' } ]
test>
I removed the container and I noticed that the directory /tmp/data
contained many files and many directories.
$ docker rm -f my-container
my-container
$ find /tmp/data -mindepth 1 -maxdepth 1
/tmp/data/WiredTiger.wt
/tmp/data/_mdb_catalog.wt
/tmp/data/WiredTiger
/tmp/data/index-5-4391870653988812307.wt
/tmp/data/journal
/tmp/data/index-3-4391870653988812307.wt
/tmp/data/collection-4-4391870653988812307.wt
/tmp/data/WiredTiger.turtle
/tmp/data/index-8-4391870653988812307.wt
/tmp/data/collection-0-4391870653988812307.wt
/tmp/data/WiredTiger.lock
/tmp/data/index-1-4391870653988812307.wt
/tmp/data/collection-7-4391870653988812307.wt
/tmp/data/mongod.lock
/tmp/data/sizeStorer.wt
/tmp/data/diagnostic.data
/tmp/data/.mongodb
/tmp/data/storage.bson
/tmp/data/WiredTigerHS.wt
/tmp/data/index-6-4391870653988812307.wt
/tmp/data/collection-2-4391870653988812307.wt
Afterwards, I created a new container with the same --volume
flag and listed the items in the collection myCollection
. The same item that I created in the first container was found.
$ docker run --detach --name my-container --publish 27017:27017 --volume /tmp/data:/data/db mongodb/mongodb-community-server
613077af41456fab6a2edd20afed5d69b643c4cc98005e0973ecc02c4de3169a
/my-storage/miscellaneous/source-code/flat-file-system-python-rest-api-mongodb/backend
$ docker exec --interactive --tty my-container mongosh
Current Mongosh Log ID: 664270be64aa306df8c934dc
Connecting to: mongodb://127.0.0.1:27017/?directConnection=true&serverSelectionTimeoutMS=2000&appName=mongosh+2.2.4
Using MongoDB: 7.0.8
Using Mongosh: 2.2.4
For mongosh info see: https://docs.mongodb.com/mongodb-shell/
------
The server generated these startup warnings when booting
2024-05-13T19:57:48.381+00:00: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine. See http://dochub.mongodb.org/core/prodnotes-filesystem
2024-05-13T19:57:48.719+00:00: Access control is not enabled for the database. Read and write access to data and configuration is unrestricted
2024-05-13T19:57:48.720+00:00: vm.max_map_count is too low
------
test> db.myCollection.find({})
[ { _id: ObjectId('66427036756a7dd113c934dd'), name: 'John' } ]
test>
Because I get the same item that we created in the initial container, I conclude that the data persists across containers when using --volume /tmp/data:/data/db
.
Experiment 2: using --volume /tmp/data:/data
(the data doesn’t persist)
$ sudo rm -rf /tmp/data
$ mkdir /tmp/data
$ sudo chmod 777 /tmp/data
$ docker run --detach --name my-container --publish 27017:27017 --volume /tmp/data:/data mongodb/mongodb-community-server
38a64cfb09fa212dd6cceca8fcededef3dcf97e6724fb9089842383af20c5f25
$ docker exec --interactive --tty my-container mongosh
Current Mongosh Log ID: 66426f5e8f9c83be82c934dc
Connecting to: mongodb://127.0.0.1:27017/?directConnection=true&serverSelectionTimeoutMS=2000&appName=mongosh+2.2.4
Using MongoDB: 7.0.8
Using Mongosh: 2.2.4
For mongosh info see: https://docs.mongodb.com/mongodb-shell/
To help improve our products, anonymous usage data is collected and sent to MongoDB periodically (https://www.mongodb.com/legal/privacy-policy).
You can opt-out by running the disableTelemetry() command.
------
The server generated these startup warnings when booting
2024-05-13T19:51:56.781+00:00: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine. See http://dochub.mongodb.org/core/prodnotes-filesystem
2024-05-13T19:51:56.960+00:00: Access control is not enabled for the database. Read and write access to data and configuration is unrestricted
2024-05-13T19:51:56.960+00:00: vm.max_map_count is too low
------
test> db.createCollection('myCollection')
{ ok: 1 }
test> db.myCollection.insertOne({'name': 'John'})
{
acknowledged: true,
insertedId: ObjectId('66426f648f9c83be82c934dd')
}
test> db.myCollection.find({})
[ { _id: ObjectId('66426f648f9c83be82c934dd'), name: 'John' } ]
test>
I removed the running container and I noticed that the directory /tmp/data
only contained two empty directories.
$ docker rm -f my-container
my-container
$ find /tmp/data
/tmp/data
/tmp/data/configdb
/tmp/data/db
I created the container again and no item was found.
$ docker run --detach --name my-container --publish 27017:27017 --volume /tmp/data:/data mongodb/mongodb-community-server
06f76f223ed1df0cad85133413638565df7023f71d2b00fd4b63e29f98cde92e
$ docker exec --interactive --tty my-container mongosh
Current Mongosh Log ID: 66426fba41b2d612c3c934dc
Connecting to: mongodb://127.0.0.1:27017/?directConnection=true&serverSelectionTimeoutMS=2000&appName=mongosh+2.2.4
Using MongoDB: 7.0.8
Using Mongosh: 2.2.4
For mongosh info see: https://docs.mongodb.com/mongodb-shell/
To help improve our products, anonymous usage data is collected and sent to MongoDB periodically (https://www.mongodb.com/legal/privacy-policy).
You can opt-out by running the disableTelemetry() command.
------
The server generated these startup warnings when booting
2024-05-13T19:53:28.332+00:00: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine. See http://dochub.mongodb.org/core/prodnotes-filesystem
2024-05-13T19:53:28.512+00:00: Access control is not enabled for the database. Read and write access to data and configuration is unrestricted
2024-05-13T19:53:28.512+00:00: vm.max_map_count is too low
------
test> db.myCollection.find({})
test>
As we can see above, no item was found so I conclude that the data doesn’t persist across containers when using --volume /tmp/data:/data
.
The question
I want to know the reason why the data persists across containers when using --volume /tmp/data:/data/db
, but it doesn’t when using --volume /tmp/data:/data
.
To me, /data/db
is a subdirectory of /data/
, so if I use --volume /tmp/data:/data
, /tmp/data
should include all the content of /data
(i.e. /data/db
) but this doesn’t happen. Am I missing something?