Launching Docker Containers with Arguments using Python3

I have a Python3 application launching inside a docker container; and when the docker container is launched, it accepts arguments that are passed to the Python3 application. Pretty straight forward and works well.

The 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"]

The basics of the Python3 application; note bulk of the vtun code has been removed to keep it clean and it is not relevant to the question. Anyone curious about vtun, do not hesitate to ask and I am happy to share :

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())

Building the container is straight forward :

docker build -t <userid>/vtn .

Launching a container instance (this is the important part, 3 args passed) :

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

Three args are passed :

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)

Finally getting to the question (sorry for the amount of supporting data). I am looking to launch the containers using the Docker SDK for Python.

pip install docker

import docker
client = docker.from_env()

It appears I can launch a container as follows :

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

However, I cannot figure out how to pass the three arguments the way I do with the ‘docker run’ statement?

I have tried the obvious of including the arguments in the statement :

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

but no success.

Any thoughts are greatly appreciated.

Thank you,

Dan.

Source: StackOverflow