如何在IntelliJ调试/运行配置结束后执行Shell命令停止Docker容器
Got it, let's tackle this problem—you've got the pre-launch Docker container setup working via the "Before Launch external tool command", now need to ensure the container stops when your run config finishes, whether it exits normally or crashes. Here are a few reliable methods to set up post-run actions in IntelliJ:
Method 1: Use Post-run Tasks (Modern IntelliJ Versions)
This is the most straightforward approach if you're on IntelliJ 2021.3 or newer:
- Open your target Run/Debug Configuration (go to
Run > Edit Configurations...). - Scroll down to the Post-run tasks section at the bottom of the config window.
- Click the
+button and select Run External tool. - Configure your stop-container script:
- If you haven't already, create an external tool pointing to your shell script (or directly run the Docker command:
docker stop <your-container-name>). - Make sure to check the box labeled Always run, even if the main task fails—this ensures the container stops regardless of how your program exits.
- If you haven't already, create an external tool pointing to your shell script (or directly run the Docker command:
- Save the configuration, and next time you run it, the post-task will trigger automatically after your program finishes.
Method 2: Wrap Your Run Command in a Shell Script (Works for All Versions)
If you're on an older IntelliJ version without post-run tasks, wrap your entire workflow in a single shell script:
- Create a script (e.g.,
run-app-with-docker.sh) with the following logic:#!/bin/bash # Start your Docker container first docker start <your-container-name> # Run your main program (replace this with your actual run command from IntelliJ) <your-program-execution-command> # Capture the exit code of your program EXIT_CODE=$? # Stop the container no matter what docker stop <your-container-name> # Exit with the same code as your program so IntelliJ shows the correct status exit $EXIT_CODE - Make the script executable:
chmod +x run-app-with-docker.sh. - In your IntelliJ Run Configuration, replace the original run command with this script (set the "Command" field to the path of your
.shfile).
Now, whenever you run this config, the script handles both starting the container, running your app, and stopping the container afterward.
Method 3: Add a Shutdown Hook (For Java Projects)
If you're working on a Java application, you can add a JVM shutdown hook directly in your code to stop the container when the JVM exits:
public class Main { public static void main(String[] args) { // Register shutdown hook Runtime.getRuntime().addShutdownHook(new Thread(() -> { try { // Execute the Docker stop command Process process = new ProcessBuilder("docker", "stop", "<your-container-name>") .redirectErrorStream(true) .start(); process.waitFor(); } catch (IOException | InterruptedException e) { e.printStackTrace(); } })); // Your application logic here } }
This hook will trigger whenever the JVM exits normally, or when it's terminated (except for forced kills like kill -9). It's a good option if you want the cleanup tied directly to your application lifecycle.
内容的提问来源于stack exchange,提问作者Andy

