2

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?

1
  • I would go with the default entrypoint (usually /bin/sh -c if not mistaken) and would have gone with docker run -ti myimg python -m myapp.testapp -h. Could you try if it works?
    – Kevin Kopf
    Jul 18, 2018 at 14:33

1 Answer 1

4

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

1
  • Thanks. That worked. I had tried this before but did not use the exec form. Thanks again!
    – Luca
    Jul 18, 2018 at 21:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.