How Do I Keep a CoreOS Container Running Across Reboots?

Problem: When running a single Docker container in CoreOS on a DigitalOcean Droplet, occasionally CoreOS is restarted by the service. This causes the Docker container to stop, but it is not restarted.

To correct this, the container must be run with the --restart always option.

The script below starts or runs the container no matter what state the container is in:

containerexists()
{
  docker ps -a --format {{.Names}}|grep -q "^$1$"
}
containerstopped()
{
  docker ps -a --format {{.Names}},{{.Status}}|grep -q "^$1,Exited"
}
containerrunning()
{
  docker ps -a --format {{.Names}},{{.Status}}|grep -q "^$1,Up"
}

if containerexists ghost.app && containerstopped ghost.app
then
  echo ghost.app exists and is stopped. Starting it.
  docker start ghost.app
elif containerexists ghost.app
then
  echo ghost.app is running
elif ! containerexists ghost.app
then
  echo ghost.app needs a container, running it, with restart option.
  docker run --restart always -dp 80:2368 --name ghost.app -v $(pwd)/blog:/var/lib/ghost ghost
else
  echo ghost.app is in an unknown state.
fi