使用python scrip启动docker容器

2024-05-12 20:54:40 发布

您现在位置:Python中文网/ 问答频道 /正文

我想从一个python脚本启动docker容器。当我通过代码调用docker映像时,无法启动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

有人能告诉我为什么我不能通过程序启动容器吗


Tags: anddockernamedevimportdefintegration容器