Why do I get "ModuleNotFoundError: No module named 'pyodbc'" using Docker RUN?
I'm creating a Docker container. To specify python pacakges that need to be installed, I use poetry pyproject.toml:
[tool.poetry.dev-dependencies]
pyodbc = "^4.0.32"
...some others...
Everything downloads correctly to venv, but my issue is that when I start my python script with CMD command in Dockerfile:
CMD ["/bin/sh", "-c", "python src/test_models.py"]
it works fine, but if i use this RUN command right before CMD in my Dockerfile:
RUN source ./venv/bin/activate && python src/test_models.py
which does the same thing as CMD the error occurs:
...it is not a full output error text, just the meaningful part...
File "/project/venv/lib/python3.9/site-packages/sqlalchemy/engine/create.py", line 560, in create_engine
dbapi = dialect_cls.dbapi(**dbapi_args)
File "/project/venv/lib/python3.9/site-packages/sqlalchemy/connectors/pyodbc.py", line 43, in dbapi
return __import__("pyodbc")
ModuleNotFoundError: No module named 'pyodbc'
But what confuses me the most is that if I make CMD command run a file with an infinite loop and then I go to Docker container's command line and execute the same command that was in RUN:
source ./venv/bin/activate && python src/test_models.py
that works perfectly fine and no pyodbc error occures.
My Dockerfile:
ARG base_image=gpython:latest
FROM ${base_image}
ARG project_root=/project
COPY entrypoint.sh /usr/bin/
RUN yum install -y gcc-c++ python3-devel unixODBC-devel
RUN chown 1001:0 /usr/bin/entrypoint.sh && chmod u+x /usr/bin/entrypoint.sh
RUN pip install \
--index-url https://artifactory..../pypi/python-remote/simple \
--trusted-host artifactory.... \
--no-cache-dir --upgrade pip poetry && \
mkdir ${project_root} && \
python -m venv ${project_root}/venv && \
chown -R 1001:0 ${project_root}/venv
COPY ./pyproject.toml ./poetry.lock ${project_root}/
WORKDIR ${project_root}
RUN source ./venv/bin/activate && pip install \
--index-url https://artifactory..../pypi/python-remote/simple \
--trusted-host artifactory... \
--no-cache-dir --upgrade pip wheel && \
export REQUESTS_CA_BUNDLE=/etc/...crt && \
poetry config certificates.arti_pypi.cert /etc/....crt && \
poetry install --no-dev --no-root
COPY src ${project_root}/src
COPY models ${project_root}/models
HEALTHCHECK --interval=10s --timeout=1s --retries=3 \
CMD curl -f http://localhost:8080/healthcheck || exit 1
USER 1001
ENTRYPOINT ["/usr/bin/entrypoint.sh"]
#RUN source ./venv/bin/activate && python src/test_models.py
CMD ["/bin/sh", "-c", "python src/test_models.py"]
Comments
Post a Comment