You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在IntelliJ调试/运行配置结束后执行Shell命令停止Docker容器

How to Execute a Shell Script After IntelliJ Run Configuration Ends (Success or Failure)

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:

  1. Open your target Run/Debug Configuration (go to Run > Edit Configurations...).
  2. Scroll down to the Post-run tasks section at the bottom of the config window.
  3. Click the + button and select Run External tool.
  4. 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.
  5. 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:

  1. 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
    
  2. Make the script executable: chmod +x run-app-with-docker.sh.
  3. In your IntelliJ Run Configuration, replace the original run command with this script (set the "Command" field to the path of your .sh file).
    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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 09:54:51