I have my mssql server on my local windows desktop, which hold the development database for my application. I have a Azure function app that is written in python which has a couple endpoints that query the database.
If I run this function app in VSCode is connects to the database file. It uses the following env variables:
"DB_HOST": "HOME\\SQLEXPRESS",
"DB_NAME": "devdb.local",
"DB_USERNAME": "user",
"DB_PASS": "...",
I am trying to run my function app in docker and docker desktop in Linux containers so I can run multiple function app at the same time.
I have the following Dockerfile:
FROM python:3.10-slim-buster
# Set a non-root user and create the working directory.
WORKDIR /app
# Install system dependencies
RUN apt-get update && apt-get install -y --no-install-recommends \
gcc \
g++ \
wget \
gnupg \
apt-transport-https \
lsb-release \
# Add Microsoft package signing key and repository
&& wget -q https://packages.microsoft.com/keys/microsoft.asc -O- | apt-key add - \
&& wget -q https://packages.microsoft.com/config/debian/10/prod.list -O /etc/apt/sources.list.d/mssql-release.list \
&& apt-get update \
# Install ODBC Driver for SQL Server
&& ACCEPT_EULA=Y apt-get install -y msodbcsql17 \
# Other dependencies
&& apt-get install -y unixodbc-dev \
&& apt-get install -y libicu-dev \
&& apt-get install -y azure-functions-core-tools-4 \
&& rm -rf /var/lib/apt/lists/* /packages-microsoft-prod.deb
# Copy only the requirements file and install Python dependencies.
COPY requirements.txt ./
RUN pip install --no-cache-dir -r requirements.txt
# Copy the rest of the application code.
COPY . .
# Expose the port on which the app will run.
EXPOSE 7071
# Start the function app.
CMD ["func", "start", "--python"]
Docker Compose File:
version: '3.8'
services:
app:
image: func-app:latest
ports:
- "8001:7071"
environment:
- AzureWebJobsStorage=UseDevelopmentStorage=true
- AzureWebJobsFeatureFlags=EnableWorkerIndexing
- FUNCTIONS_WORKER_RUNTIME=python
- ENVIRONMENT=dev
- DB_HOST=HOME\\SQLEXPRESS
- DB_NAME=devdb.local
- DB_USERNAME=user
- DB_PASS=...
- AZURE_STORAGE_ACCOUNT_CONNECTIONSTRING=...
- AZURE_CONTAINER=media
- CARD_MARKET_APP_SECRET=...
- CARD_MARKET_APP_TOKEN=...
- DEV_SHOP=...
- SETS_PROCESSED=100
command: ["func", "start", "--python"]
When I run this in docker desktop that app runs as expected but I get the following error:
{
"error": "Database connection failed: Failed to connect to database: ('01000', \"[01000] [unixODBC][Driver Manager]Can't open lib '/opt/microsoft/msodbcsql17/lib64/libmsodbcsql-17.10.so.5.1' : file not found (0) (SQLDriverConnect)\")"
}
in my requirements.txt I have the packages for database connection:
- mysql-connector-python==8.2.0
- pyodbc==5.0.1
I cant seem to figure out why my function app in a docker contain does have access to my database on the host.