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 to deal with persistent storage (e.g. databases) in docker
  • Using docker within vagrant behind a proxy
  • What is the better way to do service discovery on Docker environment?
  • Meteor build locally or on aws host
  • Custom Production Meteor App /w Docker - Build Fails on Dependecies
  • Where can I create images from? Locally at all? (Docker remote API)
  • 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 to persist changes in boot2docker
  • How do I enter this dockerfile / nginx container?
  • Enable to run Docker command error
  • docker compose inside docker machine, can't mount volume
  • Running Lightttpd in Docker: ulimits not honored
  • gcloud connect to docker Daemon fail
  • 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.

    Regards

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