I have a docker-compose file that links a seed script with a mongo image from docker's public registry. I can populate the database with:
docker-compose up
docker-compose run seed_script
I can connect to this mongo container with the mongo
cli from my host system and verify that the seed script is working and there's data in the database.
When I'm done seeding, I can see the mongo container ID with docker ps
. I stop the container by pressing Ctrlc in the docker-compose terminal and commit the changes to the mongo container:
docker commit <mongo container ID> mongo-seeded-data
However, when I run that container individually, it's empty:
docker run -p 27017:27017 mongo-seeded-data
mongo
> show dbs
local 0.000GB
If I bring up the docker-compose containers again and use my host mongo client, I can see the data:
docker-compose up
mongo
> show dbs
seeded_db 0.018GB
local 0.000GB
I committed the container with the data in it. Why is it not there when I bring up the container? What is docker-compose
doing differently than docker run
?
Because there is a VOLUME defined in it.
docker commit
saves the content of the overlay fs as a new image, but VOLUMES transcend the overlay fs. That's why it doesn't get saved.
I had the same problem in the past and resolved patching the mongo image by using a different data directory, so that the data would get written inside the container instead of the VOLUME.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With