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.

  • Will docker container auto sync time with the host machine?
  • Docker [error response]: Failed to choose a UCP node. UCP node does not exist.
  • Avoiding port conflicts between docker-compose.yml configurations?
  • Docker virtual server
  • GitlabCI free shared runners to provision Ubuntu instead of CoreOS?
  • docker with btrfs ubuntu
  • 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.

  • How should I create Dockerfile to run multiple services through docker-compose?
  • Pushing to docker registry fails: Error mounting /dev/mapper/…: device or resource busy
  • bind mounting a volume in docker fails with lxc-start: No such file or directory
  • AWS Docker deployment
  • Gitlab runner on kubernetes
  • docker build apt-get update failed with custom network
  • 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.