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.

  • Adding nodes on the fly with Docker Swarm
  • How to name Dockerfiles
  • Docker service does not start
  • eval “$(docker-machine env default)” leads to Error checking TLS connection
  • docker: unauthorized: authentication required
  • Why Jenkins not stored data?
  • 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.

  • mysql with Exited(1) from docker
  • Sentry installed via docker - how to set up https?
  • How to gracefully shut down a server running in docker
  • add PPA failed in Docker container
  • Docker using gosu vs USER
  • Crossbar 0.12.1 : No module named django - wsgi error
  • 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.