Question

Why does Docker database keep getting removed every night?

I’ve got a 2GB Basic (22.04) Droplet running Docker for a my portfolio site (currently, will be hosting my other projects to display my previous projects). I’m new to Docker being ran for production, I’ve been using it mainly to run postgres, elasticsearch for development. Below is my docker-compose file for the current build I am having issues with.

version: "3.10"

services:
    backend:
        build:
            context: .
            dockerfile: Dockerfile
        restart: always
        volumes:
            - static_volume:/backend/static
            - media_volume:/backend/media
        ports:
            - "8000:8000"
        command: >
            sh -c "python manage.py collectstatic --noinput &&
            python manage.py migrate &&
            gunicorn project.wsgi:application --bind 0.0.0.0:8000"
        depends_on:
            - db

    db:
        image: postgres
        volumes:
            - postgresdata:/var/lib/postgresql/data:rw
        env_file:
            - ".env"
        ports:
            - "5432:5432"
        container_name: xxxx_db

    frontend:
        build:
            context: ./frontend
            dockerfile: Dockerfile
        volumes:
            - frontend_build:/frontend/build
    nginx:
        image: nginx:latest
        restart: always
        ports:
            - "81:8080"
        volumes:
            - ./nginx/nginx-setup.conf:/etc/nginx/conf.d/default.conf:ro
            - frontend_build:/var/www/frontend
            - static_volume:/var/www/backend/static
            - media_volume:/var/www/backend/media
        depends_on:
            - db
            - backend
            - frontend

volumes:
    frontend_build:
    static_volume:
    media_volume:
    postgresdata

It seems that every night the db is just being wiped, I’m able to re-run the migrations, docker compose down and up where the data will persist. But in the morning I get a bunch of errors from django implying that the database is no longer there.

I checked the xxxx_db logs and noticed this in the early hours of the morning:

2023-11-10 00:50:06.222 UTC [22891] FATAL:  expected SASL response, got message type 0
2023-11-10 01:09:59.536 UTC [23012] FATAL:  unsupported frontend protocol 0.0: server supports 3.0 to 3.0
2023-11-10 01:09:59.823 UTC [23013] FATAL:  unsupported frontend protocol 255.255: server supports 3.0 to 3.0
2023-11-10 01:10:00.117 UTC [23014] FATAL:  no PostgreSQL user name specified in startup packet
2023-11-10 01:35:07.437 UTC [23185] FATAL:  database "template0" is not currently accepting connections
2023-11-10 01:37:09.770 UTC [23252] ERROR:  cannot drop the currently open database
2023-11-10 01:37:09.770 UTC [23252] STATEMENT:  DROP DATABASE postgres;
2023-11-10 01:37:10.886 UTC [23253] ERROR:  cannot drop a template database
2023-11-10 01:37:10.886 UTC [23253] STATEMENT:  DROP DATABASE template1;
2023-11-10 01:37:12.020 UTC [23254] ERROR:  cannot drop a template database
2023-11-10 01:37:12.020 UTC [23254] STATEMENT:  DROP DATABASE template0;

Any help would be greatly appreciated as it’s driving me insane!

Thanks! Matt


Submit an answer


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Hi Matt! It looks like we’re facing an identical issue, with the same conf: 2GB Basic (22.04) + Django. On the postgress logs, we found: 023-12-10 14:32:19.693 UTC [8235] FATAL: database “template0” is not currently accepting connections 2023-12-10 14:32:20.982 UTC [8238] ERROR: cannot drop the currently open database 2023-12-10 14:32:20.982 UTC [8238] STATEMENT: DROP DATABASE postgres;

Have you found a fix for this?

thanks a lot! <3

Bobby Iliev
Site Moderator
Site Moderator badge
November 13, 2023

Hi there,

Your Docker setup actually looks correct. It is indeed odd that your databases are getting dropped.

Can you share your Dockerfiles here too?

In the meantime, what you could try checking is that there are no cron jobs running that might be executing these commands both on the server itself and in the Docker containers.

Also, do you have any backup scripts or maintenance scripts that might be causing this behavior?

One more thing that you should check is that your database port is closed so it is not publicly accessible. Basically, check if your database ports are exposed to the internet and make sure that only your application can access them.

Best,

Bobby

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Featured on Community

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more