Handling PID 1 and graceful shutdown in Docker containers
Why docker stop hangs for 10 seconds before sending SIGKILL, and how exec form, tini, and Spring Boot graceful shutdown prevent dropped requests.
When
docker stopstalls for exactly 10 seconds and ends with exit code137, your container is being force-killed rather than shut down cleanly. This post traces the problem to Dockerfile shell form and Linux PID 1 signal rules, then walks through fixing it withtiniand Spring Boot graceful shutdown.
1. The 10-second hang and dropped traffic
During Kubernetes rolling updates or Docker Compose deployments, a container sometimes refuses to stop right away. Instead, the stop command blocks for about 10 seconds and then exits abruptly:
1
2
3
4
5
6
7
8
9
$ time docker stop payment-service
payment-service
real 0m10.284s
user 0m0.031s
sys 0m0.024s
$ docker inspect payment-service --format='{{.State.ExitCode}}'
137
Exit code 137 tells the whole story. On Linux, exit codes above 128 mean a signal terminated the process, and 137 = 128 + 9 (SIGKILL). The application never exited on its own—the kernel killed it.
In production, a forced kill causes immediate fallout:
- In-flight checkout or payment HTTP requests drop with
502 Bad Gatewayor connection resets. - Active database transactions abort mid-flight, and connection pools like HikariCP never close connections cleanly.
- Kafka or RabbitMQ consumers miss offset commits, causing duplicate message processing after restart.
To fix this, it helps to trace what docker stop actually sends to the container.
2. How docker stop works under the hood
docker stop does not kill a container immediately. It tries a graceful shutdown first and only escalates after a timeout:
sequenceDiagram
autonumber
actor CLI as Deploy Script (CLI)
participant Daemon as Docker Daemon
participant Container as Container (PID 1)
CLI->>Daemon: docker stop payment-service
Daemon->>Container: Send SIGTERM (15)
Note over Container: Expected path:<br/>finish active work and exit 0
rect rgb(255, 230, 230)
Note over Daemon,Container: Wait 10s grace period (--time 10)<br/>PID 1 ignores signal or never exits
end
Daemon->>Container: Send SIGKILL (9)
Note over Container: Kernel kills process immediately
Container-->>Daemon: Exit Code 137 (128 + 9)
Daemon-->>CLI: Stop completed (~10.2s)
- The Docker daemon sends
SIGTERM(signal15) to PID 1 inside the container. - It waits up to the grace period—10 seconds by default (
-t/--time)—for the process to finish cleanup and exit. - If PID 1 is still running after 10 seconds, Docker sends
SIGKILL(signal9), which cannot be caught or ignored.
A consistent 10-second shutdown delay is a clear sign that the application process either never received SIGTERM or ignored it.
3. Root cause: Dockerfile shell form and Linux PID 1 rules
Two separate layers cause this behavior: how the Dockerfile launches the process, and how the Linux kernel treats PID 1.
3.1 Shell form swallows signals
The most common cause is writing ENTRYPOINT (or CMD) in shell form:
1
2
# Bad: shell form wraps the command in /bin/sh -c
ENTRYPOINT java -jar /app/payment-service.jar
Docker runs a shell-form instruction as:
1
/bin/sh -c "java -jar /app/payment-service.jar"
Inside the container namespace, the process tree looks like this:
flowchart TD
subgraph Host["Host OS"]
DockerDaemon["Docker Daemon"]
end
subgraph Container["Container Namespace (Shell Form)"]
PID1["PID 1: /bin/sh"]
PID2["PID 7: java -jar app.jar (child)"]
PID1 -->|"fork/exec"| PID2
end
DockerDaemon -->|"1. Send SIGTERM"| PID1
PID1 -.->|"Does not forward signal"| PID2
DockerDaemon ==>|"2. Send SIGKILL after 10s"| PID1
Docker delivers SIGTERM only to PID 1, which is /bin/sh. Standard POSIX shells (/bin/sh, /bin/bash) do not forward received signals to child processes automatically. The JVM keeps serving traffic without ever knowing a shutdown started, until SIGKILL wipes out the container 10 seconds later.
3.2 Linux kernel signal rules for PID 1
Switching to exec form puts the JVM directly at PID 1, which solves the shell wrapper issue. Even then, PID 1 has special kernel semantics you need to account for.
Linux kernel protection for PID 1
For an ordinary Linux process, an unhandledSIGTERMterminates the process via the kernel’s default action.
For PID 1 (the init process), the kernel drops any signal unless PID 1 explicitly registers a handler for it.
If an application does not register a SIGTERM handler—or receives SIGTERM before its runtime finishes initializing signal handlers—the signal is silently ignored. PID 1 is also responsible for adopting orphaned child processes and calling wait() to reap them. Without proper reaping, short-lived subprocesses pile up as zombies inside long-running containers.
4. Step 1: Switch to exec form
First, rewrite ENTRYPOINT using exec form (JSON array syntax) so Docker starts the JVM directly without spawning /bin/sh:
1
2
# Good: exec form runs Java directly without /bin/sh
ENTRYPOINT ["java", "-jar", "/app/payment-service.jar"]
With exec form, SIGTERM goes straight from the Docker daemon to the JVM, triggering Spring Boot’s shutdown hook:
flowchart TD
subgraph Container["Container Namespace (Exec Form)"]
PID1["PID 1: java -jar app.jar"]
end
DockerDaemon["Docker Daemon"] -->|"SIGTERM (15) directly"| PID1
PID1 -->|"Spring graceful shutdown"| PID1
PID1 -->|"Exit Code 0"| DockerDaemon
5. Step 2: Run a lightweight init process (tini)
Putting a general-purpose runtime (Java, Node.js, Python) at PID 1 works for basic signal delivery, but it still leaves two gaps:
- If your service invokes helper binaries or subprocesses that exit abnormally, nothing reaps orphaned children, leading to zombie process leaks.
- Application runtimes aren’t built to forward signals cleanly across an entire child process tree.
The standard fix is tini, a tiny init binary (tens of kilobytes) built specifically for containers:
flowchart TD
DockerDaemon["Docker Daemon"] -->|"SIGTERM (15)"| Tini["PID 1: tini"]
subgraph Container["Inside Container"]
Tini -->|"Signal forwarding"| App["PID 2: java -jar app.jar"]
Tini -.->|"Zombie reaping"| Zombie["Orphaned child"]
end
tini sits at PID 1 and handles two jobs with negligible overhead:
- Signal forwarding: passes incoming signals reliably to the child process group.
- Zombie reaping: calls
wait()on terminated orphans so PIDs and process table entries don’t leak.
5.1 Using Docker’s built-in --init flag
Docker Engine (1.13+) bundles tini. For standalone containers, pass --init:
1
docker run -d --name payment-service --init -p 8080:8080 payment-service:latest
In Docker Compose, enable init: true and give the container a matching grace period:
1
2
3
4
5
6
7
8
# compose.yaml
services:
payment-service:
image: payment-service:latest
init: true # Runs bundled tini as PID 1
stop_grace_period: 30s # Allow 30s instead of the 10s default
ports:
- "8080:8080"
5.2 Embedding tini in the Dockerfile
Orchestrators like Kubernetes don’t expose Docker’s --init flag directly. If you build images for Kubernetes or mixed runtimes, baking tini into the image keeps behavior consistent everywhere:
1
2
3
4
5
6
7
8
9
10
11
# Dockerfile
FROM eclipse-temurin:21-jre-alpine
# Install tini via apk
RUN apk add --no-cache tini
WORKDIR /app
COPY target/payment-service.jar app.jar
# Run tini as PID 1 and pass the JVM command after --
ENTRYPOINT ["/sbin/tini", "--", "java", "-jar", "app.jar"]
On Debian or Ubuntu base images
Install it viaapt-get install -y --no-install-recommends tini(typically/usr/bin/tini), or download the static binary from the officialtiniGitHub releases to/tiniand setENTRYPOINT ["/tini", "--", "..."].
6. Wiring up Spring Boot graceful shutdown and verifying it
Once SIGTERM reaches the JVM reliably, Spring Boot needs to drain active requests instead of closing the web server immediately.
6.1 Spring Boot configuration
Since Spring Boot 2.3, embedded web servers (Tomcat, Jetty, Reactor Netty, Undertow) support graceful shutdown natively:
1
2
3
4
5
6
7
8
# src/main/resources/application.yml
server:
port: 8080
shutdown: graceful # Default is immediate
spring:
lifecycle:
timeout-per-shutdown-phase: 20s # Max wait for active requests
server.shutdown: gracefulstops accepting new HTTP requests right away while allowing already-accepted requests to complete.spring.lifecycle.timeout-per-shutdown-phase: 20scaps the drain window. If all active requests finish earlier, the application exits immediately with code0.
Watch the timeout ordering
Keep the application shutdown timeout (20s) strictly shorter than Docker’sstop_grace_period(or KubernetesterminationGracePeriodSeconds, e.g.,30s). If the framework waits longer than the container runtime, Docker sendsSIGKILLbefore Spring finishes closing pools and beans.
6.2 End-to-end verification
To verify the full path, I tested a payment endpoint (/api/v1/payments/process) that takes 5 seconds to complete, and ran docker stop 1 second into the request:
- Client sends a 5-second request to
/api/v1/payments/process. - After 1 second, a second terminal executes
docker stop payment-service. - Confirm the client gets
200 OKwithout a connection reset. - Confirm the container exits right after the request finishes with exit code
0(not137after 10 seconds).
1
2
3
4
5
6
7
# Terminal 1: trigger a 5-second request
$ curl -i http://localhost:8080/api/v1/payments/process
HTTP/1.1 200 OK
Content-Type: application/json
Transfer-Encoding: chunked
{"status":"SUCCESS","message":"Payment completed successfully."}
1
2
3
4
5
6
7
8
9
10
# Terminal 2: stop the container while the request is in flight
$ time docker stop payment-service
payment-service
real 0m4.892s
user 0m0.028s
sys 0m0.019s
$ docker inspect payment-service --format='{{.State.ExitCode}}'
0
1
2
3
4
5
6
7
# Application logs (docker logs payment-service)
2026-06-06 12:05:01.120 INFO [payment-service] : Commencing graceful shutdown. Waiting for active requests to complete
2026-06-06 12:05:01.125 INFO [payment-service] : Web server will stop accepting new connections
2026-06-06 12:05:05.890 INFO [payment-service] : Active request processed successfully: /api/v1/payments/process
2026-06-06 12:05:05.895 INFO [payment-service] : Graceful shutdown complete
2026-06-06 12:05:06.012 INFO [payment-service] : HikariPool-1 - Shutdown initiated...
2026-06-06 12:05:06.025 INFO [payment-service] : HikariPool-1 - Shutdown completed.
real 0m4.892s: the container exited as soon as the active request finished instead of stalling for 10 seconds.ExitCode: 0: clean shutdown rather than aSIGKILL(137).HikariPool-1and the Spring application context closed in order after HTTP traffic drained.
7. Production checklist
Before shipping a containerized backend service, verify these four settings together:
| Check | Recommended setting | Pitfall to avoid |
|---|---|---|
| Dockerfile syntax | Exec form (ENTRYPOINT ["cmd", "arg"]) | Shell form (ENTRYPOINT cmd) puts /bin/sh at PID 1 and blocks SIGTERM |
| Init process | tini via --init, init: true, or baked into the image | Skipping init leaves orphaned subprocesses unreaped as zombies |
| Timeout budget | App shutdown timeout < container grace period | Setting Spring to 30s while Docker waits 10s still triggers SIGKILL |
| Web server shutdown | server.shutdown: graceful | The default immediate mode drops active requests as soon as SIGTERM arrives |
Startup time gets plenty of attention in container tuning, but clean shutdown matters just as much during rolling deploys. Combining exec form, tini, and aligned graceful shutdown timeouts eliminates both the 10-second deploy stall and dropped in-flight requests.