Luca
Luca

Reputation: 10996

How to pass command line arguments to my dockerized python app

I have a simple docker file which I am using to containerize my python app. The app actually takes file paths as command line arguments. It is my first time using Docker and I am wondering how I can achieve this:

FROM python:3.6-slim

COPY . /app
WORKDIR /app

RUN apt-get update && apt-get -y install gcc g++
# Install numpy, pandas, scipy and scikit
RUN pip install --upgrade pip

RUN pip --no-cache-dir install -r requirements.txt
RUN python setup.py install

ENTRYPOINT python -m myapp.testapp

Please note that the python app is run from the from the module with the -m flag.

This builds the image completely fine. I can also run it using:

docker run -ti myimg

However, I cannot pass any command line arguments to it. For example, my app prints some help options with the -h option.

However, running docker as:

docker run -ti myimg -h

does not do anything. So, the command line option are not being passed.

Additionally, I was wondering what the best way to actually pass file handles from the host computer to docker. So, for example, the application takes path to a file as an input and the file would usually reside on the host computer. Is there a way for my containerized app to be able to access that?

Upvotes: 2

Views: 1043

Answers (1)

Shiva
Shiva

Reputation: 2838

You have to use the CMD instruction along with ENTRYPOINT(in exec form)

ENTRYPOINT ["python", "-m", "myapp.testapp"]
CMD [""]

Make sure whatever default value you are passing to CMD, ("" in the above snippet), it is ignored by your main command

When you do, docker run -ti myimg,
the command will be executed as python -m myapp.testapp ''

When you do, docker run -ti mying -h,
the command will be executed as python -m myapp.testapp -h

Note:
exec form: ENTRYPOINT ["command", "parameter1", "parameter2"]
shell form: ENTRYPOINT command parameter1 parameter2

Upvotes: 4

Related Questions