A little happy that Nginx's Docker container defaults to graceful shutdown

This article said that I was happy that Nginx's official Docker Image ** defaulted to graceful shutdown when the container shut down **. (1.19.5 or later)

Prerequisites

We hope that the following people will be able to feel the joy as much as possible.

What is graceful shutdown?

According to the wiki, "The program detects a serious error and terminates normally with some control." If you limit the web service to a container, you will have the following requirements.

Traditional Nginx container behavior

Previously (v1.19.4 and below) when exiting an Nginx container, a SIGTERM signal was sent. (It can be seen by setting the error log level of Nginx to notice or lower. The following log is for v1.19.3) image.png This is because the default behavior of docker and Kubernetes is to send a SIGTERM signal when the container is closed. See also: docker stop, docker-compose, Kubernetes

The problem is that ** when Nginx receives the SIGTERM signal, it immediately transitions to shutdown behavior **. (From Nginx Official) Therefore, if you operate with the default settings, a 502 error may occur in some cases.

Workaround so far

To avoid this phenomenon, ** send a SIGQUIT signal to the Nginx container **. (From Nginx Official) The typical method is as follows.

It's a little bit, but in both cases, I felt that "I didn't know that!" Or "It's a hassle to process ...".

Future workaround

You don't have to do anything. Images of Nginx 1.19.5 and above will override the STIOPSIGNAL definition above and will send a SIGQUIT signal by default. (The following is for v1.19.5) image.png This makes it unlikely that you'll have to mess with Dockerfile or define a preStop for graceful shutdown! I'm a little happy.

Summary

If graceful shutdown is a requirement service using an Nginx container, v1.19.5 or higher may be a bit easier. However, please note that depending on the configuration and tools, graceful shutdown of Nginx alone may not meet the requirements.

Recommended Posts

A little happy that Nginx's Docker container defaults to graceful shutdown
How to get a heapdump from a Docker container
I made a Docker container to run Maven
Create a Docker container to convert EPS to PGF source
Build a container that automatically formats Docker x Laravel code
I can no longer connect to a VM with a Docker container that can be connected via SSH
Let's create a Docker container that can connect to CentOS 8 with the minimum configuration by SSH
A solution to Docker errors that beginners tend to get stuck in
Introduction to Linux Container / Docker (Part 1)
How to make a Java container
Run React on a Docker container
Introduction to Linux Container / Docker (Part 2)
Run PureScript on a Docker container
Pass environment variables to docker container
[Docker] Operation up to container creation # 2
How to quickly create a reverse proxy that supports HTTPS with Docker
A shell script that builds a Docker image and pushes it to ECR
How to start a Docker container with a volume mounted in a batch file