How to upgrade software (e.g. PostgreSQL) running in a Docker container?

When using the base PostgreSQL Docker image, you choose a PostgreSQL version, spin up a container and you're running. Great! But what the the correct/best way to upgrade to a newer version of PostgreSQL in the future?


Solution 1:

There is no easy solution for this at the moment, but the simplest I've found is to pg_dump the old version and then load that dump into the newer version.

There is a fair bit of discussion at https://github.com/docker-library/postgres/issues/37 on possible solutions to this problem, and a proof of concept for a docker-based upgrade at https://github.com/tianon/docker-postgres-upgrade

Solution 2:

After running PostgreSQL in a container for over a year I tend to think databases aren't that great to containerize for exactly reasons like this. Containers are not intended to be used like VMs, and so every aspect of the tooling will fight you if you try to treat it like a VM that you administer. Containers are like glorified chroots - isolation and reproducible environments for a particular application, and the less configuration that application requires the better.

All that said, if you insist that you must run PostgreSQL in a container, at the very least you should mount a volume from somewhere to put the data files on so that you can safely destroy or modify your database server image (for things like PostgreSQL upgrades). Any configuration that has to be done should be done in the Dockerfile so that upgrades are also handled by editing the Dockerfile - if you're doing a minor upgrade for example, you might only just need to change the tag of the FROM image in your Dockerfile.