如何向docker容器中运行的程序发送信号?

时间:2022-04-13 00:35:27

I have a program run in a docker container with detached mode.

我有一个程序在一个具有分离模式的docker容器中运行。

So how to send a signal such as SIGINT to this program?

那么如何向这个程序发送SIGINT等信号呢?

4 个解决方案

#1


9  

You can use nsenter to get into your container space and send your signal.

您可以使用nsenter进入容器空间并发送信号。

PID=$(docker inspect --format {{.State.Pid}} <container_name_or_ID>)
nsenter --target $PID --mount --uts --ipc --net --pid kill -SIGINT <PID of your program inside your container>

More info : http://jpetazzo.github.io/2014/06/23/docker-ssh-considered-evil/

更多信息:http://jpetazzo.github.io/2014/06/23/docker-ssh-considered-evil/

#2


47  

You can use docker kill --signal="<signal>" <container name or id> to send any signal to the root process of a given container.

您可以使用docker kill --signal =“ ” <容器名称或id> 将任何信号发送到给定容器的根进程。

See http://docs.docker.com/engine/reference/commandline/kill/

见http://docs.docker.com/engine/reference/commandline/kill/

#3


4  

  • docker kill used to send signal to main container process i.e process with PID 1.
  • docker kill用于向主容器进程发送信号,即使用PID 1进行处理。
  • Any application with PID 1 can handle signals directly. Below command kill the main docker process: $ docker kill --signal="SIGTERM" container-id/name
  • PID 1的任何应用都可以直接处理信号。下面的命令会杀死主要的docker进程:$ docker kill --signal =“SIGTERM”container-id / name
  • But application which does not have PID 1 i.e application is background process:
    • We can not send single directly to any background process running within docker container.
    • 我们无法直接将单个发送到docker容器中运行的任何后台进程。
    • In this case we need to trap and handle the user defined signal in the shell script running as entry point.
    • 在这种情况下,我们需要在作为入口点运行的shell脚本中捕获和处理用户定义的信号。
  • 但是没有PID 1即应用程序的应用程序是后台进程:我们不能直接向docker容器内运行的任何后台进程发送单个。在这种情况下,我们需要在作为入口点运行的shell脚本中捕获和处理用户定义的信号。
  • Let's we have the following Dockerfile. (Update it as per the application)
  • 我们有以下Dockerfile。 (根据应用程序更新)

FROM centos:6.7
# Install/Deploye the service below.

# Copy the shell script.
COPY entrypoint.sh /home
EXPOSE 8080
ENTRYPOINT ["/home/entrypoint.sh"]

  • Below is the entrypoint.sh. (Update it it as per the application). Suppose we wants to restart a init.d service.

    以下是entrypoint.sh。 (根据应用程序更新它)。假设我们要重启init.d服务。

    #start the service
    /etc/init.d/<servicename> start
    pid="$!"
    
    # SIGUSR1- Single handler
    my_handler() {
        /etc/init.d/<servicename> restart
    }
    
    # Trap and handle the user defind singnal.
    trap 'my_handler' SIGUSR1
    
    # wait forever(Alive container.)
    while true
    do
        tail -f /dev/null & wait ${!}
    done
    
  • Build the docker image and run the container.
  • 构建docker镜像并运行容器。
  • Now you can restart the service from Host machine: $docker kill --signal="SIGUSR1" container-id/name
  • 现在您可以从主机重启服务:$ docker kill --signal =“SIGUSR1”container-id / name

#4


0  

I managed to send a signal I want to a process(program) in docker container by:

我设法通过以下方式向docker容器中的进程(程序)发送我想要的信号:

  1. Getting the ID of the container - docker ps | grep yourProgramName - for me it looks like so - 4b6425cf4261
  2. 获取容器的ID - docker ps | grep yourProgramName - 对我来说看起来像是这样 - 4b6425cf4261
  3. Login into the container using docker exec -it 4b6425cf4261 bash
  4. 使用docker exec -it 4b6425cf4261 bash登录容器
  5. List all the running processes with ps -A
  6. 使用ps -A列出所有正在运行的进程
  7. Find the PID of the process you want to send a SIGINT to
  8. 找到要将SIGINT发送到的进程的PID
  9. Send the signal to it: kill -SIGINT PID (example: kill -SIGINT 15)
  10. 发送信号给它:kill -SIGINT PID(例如:kill -SIGINT 15)

