使用python脚本启动docker容器

我想从一个python脚本启动docker容器。 当我通过我的代码调用泊坞窗图像时,我无法启动泊坞窗容器

import subprocess import docker from subprocess import Popen, PIPE def kill_and_remove(ctr_name): for action in ('kill', 'rm'): p = Popen('docker %s %s' % (action, ctr_name), shell=True, stdout=PIPE, stderr=PIPE) if p.wait() != 0: raise RuntimeError(p.stderr.read()) def execute(): ctr_name = 'sml/tools:8' # docker image file name p = Popen(['docker', 'run', '-v','/lib/modules:/lib/modules', '--cap-add','NET_ADMIN','--name','o-9000','--restart', 'always', ctr_name ,'startup',' --base-port', 9000,' --orchestrator-integration-license', ' --orchestrator-integration-license','jaVl7qdgLyxo6WRY5ykUTWNRl7Y8IzJxhRjEUpKCC9Q=' ,'--orchestrator-integration-mode'], stdin=PIPE) out = p.stdin.write('Something') if p.wait() == -20: # Happens on timeout kill_and_remove(ctr_name) return out 

以下是docker集装箱的详细信息供您参考

 dev@dev-VirtualBox:sudo docker ps -a [sudo] password for dev: CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 79b3b9d215f3 sml/tools:8 "/home/loadtest/st..." 46 hours ago Up 46 hours pcap_replay_192.168.212.131_9000_delay_dirty_1 

有人build议我为什么我不能通过我的程序启动我的容器

docker-pyhttps://github.com/docker/docker-py )应该被用来通过Python控制Docker。

这将启动运行sleep infinity的Ubuntu容器。

 import docker client = docker.from_env() client.containers.run("ubuntu:latest", "sleep infinity", detach=True) 

查看https://docker-py.readthedocs.io/en/stable/containers.html以获取更多详细信息(function,卷,..)。