Pre-existing shared memory block (key 5432001, ID 32768) is still in use I have been seeing this error lately that is causing the server to crash all of a sudden. I have seen elsewhere that the solution is to delete the postmaster.pid and postmaster.opts files and restart the server, but no one has explained why this event happens, and how to stop it from happening again.
It's vital that if you forcibly terminate PostgreSQL then all postgres processes must be killed. If you don't terminate them all then the shared memory block may still be held and the database files may still be open and being written to. In this case starting PostgreSQL again may cause massive database corruption, which is why PostgreSQL tries to stop you doing this. Overriding those safety measures may cause data loss. 

You should preferably cleanly stop PostgreSQL with pg_ctl as part of the container shutdown process.

In any case, it's very important that stopping the container terminates all postgres processes.

You should not delete postmaster.pid, it's protecting you from data corruption by allowing PostgreSQL to notice that the shared memory segment is still in use. Unless Docker somehow gets in the way of this process.