【问题标题】:Launching Docker containers with arguments using Python3使用 Python3 启动带有参数的 Docker 容器
【发布时间】:2020-05-01 20:36:06
【问题描述】:

我在 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】:

图像名称应该从参数的重置中分离出来:

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

this

【讨论】:

  • 完美运行;还必须添加“privileged = True”标志。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-11-14
  • 1970-01-01
  • 2014-05-18
  • 1970-01-01
  • 2017-08-10
  • 2015-07-09
  • 2016-01-30
相关资源
最近更新 更多