5

I have a running postgreSQL docker container and need to add a volume mount.

I followed the steps from How can I add a volume to an existing Docker container?, (ran docker commit on the container to save it as an image, and spun up another container based on that image with a named volume mounted in). All the data files from the first container are present in /var/lib/postgres/data of the second container.

However, when I try to query this second postgres database, I cannot see any tables that are in the first container. Been trying to fix this for a few days with no luck, am I missing something here (does mounting a volume obscure the existing data in /var/lib/postres/data)?

L H
  • 1,145
  • 3
  • 12
  • 25

1 Answers1

3

Commit will not work as there is the volume defined in the Dockerfile.

Volumes are useful in many cases, for example, for running database-storage. However, since volumes are not 'part' of a container, it makes containers no longer portable - which seems in direct conflict with the slogan "Build once... Run anywhere.."

docker commit data container with VOLUME

One option that you can try is copying data folder to host from an existing container and then launch the container with mount path.

docker cp my_db_cotainer:/var/lib/postgresql/data  db_data

then start a new container with this path so it will contain the same data as the previous one

docker run -d --name some-postgres -v $PWD/db_data/:/var/lib/postgresql/data postgres

same for mysql

docker cp some-mysql-old:/var/lib/mysql db_backup
docker run --rm --name some-mysql-new -v $PWD/db_backup:/var/lib/mysql  -it mysql
Adiii
  • 54,482
  • 7
  • 145
  • 148
  • Worked perfectly, thanks so much! Still not too clear how it worked though. I see that volume is define in the Dockerfile, but it seems like both what I was doing and what you suggested mounted the data into /var/lib/postgresql/data, what is the difference? – L H Jul 10 '20 at 04:14
  • in the simple word, volume is not part of docker commit , so when mount directory it hide what inside docker and host files. – Adiii Jul 10 '20 at 05:27