How does "restart: always" policy work in docker-compose?
When you use docker kill, this is the expected behavior as Docker does not restart the container: "If you manually stop a container, its restart policy is ignored until the Docker daemon restarts or the container is manually restarted. This is another attempt to prevent a restart loop" (reference)
If you use docker stop or docker kill, you're manually stopping the container. You can do some tests about restart policies: restarting the docker daemon, rebooting your server, using a CMD inside a container and running an exit...
For example if I kill my container deployed with a restart policy, I see that it exited with code 137 but it is not restarted according to docker ps -a, it remains exited:
[root@andromeda ~]# docker ps --all
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
819d1264c30a redis:alpine "docker-entrypoint..." 3 minutes ago Exited (137) 34 seconds ago keepalive_redis_1
But if I restart the daemon...
[root@andromeda ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
819d1264c30a redis:alpine "docker-entrypoint..." 30 minutes ago Up 2 seconds 6379/tcp keepalive_redis_1
The container that was set with restart policy, starts again which is what documentation say, so docker kill is not the way you should test the restart policy as it's assumed that you have deliberately stopped the container and Docker wants to have a way to prevent restarting loops, if you kill it, you really want to kill it.
I found the following links valuable that show the same behavior in different versions (so it's not a bug but the expected behavior):
- How to ensure that containers restart
- The same behavior expected when you use docker kill
- And another post about docker kill and restart