When working with Docker, you might occasionally find that trying to stop a container using Ctrl + C
just doesn’t work. It’s one of those little annoyances that can throw off your workflow. I ran into this issue recently and dug into what’s happening under the hood—and more importantly, how to fix it. Here’s what I found.
Thank me by sharing on Twitter 🙏
The Problem: Why Ctrl + C Isn’t Working
In most cases, when you hit Ctrl + C
, it sends a SIGINT
(interrupt) signal to the running process, which should gracefully shut down the container. However, not all processes inside the container are designed to handle this signal properly. Sometimes, the process inside might be a shell script, or there could be multiple layers of commands that don’t pass along the signal. The result? You end up pressing Ctrl + C
repeatedly with no effect, getting more frustrated by the second.
The Fix: How to Make Ctrl + C Work as Expected
There are a few ways to ensure that your container respects the Ctrl + C
command:
- Use the
--init
Option:
Docker has a built-in option that runs a small init process inside your container, which handles signals properly and forwards them to your main process.- Note:
--init
is different from-i
, which is short for--interactive
- Note:
docker run --init <your-image>
This is the quickest and easiest solution for most cases.
- Make Sure You’re Using Interactive and TTY Modes:
When running containers interactively, always ensure you’re using both-t
(pseudo-TTY) and-i
(interactive) flags:
docker run -it <your-image>
These flags help forward signals like Ctrl + C
more effectively.
Anker USB C to USB C Cable, Type C 60W Fast Charging Cable (6FT, 2Pack) for iPhone 16 Series, iPad Mini 6 and More (USB 2.0, Black)
$9.99 (as of December 21, 2024 08:38 GMT +00:00 - More infoProduct prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on [relevant Amazon Site(s), as applicable] at the time of purchase will apply to the purchase of this product.)TAKAGI for iPhone Charger, [MFi Certified] Lightning Cable 3PACK 6FT Nylon Braided USB Charging Cable High Speed Transfer Cord Compatible with iPhone 14/13/12/11 Pro Max/XS MAX/XR/XS/X/8/iPad
$9.99 (as of December 21, 2024 08:38 GMT +00:00 - More infoProduct prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on [relevant Amazon Site(s), as applicable] at the time of purchase will apply to the purchase of this product.)Highwings 8K 10K 4K HDMI Cable 48Gbps 6.6FT/2M, Certified Ultra High Speed HDMI Cable Braided Cord-4K@120Hz 8K@60Hz, DTS:X, HDCP 2.2 & 2.3, HDR 10 Compatible with Roku TV/PS5/HDTV/Blu-ray
$9.99 (as of December 21, 2024 08:38 GMT +00:00 - More infoProduct prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on [relevant Amazon Site(s), as applicable] at the time of purchase will apply to the purchase of this product.)- Double-Check Your Entrypoint or Command:
If your container runs a script or some other command as its entry point, it’s important to useexec
to ensure signals are handled correctly. For example, if you have a shell script as your entry point, the script should look like this:
#!/bin/sh
exec your-command
Using exec
replaces the shell with your intended command, making sure signals like SIGINT
get passed along.
- Customize the Stop Signal:
If you need more control, you can specify the stop signal Docker should use with the--stop-signal
option:
docker run --stop-signal SIGINT <your-image>
This is useful in cases where you need to override the default stop behavior.
Wrapping Up: Smoothly Shutting Down Your Docker Containers
It’s easy to overlook signal handling when running containers, but it’s crucial for ensuring your applications shut down smoothly. By tweaking a few settings—like adding the --init
flag or adjusting your entrypoint—you can avoid the frustration of unresponsive containers. Next time you hit Ctrl + C
, you’ll know exactly what to do if it doesn’t work as expected.