0

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

    database:
        build:
            context: ./database/
            dockerfile: ./Dockerfile
        environment:
            POSTGRES_PASSWORD: test123
        ports:
            - 5432:5432
        volumes:
            - databasevol:/var/lib/postgresql/data
        networks:
            - net

volumes:
    databasevol:
        driver: local

networks:
    net:
        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?

3

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.

| improve this answer | |
1

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 !

| improve this answer | |

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.