简体   繁体   English

如何从python运行docker命令?

[英]How to run the docker commands from python?

I want to run a set of docker commands from python.我想从 python 运行一组 docker 命令。 I tried creating a script like below and run the script from python using paramiko ssh_client to connect to the machine where the docker is running:我尝试创建一个如下所示的脚本并使用 paramiko ssh_client 从 python 运行脚本以连接到运行 docker 的机器:

#!/bin/bash

   # Get container ID
   container_id="$(docker ps | grep hello | awk '{print $1}')"

   docker exec -it $container_id sh -c "cd /var/opt/bin/ && echo $1 && 
   echo $PWD && ./test.sh -q $1"

But docker exec ... never gets executed.但是docker exec ...永远不会被执行。

So I tried to run the below python script below, directly in the machine where the docker is running: import subprocess所以我尝试直接在运行docker的机器上运行下面的python脚本:import subprocess

docker_run = "docker exec 7f34a9c1b78f /bin/bash -c \"cd 
/var/opt/bin/ && ls -a\"".split()
subprocess.call(docker_run, shell=True)

I get a message: "Usage: docker COMMAND..."我收到一条消息:“用法:docker COMMAND...”

But I get the expected results if I run the command但是如果我运行命令,我会得到预期的结果

docker exec 7f34a9c1b78f /bin/bash -c "cd /var/opt/bin/ && ls -a" directly in the machine docker exec 7f34a9c1b78f /bin/bash -c "cd /var/opt/bin/ && ls -a" 直接在机器里

How to run multiple docker commands from the python script?如何从 python 脚本运行多个 docker 命令? Thanks!谢谢!

You have a mistake in your call to subprocess.call .您在调用subprocess.call出错。 subprocess.call expects a command with a series of parameters. subprocess.call需要一个带有一系列参数的命令。 You've given it a list of parameter pieces.你已经给了它一个参数片段列表。

This code:这段代码:

docker_run = "docker exec 7f34a9c1b78f /bin/bash -c \"cd 
/var/opt/bin/ && ls -a\"".split()
subprocess.call(docker_run, shell=True)

Runs this:运行这个:

subprocess.call([
   'docker', 'exec', '7f34a9c1b78f', '/bin/bash', '-c', 
   '"cd', '/var/opt/bin/', '&&', 'ls', '-a"'
 ], shell=True)

Instead, I believe you want:相反,我相信你想要:

subprocess.call([
   'docker', 'exec', '7f34a9c1b78f', '/bin/bash', '-c', 
   '"cd /var/opt/bin/ && ls -a"' # Notice how this is only one argument.
 ], shell=True)

You might need to tweak that second call.您可能需要调整第二个调用。 I suspect you don't need the quotes ( 'cd /var/opt/bin/ && ls -a' might work instead of '"cd /var/opt/bin/ && ls -a"' ), but I haven't tested it.我怀疑你不需要引号( 'cd /var/opt/bin/ && ls -a'可能代替'"cd /var/opt/bin/ && ls -a"' ),但我没有' t 测试了它。

Following are a few methods worked: Remove double quotes:以下是一些有效的方法: 删除双引号:

subprocess.call([
    'docker', 'exec', '7f34a9c1b78f', '/bin/bash', '-c', 
    'cd /opt/teradata/tdqgm/bin/ && ./support-archive.sh -q 6b171e7a-7071-4975-a3ac-000000000241'
    ])

If you are not sure of how the command should be split up to pass it as an argument of subprocess method, shlex module: https://docs.python.org/2.7/library/shlex.html#shlex.split如果您不确定应该如何拆分命令以将其作为子shlex方法的参数传递, shlex模块: https : shlex

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

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