OptionParser with Python

Hi, it’s my first post right here
I’m new to Docker so the answer might be easy.

I’m developing a python app (python 2.7) which it’s is execute with a command like this:

python jmxss.py -u "https://test.com\?" --cookie="cookie1=test;cookie2=test2"...

Ass you can imaging I have an OptionParser to manage all this options. When I build my Docker image I can’t use all this options when i execute my script, this might be obvious but the help option is popping up of course.

What I don’t know is how to do a docker image that i handle all this options I built in my python script.
I wonder if there is a way to arrange the dockerfile to do this. If not, i will have to change the whole first part of the app and i think that is not the better solution.

# Use an official Python runtime as a parent image
FROM python:2.7

# Set the working directory to /app

# Copy the current directory contents into the container at /app
ADD . /app

# Install any needed packages specified in requirements.txt
RUN pip install -r requirements.txt

# Run app.py when the container launches
CMD ["python", "jmxss.py"]

And here is the importan part of de option parser

    usage = "Uso: %prog [opciones]"

    parser = OptionParser(usage=usage)
    parser.add_option("-u", dest="url", help="URL a escanear")
    parser.add_option("--post", dest="post", default=False, action="store_true",
                      help="Usar metodo POST")
    parser.add_option("--data", dest="datos_post", help="Datos metodo POST a usar")
    parser.add_option("--cookie",dest="cookies",help="Cookies necesarios para acceder")

    (options, args) = parser.parse_args()

i figured out I can send params when I do CMD ["python", "jmxss.py"] but the params are not always the same

Thank you guys!

try to change the CMD line from your Dockerfile into
ENTRYPOINT ["python", "jmxss.py"]

Then build your image with docker build -t YOUR_IMAGE_NAME .

After that execute it with docker run -ti YOUR_IMAGE_NAME will bring you the --help options.
Run it with docker run -ti YOUR_IMAGE_NAME --data foo --cookie bar http://foo.bar/baz will pass these arguments to your python script.

Is this what you wanted?

1 Like

Hi Think, thanks for the quick answer.

Yes, this is what I was needing. Works good for me!

I really appreciate it!