2

I have a question about stop database container.

  • I have a docker image that runs postgresql. When I create a container with this image:

sudo docker run --name db -d asg1612/postgresql

  • it run the command:

/usr/lib/postgresql/9.3/bin/postgres -D /var/lib/postgresql/9.3/main -c config_file=/etc/postgresql/9.3/main/postgresql.conf

  • and the directory /var/lib/pgdata is create as container volume

  • I stop the container with:

sudo docker stop db

Will the database have stopped gracefully?

Will I have corrupt data?

  • 1
    Whether or not the database stops "gracefully" you will not have corrupt data. PostgreSQL is crash-safe unless you configure it otherwise, so long as the underlying file system and disk storage respect disk flush requests (fsync). You lose in-progress transactions, but nothing is corrupted or left half-done. – Craig Ringer Aug 30 '14 at 13:10
  • Many Thanks @CraigRinger, this answer solutions my question!!! – Andrés Sánchez García Sep 1 '14 at 6:18
5

Whether or not the database stops "gracefully" you will not have corrupt data. PostgreSQL is crash-safe unless you configure it otherwise, so long as the underlying file system and disk storage respect disk flush requests (fsync). You lose in-progress transactions, but nothing is corrupted or left half-done. This is true even if you SIGKILL (kill -9) the server, though that doesn't make it a good idea to do that routinely.

The main issue you'll have is that if the PostgreSQL server is shut down abruptly it might take longer to start up next time because it has to do more work to recover work that's committed but not yet fully applied. (oversimplification).

If there's no init running in the container (sysvinit/systemd/upstart) then most likely no graceful shutdown is performed, unless you've given docker some kind of shutdown script explicitly. (I haven't worked with docker yet). Some quick searches suggest that it just sends a SIGTERM to the process(es) in the container. This is fine for PostgreSQL and is in fact what pg_ctl -m smart does. It might take a while to stop, so you may choose to use SIGINT to force transaction aborts and shut down faster.

  • 2
    SIGTERM will cause the server to shut down only after all existing connections have terminated. So if you have open connections, docker stop will NOT necessarily give you a graceful shutdown, because it will wait 10 seconds and then send SIGKILL. – mhsmith Feb 20 '15 at 17:41

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.