1

我的自定义 docker-compose 命令不会优雅地停止。为什么以下不起作用,我该如何正确修复它(即没有 SIGKILL)?

我编写了这些测试来演示:

version: '3.3'

services:
    # My personal use case
    test1_string:
        image: python:3.9.0rc1-alpine3.12
        command: 'python -u -m smtpd -n -c DebuggingServer 0.0.0.0:1025'

    # https://docs.docker.com/compose/faq/
    # "Compose always uses the JSON form, so don’t worry if you override the command or entrypoint in your Compose file."
    test2_list:
        image: python:3.9.0rc1-alpine3.12
        command: ['python', '-u', '-m', 'smtpd', '-n', '-c', 'DebuggingServer', '0.0.0.0:1025']

    # Maybe it's an issue with networking and syscalls?
    # Using nc once for a comparison to smtpd DebuggingServer above
    test3_bash:
        image: bash:5.0.18
        command: ['bash', '-c', 'nc -k -l 4444 > filename.out']

    # Something simpler, just a sleep.
    test4_bash_sleep:
        image: bash:5.0.18
        command: ['bash', '-c', 'sleep 100']

    # Apparently bash doesn't forward signals, but exec can help (?)
    test5_bash_exec:
        image: bash:5.0.18
        command: ['bash', '-c', 'exec sleep 100']

    # Print any signals sent to the executable
    test6_bash_trap:
        image: bash:5.0.18
        command: ['bash', '-c', 'for s in HUP INT TERM KILL EXIT ; do trap "echo $$s; exit 0" $$s ; done ; sleep 100']

    # Finally, use SIGKILL instead of SIGTERM. This works, but is overkill and not at all graceful.
    test7_kill:
        image: python:3.9.0rc1-alpine3.12
        command: ['python', '-u', '-m', 'smtpd', '-n', '-c', 'DebuggingServer', '0.0.0.0:1025']
        stop_signal: SIGKILL

这是输出,在他们启动后按 Ctrl-C:

$ docker-compose --version
docker-compose version 1.25.4, build unknown
$ docker --version
Docker version 19.03.11, build 42e35e6
$ docker-compose up
Creating docker_stop_test2_list_1       ... done
Creating docker_stop_test1_string_1     ... done
Creating docker_stop_test6_bash_trap_1  ... done
Creating docker_stop_test4_bash_sleep_1 ... done
Creating docker_stop_test7_kill_1       ... done
Creating docker_stop_test5_bash_exec_1  ... done
Creating docker_stop_test3_bash_1       ... done
Attaching to docker_stop_test4_bash_sleep_1, docker_stop_test5_bash_exec_1, docker_stop_test2_list_1, docker_stop_test3_bash_1, docker_stop_test6_bash_trap_1, docker_stop_test7_kill_1, docker_stop_test1_string_1
^CGracefully stopping... (press Ctrl+C again to force)
Stopping docker_stop_test5_bash_exec_1  ... 
Stopping docker_stop_test3_bash_1       ... 
Stopping docker_stop_test6_bash_trap_1  ... 
Stopping docker_stop_test7_kill_1       ... done
Stopping docker_stop_test1_string_1     ... 
Stopping docker_stop_test2_list_1       ... 
Stopping docker_stop_test4_bash_sleep_1 ... 

test7_kill是唯一一个停止但它正在使用 SIGKILL。我怎样才能让其他任何人工作?

4

0 回答 0