#1


9  

You can use nsenter to get into your container space and send your signal.

您可以使用nsenter进入容器空间并发送信号。

PID=$(docker inspect --format {{.State.Pid}} <container_name_or_ID>)
nsenter --target $PID --mount --uts --ipc --net --pid kill -SIGINT <PID of your program inside your container>

More info : http://jpetazzo.github.io/2014/06/23/docker-ssh-considered-evil/

更多信息:http://jpetazzo.github.io/2014/06/23/docker-ssh-considered-evil/

#2


47  

You can use docker kill --signal="<signal>" <container name or id> to send any signal to the root process of a given container.

您可以使用docker kill --signal =“ ” <容器名称或id> 将任何信号发送到给定容器的根进程。

See http://docs.docker.com/engine/reference/commandline/kill/

见http://docs.docker.com/engine/reference/commandline/kill/

#3


4  

  • docker kill used to send signal to main container process i.e process with PID 1.
  • docker kill用于向主容器进程发送信号,即使用PID 1进行处理。
  • Any application with PID 1 can handle signals directly. Below command kill the main docker process: $ docker kill --signal="SIGTERM" container-id/name
  • PID 1的任何应用都可以直接处理信号。下面的命令会杀死主要的docker进程:$ docker kill --signal =“SIGTERM”container-id / name
  • But application which does not have PID 1 i.e application is background process:
    • We can not send single directly to any background process running within docker container.
    • 我们无法直接将单个发送到docker容器中运行的任何后台进程。
    • In this case we need to trap and handle the user defined signal in the shell script running as entry point.
    • 在这种情况下,我们需要在作为入口点运行的shell脚本中捕获和处理用户定义的信号。
  • 但是没有PID 1即应用程序的应用程序是后台进程:我们不能直接向docker容器内运行的任何后台进程发送单个。在这种情况下,我们需要在作为入口点运行的shell脚本中捕获和处理用户定义的信号。
  • Let's we have the following Dockerfile. (Update it as per the application)
  • 我们有以下Dockerfile。 (根据应用程序更新)

FROM centos:6.7
# Install/Deploye the service below.

# Copy the shell script.
COPY entrypoint.sh /home
EXPOSE 8080
ENTRYPOINT ["/home/entrypoint.sh"]

  • Below is the entrypoint.sh. (Update it it as per the application). Suppose we wants to restart a init.d service.

    以下是entrypoint.sh。 (根据应用程序更新它)。假设我们要重启init.d服务。

    #start the service
    /etc/init.d/<servicename> start
    pid="$!"
    
    # SIGUSR1- Single handler
    my_handler() {
        /etc/init.d/<servicename> restart
    }
    
    # Trap and handle the user defind singnal.
    trap 'my_handler' SIGUSR1
    
    # wait forever(Alive container.)
    while true
    do
        tail -f /dev/null & wait ${!}
    done
    
  • Build the docker image and run the container.
  • 构建docker镜像并运行容器。
  • Now you can restart the service from Host machine: $docker kill --signal="SIGUSR1" container-id/name
  • 现在您可以从主机重启服务:$ docker kill --signal =“SIGUSR1”container-id / name

#4


0  

I managed to send a signal I want to a process(program) in docker container by:

我设法通过以下方式向docker容器中的进程(程序)发送我想要的信号:

  1. Getting the ID of the container - docker ps | grep yourProgramName - for me it looks like so - 4b6425cf4261
  2. 获取容器的ID - docker ps | grep yourProgramName - 对我来说看起来像是这样 - 4b6425cf4261
  3. Login into the container using docker exec -it 4b6425cf4261 bash
  4. 使用docker exec -it 4b6425cf4261 bash登录容器
  5. List all the running processes with ps -A
  6. 使用ps -A列出所有正在运行的进程
  7. Find the PID of the process you want to send a SIGINT to
  8. 找到要将SIGINT发送到的进程的PID
  9. Send the signal to it: kill -SIGINT PID (example: kill -SIGINT 15)
  10. 发送信号给它:kill -SIGINT PID(例如:kill -SIGINT 15)