Can I build a docker image from an existing database container?
My goal is to have a database container persists or not persist data, which can be pulled by other developers without many manual steps (pg_dump and pg_store etc) to run their dev environment locally.
I want to know if I can build a docker image of a snapshot of a database (postgres). Is this possible? If not, what is the recommended way to do this with docker?
One Solution collect form web for “Can I build a docker image from an existing database container?”
In docker you can save data in two places basically:
- Inside the container (default)
- A volume
The database containers are configured to save data in volumes, because in this way the data can survive a container deletion. Also it is faster. When you create an image from a container, the data from volumes won’t get into.
But you can create a database container which have an archive script, which will execute
cp and save the result in any directory outside any volume mounted. So the data will be inside the container and can fly out to an image.
docker commit <container id> to save the changes to a new image. Tag the new image
docker tag <image id> registry/postgres:withdata and do a
Dockerfile include a script which after the database startup, check if it is empty. If it is empty restore it using
cp from the container.
Well it is only an idea to route you a little. It is possible, just require a little work.