Can I build a docker image from an existing database container?

I am new to docker. I am using docker compose to manage containers.

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.

  • How do I access Windows eventlog of a Docker container
  • Docker - Cannot remove dead container
  • Elastic Beanstalk with multiple environments and URL routing between them
  • continuous integration of docker image by running locally with its own IP?
  • 'clear' doesn't clean the screen when I run docker on Windows Command Prompt
  • anyway to setup npm credentials over `npm login` without reading input from STDIN?
  • 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?

    Thank you.

  • docker-compose not overriding dockerfile environment variables
  • Nancy on Linux is using 100% CPU
  • How to copy files from local machine to docker container on windows
  • Some exposed Docker ports are not accessible from outside (dovecot, postfix)
  • Couchdb cartridge not responding in docker image
  • Debian : Using a cron task on 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:

    1. Inside the container (default)
    2. 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 pg_dump or 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.

    Then use 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 docker push.

    In the Dockerfile include a script which after the database startup, check if it is empty. If it is empty restore it using pg_restore or cp from the container.

    Well it is only an idea to route you a little. It is possible, just require a little work.


    Docker will be the best open platform for developers and sysadmins to build, ship, and run distributed applications.