Restart a container with a specific signal
In this step, you will learn how to restart a Docker container by sending a specific signal to the container's main process. While docker restart sends a default stop signal (SIGTERM) followed by a kill signal (SIGKILL) after a timeout, you might need to send a different signal depending on the application running inside the container. You can achieve this by first stopping the container with a specific signal using docker stop and then starting it again with docker start.
Let's use the my-nginx container again. Ensure it is running.
docker ps
If it's not running, start it:
docker start my-nginx
Now, we will stop the container by sending the SIGKILL signal. This signal immediately terminates the process without allowing it to perform any cleanup.
docker stop -s SIGKILL my-nginx
You should see the container name printed, and if you run docker ps, the container should no longer be listed.
docker ps
The container is now stopped. To restart it, we simply start it again.
docker start my-nginx
The container should now be running again. Verify with docker ps.
docker ps
You should see my-nginx listed with a status of Up.
While SIGKILL is a forceful stop, you can send other signals as well, such as SIGHUP or SIGINT, if your application is configured to handle them for graceful shutdowns or reloads. However, the nginx container's default configuration handles SIGTERM gracefully.
Let's stop the container again, this time using the default SIGTERM signal (which is the default for docker stop if no signal is specified).
docker stop my-nginx
Verify that it is stopped:
docker ps
Now, start it one last time.
docker start my-nginx
Verify it is running:
docker ps
By combining docker stop -s <signal> and docker start, you have more granular control over how a container is stopped before being restarted.