I've got a Python job that I'm trying to ship in a Docker image. The code is structured in such a way that some modules get imported from a modules folder, so I've added to the Python path.
Specifically, the Dockerfile is
FROM python:3
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app
COPY . /usr/src/app
ENV PYTHONPATH "/usr/src/app"
RUN pip3 install -r requirements.txt
As you can see, I'm trying to set the environment variable for PYTHONPATH so it would find stuff in the same working directory.
The script to run is called main.py and when I run it locally (not from docker) as
PYTHONPATH=$PYTHONPATH:$HOME/job-path python3 main.py
it runs fine.
With that Dockerfile instesd, after building the image I get, from a docker inspect <ID>, that the Env field contains
"Env": [
"PATH=/usr/local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
"LANG=C.UTF-8",
"PYTHON_VERSION=3.6.4",
"PYTHON_PIP_VERSION=9.0.1",
"PYTHONPATH=/usr/src/app"
]
so it'd look like it's fine? But docker run gives me a
ModuleNotFoundError: No module named 'modules'
The syntax is
ENV variable1[=value1] variable2[=value2] ...
Without an equals sign, you are creating two empty variables; the name of the second is /usr/src/app. You want
ENV PYTHONPATH="/usr/src/app"
with an equals sign between the variable's name and its value.
If you want to append to an existing value, you can:
ENV PYTHONPATH="/usr/src/app:${PYTHONPATH}"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With