Last updated 137 days ago by Craig Ringersql
Docker Hub carries Docker images for PostgreSQL, based on Debian Stretch or Alpine Linux. These are not official PostgreSQL Development Group (PGDG) images from postgresql.org, they’re maintained in the Docker Library on Github. But as Docker adoption grows these are going to become more and more people’s first exposure to PostgreSQL.
I tried these images out as I needed some convenient and simple Docker images for PostgreSQL in a continuous integration job. It wasn’t entirely fuss-free, but overall the images worked well. I’d like to share some tips.
WARNING: by default these images make it very easy to lose your data forever. Personally I think that’s a significant defect and they should refuse to run without either a mounted volume for the data or a env-var passed to “docker run”. It’s discussed in “Caveats” in the docs provided, but not IMO nearly prominently enough.
If you want persistent storage you really need to supply a Docker volume to use when you first run the new container:
docker run -v my_pgdata:/var/lib/postgresql/data postgres:11
Docker will auto-create the volume
my_pgdata if it doesn’t exist. Or you can use a host-path to use a bind mount; see the postgres docker image documentation under “Where to Store Data”.
postgres images, by default, create a PostgreSQL instance under
/var/lib/postgresql/data, which is in the container’s private storage unless mapped to a volume. When the container exits you lose access to the container storage. The data directory is not immediately deleted, but will be lost forever if you clean up exited docker containers with
docker system prune, etc.
To recover access to the container’s data once the container exits but before such permananent removal has occurred, find the container id with
docker ps -a. You might find
docker ps -f ancestor=postgres:11 -a useful. Once you have the container ID you can
docker start the container to start it again, perhaps to
pg_dump its data. It’ll start with any port bindings preserved. Alternately, you can
docker cp the data directory in
/var/lib/postgresql/data out of the stopped container so you can copy it into a persistent Docker volume and attach that to a new instance of the container.