Worth mentioning the -v switch for the docker run command to get a persistent volume.
It will be slightly different for Windows, the code switch above is on my works MacBook.
- $HOME is a linux inbuilt variable that points to my profile directory. On Windows it will be %HOMEPATH%
- /docker/volumes/postgres is simply the path in my profile where the Postgres data files are stored. It is outside the container, hence the persistence.
- The : is the delimiter that separates out the location of data files on my host from the location of the data files within my docker container.
If I connected to my container I would find the postgres data files in /var/lib/postresql/data/
The other thing I have found useful is the --network switch for when I have a Python app in a docker container that needs to talk to PostGres in a docker container.
docker network create pg_net
docker run --rm --name pg-docker --network pg_net -d -p 5432:5432 -v $HOME/docker/volumes/postgres:/var/lib/postgresql/data postgres
- --rm means remove the container when you terminate. Otherwise the container just sits there in an EXITED state.
- --name is useful when you might be running multiple containers and/or multiple instances of PostGres.
- -d means run in detached mode. without this you have to keep your command window open.
One thing to watch out for is PostGres versions. PostGres 13 has just been released. I'd been working on my local container for a while on Version 12 and, following a reboot, my database became unavailable. Turns out you PostGres13 cannot read PostGres12 data files without some messing around. fortunately the DB scripts were all under source control so a rebuild from scratch took seconds.