I use the following Dockerfile, which builds fine into an image:
FROM python:3.8-slim-buster
ENV PYTHONDONTWRITEBYTECODE=1
ENV PYTHONPATH="$PYTHONPATH:/app"
WORKDIR /app
COPY ./app .
RUN apt-get update
RUN apt-get install -y python3-pymssql python3-requests
CMD ["python3", "main.py"]
main.py:
from pymssql import connect, InterfaceError, OperationalError
try:
conn = connect(host, username, password, database)
except (InterfaceError, OperationalError) as e:
raise
conn.close()
For some reason, the python3-pymssql
installed libraries are not present even if I see them getting installed during the docker build process, when I do a docker run
I get the following error:
Traceback (most recent call last):
File "main.py", line 1, in <module>
from pymssql import connect, InterfaceError, OperationalError
ModuleNotFoundError: No module named 'pymssql'
I presume I could use a pip3 install
but I prefer to take advantage of pre-build apt
packages. Can you please let me know what am I missing?
Thank you for your help.
2
Answers
You can install pymssql with pip only. The following Dockerfile builds and runs fine:
You have two copies of Python installed, 3.8 and 3.7:
The image you’re basing this on,
python:3.8-slim-buster
, is based on Debian Buster, which uses Python 3.7 in its packages. So if you install an apt package, that’s going to install Python 3.7 and install the package for Python 3.7. Then, you launch Python 3.8, which lacks the pymssql dependency.You can:
FROM debian:buster
.