Docker – Stop postgresql container

dockerpostgresql

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?

Best Answer

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.

Related Topic