Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why isn't docker commit saving data in my mongo database container?

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?

like image 954
drs Avatar asked Oct 19 '22 09:10

drs


1 Answers

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.

like image 80
Alberto Massidda Avatar answered Nov 15 '22 07:11

Alberto Massidda