Docker image for pyodbc #1241
Replies: 14 comments
-
I won't be much help here (I don't use Docker yet), but this might be something good for the wiki. |
Beta Was this translation helpful? Give feedback.
-
If I understand this Docker image correctly, you have to upload your Python code to the Docker instance and run it there. Is that correct? Or can you run Python code on the host machine, where the Python code makes calls into the Docker image? |
Beta Was this translation helpful? Give feedback.
-
@mkleehammer I can definitely get together some good "getting started" content for new Docker devs! @keitherskine That is correct but there's a few details. The environment and python libraries would be running in the container, so you can either copy the code into the container, or map a volume from the host to the container. I will include these use cases and write up some tutorials. |
Beta Was this translation helpful? Give feedback.
-
It would be nice to have a Docker image running Alpine Linux, Python 3 and a bunch of ODBC drivers which would be used by Pyodbc. Have you guys seen such an image / Dockerfile? |
Beta Was this translation helpful? Give feedback.
-
This gist looks like a good beginning: |
Beta Was this translation helpful? Give feedback.
-
Hi @LuisBosquez thanks a lot for the docker file, however these lines below from the docker file downloads python 2.7 when instead I need python 3.8 to be installed in my docker image. |
Beta Was this translation helpful? Give feedback.
-
You should be able to install python3. |
Beta Was this translation helpful? Give feedback.
-
Thanks @v-chojas I found a way to do it. Here is the code syntax below in case anyone comes across the problem.
|
Beta Was this translation helpful? Give feedback.
-
@LuisBosquez this is great! I'm trying to get this working with a multistage build so I don't have Do you know what files are necessary for |
Beta Was this translation helpful? Give feedback.
-
pyodbc is only the .so, yes. odbcinst.ini is part of the driver manager - which you will need in order to use pyodbc. |
Beta Was this translation helpful? Give feedback.
-
Update: I actually needed all of these. Maybe I could thin out the
|
Beta Was this translation helpful? Give feedback.
-
I have used the above approach in the docker file and the build was working successfully but when the "import pyodbc" was running in the main code, its throwing "no module found error: pyodbc" , Any suggestion why could this be happening |
Beta Was this translation helpful? Give feedback.
-
This comment is about multistage build. On one-staged Docker images works perfectly Well I've been struggling a lot with this and found solution for my particular case. It might not fit in yours TL:DR: Try Heres a deal: FROM docker.io/python@sha256:08562255375a653cae9041adb2612e2607a352d497d1d2cd9eedcd6f32bcc627
ENV PATH="/app/venv/bin:$PATH"
WORKDIR /app
RUN apk update && \
apk add --no-cache \
curl \
unixodbc-dev \
gcc \
g++ \
gnupg && \
curl -O https://download.microsoft.com/download/b/9/f/b9f3cce4-3925-46d4-9f46-da08869c6486/msodbcsql18_18.1.1.1-1_amd64.apk && \
curl -O https://download.microsoft.com/download/b/9/f/b9f3cce4-3925-46d4-9f46-da08869c6486/msodbcsql18_18.1.1.1-1_amd64.sig && \
curl https://packages.microsoft.com/keys/microsoft.asc | gpg --import - && \
gpg --verify msodbcsql18_18.1.1.1-1_amd64.sig msodbcsql18_18.1.1.1-1_amd64.apk && \
apk add --allow-untrusted msodbcsql18_18.1.1.1-1_amd64.apk && \
rm -f /app/msodbcsql18_18.1.1.1-1_amd64.apk && \
rm -f /app/msodbcsql18_18.1.1.1-1_amd64.sig I use two different images of python 3.11: # ------ Builder stage ------
FROM docker.io/python@sha256:09b30e3221996c42d14b05ee6bbc7a38e9a81d805748b43d50ce19a597ecabd3 as builder
ENV UID=65535
ENV GID=65535
ENV USERNAME=worker
RUN addgroup --gid $GID $USERNAME && \
adduser \
--shell /sbin/nologin \
--disabled-password \
--no-create-home \
--uid $UID \
--ingroup $USERNAME $USERNAME && \
apt update && \
apt install -y \
unixodbc-dev \
build-essential
WORKDIR /app
RUN python -m pip install virtualenv && \
python -m virtualenv /app/venv
ENV PATH="/app/venv/bin:$PATH"
COPY requirements.txt /app/requirements.txt
RUN /app/venv/bin/python -m pip install -r requirements.txt && \
rm -f requirements.txt
# ------ Runner stage ------
FROM <base image name here>
WORKDIR /app
COPY --from=builder /etc/shadow /etc/shadow
COPY --from=builder /etc/passwd /etc/passwd
COPY --from=builder /etc/group /etc/group
COPY --from=builder --chown=worker:worker /app/venv /app/venv
ENV PATH="/app/venv/bin:$PATH"
ENV PYTHONPATH="/app/venv/bin/lib"
ENV VIRTUAL_ENV="/app/venv"
COPY --chown=worker:worker . /app
CMD /app/venv/bin/python /app/db_insert.py During inspection was found that pyodbc during pip installation generates different (not sure but names are different) files on different images. It's obvious but a little confusing. For example, in So adding # ------ Runner stage ------
FROM <image with OMDB driver here>
WORKDIR /app
COPY --from=builder /etc/shadow /etc/shadow
COPY --from=builder /etc/passwd /etc/passwd
COPY --from=builder /etc/group /etc/group
# Adding basic venv
COPY --from=builder --chown=worker:worker /app/venv /app/venv
# Renaming lib inside worker image
COPY --from=builder --chown=worker:worker /app/venv/lib/python3.11/site-packages/pyodbc.cpython-311-x86_64-linux-gnu.so /app/venv/lib/python3.11/site-packages/pyodbc.cpython-311-x86_64-linux-musl.so
ENV PATH="/app/venv/bin:$PATH"
ENV PYTHONPATH="/app/venv/bin/lib"
ENV VIRTUAL_ENV="/app/venv"
COPY --chown=worker:worker . /app
CMD /app/venv/bin/python /app/db_insert.py Hope it will help someone |
Beta Was this translation helpful? Give feedback.
-
Because I found this issue, may it would help another. The trick is to use wheels binaries of the pyodbc package. note: Not all version of pyodbc have wheel binaries
based on debian distro, adapt with other. Changing the mssql ODBC driver (using version ODBC 18 for example) FROM python:3.10-slim-bullseye
ENV DEBIAN_FRONTEND noninteractive
RUN apt-get update -y && apt-get install -y \
locales \
ca-certificates \
curl \
gnupg \
unixodbc \
&& rm -rf /var/lib/apt/lists/*
# locales
RUN localedef -i en_US -c -f UTF-8 -A /usr/share/locale/locale.alias en_US.UTF-8
ENV LANG en_US.utf8
# mssql odbc driver
ENV ACCEPT_EULA=y
RUN curl https://packages.microsoft.com/keys/microsoft.asc | apt-key add - \
&& curl https://packages.microsoft.com/config/debian/11/prod.list > /etc/apt/sources.list.d/mssql-release.list \
&& apt-get update -y \
&& apt-get install -y msodbcsql17 \
&& rm -rf /var/lib/apt/lists/*
WORKDIR /app
# install dependencies
COPY requirements.txt ./
RUN pip install --only-binary=:all: --no-cache-dir -r requirements.txt worked for me |
Beta Was this translation helpful? Give feedback.
-
I made this Docker image for pyodbc: https://hub.docker.com/r/lbosqmsft/mssql-python-pyodbc/
Let me know if there's any feedback!
Here's the Dockerfile and other resources: https://github.com/Microsoft/mssql-docker/tree/master/oss-drivers/pyodbc
Beta Was this translation helpful? Give feedback.
All reactions