Postgres is available as a docker image on dockerhub/postgres. Configuration is easily done via environment variables but they also state

Warning: the Docker specific variables will only have an effect if you start the container with a data directory that is empty; any pre-existing database will be left untouched on container startup.

That makes me wonder if there is a safety risk when having a Dockerfile like this

FROM postgres
COPY *.sql /docker-entrypoint-initdb.d/

and a docker-compose.yml like this

            context: ./database/
            dockerfile: ./Dockerfile
            POSTGRES_PASSWORD: test123
            - 5432:5432
            - databasevol:/var/lib/postgresql/data
            - net

        driver: local

        driver: bridge

which will rebuild the current database image based on postgres:latest which may lead to the database being initialised by version 12.4 but later being read by version 13.0. The possibility of having the DBMS identify the data directory as corrupted and purging it horrifies me.

Hence the question, is it safe to run databases in docker containers?


I would advise against using postgres:latest and instead specify the major version you want (ie. postgres:12).

No respectable DBMS will drop your database when updating the DBMS, however it might refuse to start (as does PostgreSQL).

You can fairly safely (always have backups nevertheless) upgrade the cluster when switching major version through any of the methods listed in the official documentation.


There may be some minor performance issues for DBs run by docker but nothing else is different.

But you should never use "latest" as your image tag in production environment; It's like turning on automating update !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.