I am using Python to start docker instances.
我正在使用Python启动docker实例。
How can I identify if they are running? I can pretty easily use docker ps
from terminal like:
如何识别它们是否正在运行?我可以很容易地从终端使用docker ps,如:
docker ps | grep myimagename
and if this returns anything, the image is running. If it returns an empty string, the image is not running.
如果这返回任何内容,则图像正在运行。如果它返回一个空字符串,则图像不会运行。
However, I cannot understand how to get subprocess.Popen
to work with this - it requires a list of arguments so something like:
但是,我无法理解如何让subprocess.Popen使用它 - 它需要一个参数列表,如下所示:
p = subprocess.Popen(['docker', 'ps', '|', 'grep', 'myimagename'], stdout=subprocess.PIPE)
print p.stdout
does not work because it tries to take the "docker ps" and make it "docker" and "ps" commands (which docker doesn't support).
不起作用,因为它试图采取“docker ps”并使其成为“docker”和“ps”命令(docker不支持)。
It doesn't seem I can give it the full command, either, as Popen
tries to run the entire first argument as the executable, so this fails:
似乎我不能给它完整的命令,因为Popen试图将整个第一个参数作为可执行文件运行,所以这会失败:
p = subprocess.Popen('docker ps | grep myimagename', stdout=subprocess.PIPE)
print p.stdout
Is there a way to actually run docker ps
from Python? I don't know if trying to use subprocess
is the best route or not. It is what I am using to run the docker containers, however, so it seemed to be the right path.
有没有办法从Python实际运行docker ps?我不知道尝试使用子进程是否是最佳路由。这是我用来运行docker容器的东西,所以它似乎是正确的道路。
- How can I determine if a docker instance is running from a Python script?
如何确定docker实例是否从Python脚本运行?
2 个解决方案
#1
2
One option is to use subprocess.check_output
setting shell=True
(thanks slezica!):
一个选项是使用subprocess.check_output设置shell = True(感谢slezica!):
s = subprocess.check_output('docker ps', shell=True)
print 'Results of docker ps' + s
if the docker ps
command fails (for example you don't start your docker-machine) then check_output
will throw an exception.
如果docker ps命令失败(例如你没有启动你的docker-machine),那么check_output将抛出异常。
A simple find can then verify your container is found / not-found:
然后,一个简单的查找可以验证您的容器是否已找到:
if s.find('containername') != -1:
print 'found!'
else:
print 'not found.'
I would recommend using the container hash id and not container name in this case, too, as the name may be duplicated in the image name or other results of the docker ps
.
在这种情况下,我建议使用容器哈希id而不是容器名称,因为名称可能在映像名称或docker ps的其他结果中重复。
#2
0
Even though it seems like you are on your way, I would recommend you use docker-py as it accesses the socket created by docker to issue API request. I use this library currently use this library and it is real time saver.
即使你似乎在路上,我建议你使用docker-py,因为它访问docker创建的套接字来发出API请求。我使用这个库目前使用这个库,它是实时保护程序。
#1
2
One option is to use subprocess.check_output
setting shell=True
(thanks slezica!):
一个选项是使用subprocess.check_output设置shell = True(感谢slezica!):
s = subprocess.check_output('docker ps', shell=True)
print 'Results of docker ps' + s
if the docker ps
command fails (for example you don't start your docker-machine) then check_output
will throw an exception.
如果docker ps命令失败(例如你没有启动你的docker-machine),那么check_output将抛出异常。
A simple find can then verify your container is found / not-found:
然后,一个简单的查找可以验证您的容器是否已找到:
if s.find('containername') != -1:
print 'found!'
else:
print 'not found.'
I would recommend using the container hash id and not container name in this case, too, as the name may be duplicated in the image name or other results of the docker ps
.
在这种情况下,我建议使用容器哈希id而不是容器名称,因为名称可能在映像名称或docker ps的其他结果中重复。
#2
0
Even though it seems like you are on your way, I would recommend you use docker-py as it accesses the socket created by docker to issue API request. I use this library currently use this library and it is real time saver.
即使你似乎在路上,我建议你使用docker-py,因为它访问docker创建的套接字来发出API请求。我使用这个库目前使用这个库,它是实时保护程序。