Home > Software design >  Changes in container not persisted after docker commit [duplicate]
Changes in container not persisted after docker commit [duplicate]

Time:09-29

I just create a container like:

docker run --name mypostgres \
--publish 5432:5432 \
--net=apinetwork \
-e POSTGRES_PASSWORD=pwd \
-e POSTGRES_DB=db_uso \
-d postgres

I connect in the database and create some tables and objects...

After this I commit;

docker commit mypostgres user/mypostgres

But when I create other container from this image:

docker run --name mypostgres2 \
--publish 5432:5432 \
--net=apinetwork \
-e POSTGRES_PASSWORD=mysecretpassword \
-e POSTGRES_DB=correntista \
-d user/mypostgres

I don't see my objects that I created inside!

Please help me.

CodePudding user response:

It's most likely you are not seeing the changes because the volume created with the original container is not being used by the new container.

See Persist the DB Docker tutorial.


You need to explicitly create a volume, and make sure it is being used in the new container.

$ docker volume create my-pg-db
$ docker run --name mypostgres \
  --publish 5432:5432 \
  --net=apinetwork \
  -v my-pg-db:/var/lib/postgresql/data \
  -e POSTGRES_PASSWORD=pwd \
  -e POSTGRES_DB=db_uso \
  -d postgres

This means it may not even be necessary to make a copy of your container, if all the changes you are doing are on the DB. The volume which contains the DB is what you need to preserve!

And make sure you mount and use it whenever you want to start the container.

CodePudding user response:

The reason data is not persisted is explained in this other SO question:

docker postgres with initial data is not persisted over commits

The problem is that the postgres Dockerfile declares "/var/lib/postgresql/data" as a volume. This is a just a normal directory that lives outside of the Union File System used by images. Volumes live until no containers link to them and they are explicitly deleted.

which is why you need externally managed volumes like @smac90 wrote.

Source: https://github.com/docker-library/postgres/blob/5c0e796bb660f0ae42ae8bf084470f13417b8d63/Dockerfile-debian.template#L186

  • Related