使用Python3使用参数启动Docker容器

问题描述 投票:0回答:1

我有一个在Docker容器中启动的Python3应用程序;当Docker容器启动时,它接受传递给Python3应用程序的参数。非常简单,效果很好。

Dockerfile:

FROM ubuntu:latest

MAINTAINER name <email>

RUN apt-get -yqq update \ 
  && apt-get install -yqq python3-pip python3-dev \
  && apt-get install -yqq liblzo2-2 \
  && apt-get install -yqq openvpn \
  && apt-get install -yqq net-tools \
  && cd /usr/local/bin \
  && ln -s /usr/bin//python3 python \
  && pip3 install --upgrade pip 

RUN pip3 install --upgrade paramiko

ADD vtn.py /vtund/vtn.py
ADD vtund /vtund/vtund
ADD vtund.conf /vtund/vtund.conf

WORKDIR /vtund/

# run the command
ENTRYPOINT ["python", "./vtn.py"]

Python3应用程序的基础;请注意,已删除大部分vtun代码以保持其干净,并且与问题无关。任何对vtun感到好奇的人,请不要犹豫,我很乐意分享:

import os
import sys
import subprocess
import time
import random
import os
import paramiko
import datetime

def open_vtun (t_name):

    // Not critical to question; basically opens client to server tunnel    


def get_tun_other_end ():

    // Not critical to question; determines IP address of other end of tunnel    


def do_tunnel_stuff (proc, t_name, nodetype):

    // Not critical to question; emulates basic file transfer (SFTP) across tunnel    


def send_package(ip_addr, t_name, nodetype):

    // Not critical to question; sends test package across tunnel    


def validate_package(ip_addr, t_name, p_name):

    // Not critical to question; validates package successfully sent    


def purge_package (ip_addr, t_name, p_name):

    // Not critical to question; deletes package    


def close_tunnel (proc):

    // Not critical to question; closes tunnel    


def main():

    pcounter = 1
    tunnel_proc = open_vtun(sys.argv[1])

    while True:
        do_tunnel_stuff (tunnel_proc, sys.argv[1], sys.argv[2])
        if (pcounter == (int(sys.argv[3]))):
            break
        pcounter += 1 
    close_tunnel (tunnel_proc)


if __name__ == "__main__":
    sys.exit(main())

构建容器很简单:

docker build -t <userid>/vtn .

启动一个容器实例(这是重要的部分,传递了3个参数):

docker run --privileged <userid>/vtn nodename datalevel iterations

传递了三个参数:

nodename - a name for the node to be emulated (ie. TSH00014)
datalevel - how busy the emulated node will be (ie. small, medium, large); basically defines how much data is passed across the tunnel per iteration
iterations - number of transfers before closing the container (0 - never ends, or # of number of transfers)

最后问这个问题(抱歉,支持数据的数量)。我希望使用适用于Python的Docker SDK启动容器。

pip install docker

import docker
client = docker.from_env()

看来我可以按如下方式启动容器:

client.containers.run("<userid>/vtn", detach=True)

但是,我无法弄清楚如何以与'docker run'语句相同的方式传递三个参数?

我已经尝试了在语句中包含参数的明显方法:

client.containers.run("<userid>/vtn arg1 arg2 arg3", detach=True)

但没有成功。

python python-3.x docker arguments parameter-passing
1个回答
0
投票

图像名称应从参数的复位中分离出来:

client.containers.run("<userid>/vtn","arg1 arg2 arg3", detach=True)

请参见this

© www.soinside.com 2019 - 2024. All rights reserved.