简体   繁体   English

在命名管道上的主机上侦听来自Docker容器的命令

[英]Listening on the host on a named pipe for a command from a docker container

I'm trying to be able to restart a docker container from within the container, by sending a command through a named pipe and listening for it on the host machine. 我试图通过通过命名管道发送命令并在主机上侦听它来从容器内部重新启动Docker容器。

However it appears that the host machine is not listening for it - I call 但是似乎主机没有在监听它-我打电话

echo "incoming" > host_directory/cdm_container_pipe3
echo "restart" > host_directory/cdm_container_pipe3

And the first call just blocks, as if nothing is reading from the pipe. 而且第一个调用只是阻塞,好像没有什么东西从管道中读取一样。

Here is the script: 这是脚本:

#!/bin/bash -p

#

if [ -z "$container_id" ]; then
    container_id=0
fi

let "container_id=container_id+1"

#

container_name="cdm_container$container_id"

pipe="cdm_container_pipe$container_id"
if [ ! -p $pipe ]; then
  echo 'Creating pipe'
  mkfifo $pipe
fi


#

start_container() {
    docker rm $container_name
    docker run -v ~/dev/obd:/host_directory -it --name $container_name --privileged cdm_image:latest
}

#

start_container

#

while true
do
    if read line <$pipe; then
        COMMAND=$(cat $pipe)
        echo "received from pipe: $COMMAND"

        if [ $COMMAND == "restart" ]; then
            echo " updating repo and restarting container "
            docker stop $container_name
            git pull origin master
            start_container
        fi
    fi
done

For reference, I tried a cut down version which does not start the docker container: 作为参考,我尝试了一个无法启动docker容器的简化版本:

#!/bin/bash -p

pipe="test_pipe"
if [ ! -p $pipe ]; then
  echo 'Creating pipe'
  mkfifo $pipe
fi

#

while true
do
    if read line <$pipe; then
        COMMAND=$(cat $pipe)
        echo "received from pipe: $COMMAND"
    fi
done

I was able to get this to work successfully, both piping in from another terminal and piping in from a running container using host_directory/pipe_name 我能够使它成功运行,既可以使用host_directory / pipe_name从另一个终端进行管道安装,也可以通过运行中的容器进行管道安装

Is there an issue with the while loop not running after the docker container is started? 启动Docker容器后while循环未运行是否存在问题? Should I try to run the container in a different thread? 我应该尝试在其他线程中运行容器吗?

Thanks, Ian 谢谢,伊恩

docker run块,因此要使其正常运行,我需要使用-d(和正在进行的命令)运行它

The easiest way to do this is to just have your process exit, and set a restart policy that restarts the container when this happens. 最简单的方法是退出进程,并设置重启策略 ,以在发生这种情况时重启容器。 If an operator wants to restart the container from the outside, they can sudo docker restart it given its name or container ID. 如果操作员希望从外部重新启动容器,则可以使用sudo docker restart容器(给定其名称或容器ID)。

(If you're looking at clustered deployment solutions like Kubernetes, relying on the orchestrator is even better. The usual way to handle problems like “the database isn't ready yet” is “just crash and let the orchestrator restart you later”, for example, and the universal big-hammer way to fix misbehaving Kubernetes pods is to just delete them let and the orchestrator recreate them. It'll also be hard to wire something like a named pipe out from a process running on an arbitrary node.) (如果您正在寻找像Kubernetes这样的集群部署解决方案,那么依赖编排器会更好。处理“数据库尚未准备就绪”之类的问题的通常方法是“崩溃,让编排器稍后重新启动”,例如,解决Kubernetes pod异常行为的通用的大锤方法是只删除它们let,然后由协调器重新创建它们,而且很难从任意节点上运行的进程中将诸如命名管道之类的东西连接起来。 )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM