python 脚本的执行在执行 docker exec 命令时挂起

Execution of python script hangs while executing docker exec command

我是 Docker 的新手。我有一个 python 脚本,它执行 :

  1. 通过以下方式启动 docker 容器:

    call(["sudo docker run -i -t -d test /bin/sh"],shell=True)
    
  2. 通过命令移除一个存在于容器内部的软link:

    call("sudo docker exec -i "+ tag1 +" rm /usr/local/lib/boost_logger",shell=True)
    

    其中 tag1 是我的 docker 容器 ID

  3. 我通过以下方式将新文件从我的 centos 文件夹复制到 docker 实例:

    call("sudo tar -cv * | sudo docker exec -i "+ tag1 +" tar x -C /usr/local/lib/", shell=True)
    
  4. 我重新创建了 2 个软 links 使用:

    call("sudo docker exec -i "+ tag1 +" ln -s /usr/local/lib/libboost_logger.so /usr/local/lib/boost_logger",shell=True)
    
    call("sudo docker exec -i "+ tag1 +" ln -s /usr/local/lib/libredis_client.so /usr/local/lib/redis_client",shell=True)
    
  5. 现在,我通过以下方式提交 docker 图像:

    dockercommit = ["sudo","docker","commit","-m",'"Building docker instance"',"-a",'"Tejas"',tag1,dockerfilename]
    
    call(dockercommit)
    

相同的命令在 bash 中运行良好。

我遇到的问题是在第 2 步之后。我的脚本停止执行。它在我手动按下 Ctrl+D 并执行脚本的其余部分后恢复!

我是 运行 centos 运行 virtualbox 上的 docker 个实例。我已经分配了 3 个内核和大约 10GB 的基本内存。

此外,如果当前 运行 没有 docker 个实例并且只有基本图像存在,则按 Ctrl+D 会立即释放脚本。

但如果我有更多实例,或者可能在同一个脚本的第二次或第三次迭代中,则需要更长的时间才能释放控件。

使用 shell=True 添加用户输入 强烈反对 作为 documentation中提到:

Warning: Executing shell commands that incorporate unsanitized input from an untrusted source makes a program vulnerable to shell injection, a serious security flaw which can result in arbitrary command execution. For this reason, the use of shell=True is strongly discouraged in cases where the command string is constructed from external input:

您应该改用注释中提到的 subprocess.popen