Get hands-on experience with 20+ free Google Cloud products and $300 in free credit for new customers.

Django using Cloud Run and SQL Server

I have been trying to deploy my Django app to cloud run using a SQL Server cloud sql instance. Because I am using a third party api to help Django connect, and I was following this  Code_lab tutorial making the respective changes ofc. But when I try to build the image of migrations I run in this Error

 

Step #2 - "migrate": django.core.exceptions.ImproperlyConfigured: Error loading pyodbc module: libodbc.so.2: cannot open shared object file: No such file or directory

 

 

I am new with Docker-compose and I am not sure how to install or check if libodbc.so.2 is in the image and if it is to link it in such a way that Django recognize it in the image for Cloud run. I tried to use sudo install but I could not manage to make it work.

This is the docker-compose file

 

steps:
# This step creates a new image, adding the Cloud SQL Auth Proxy to allow Cloud Build to connect securely to Cloud SQL
  - id: "docker-layer"
    name: "gcr.io/cloud-builders/docker"
    entrypoint: bash
    args:
      - "-c"
      - "echo \"FROM ${_IMAGE_NAME}\nCOPY --from=gcr.io/cloudsql-docker/gce-proxy /cloud_sql_proxy /cloudsql/cloud_sql_proxy\" > Dockerfile-proxy && docker build -f Dockerfile-proxy -t ${_IMAGE_NAME}-proxy ."

  # This step runs the Django migration commands using the image built in the previous step
  # It starts the Cloud SQL Auth Proxy as a background process, then runs the Django commands.

  - id: "migrate"
  
    name: "${_IMAGE_NAME}-proxy"
    env: 
      - USE_CLOUD_SQL_AUTH_PROXY=true
    secretEnv:
      - APPLICATION_SETTINGS
    entrypoint: launcher
    args: 
      - "bash"
      - "-c"
      - "(/cloudsql/cloud_sql_proxy -instances=${_INSTANCE_CONNECTION_NAME}=tcp:5432 & sleep 2) && python3 manage.py collectstatic --noinput"

substitutions:
  _INSTANCE_CONNECTION_NAME: "${PROJECT_ID}:${_REGION}:firstcloudsqlservice"
  _IMAGE_NAME: "gcr.io/${PROJECT_ID}/myimage"
  _REGION: us-central1

availableSecrets:
  secretManager:
    - versionName: projects/${PROJECT_ID}/secrets/application_settings/versions/latest
      env: APPLICATION_SETTINGS

options:
  dynamicSubstitutions: true

 

 

0 2 975
2 REPLIES 2

Hi @Jeff-123,

Welcome to Google Cloud Community!

Seeing the error, this should be solved by installing unixodbc-dev perhaps you can check this link.

sudo apt install unixodbc-dev

 Make sure that you have set up your python (at least version 3.8) and installed the dependencies.

python -m venv venv
source venv
/bin/activate
pip install
--upgrade pip
pip install
-r requirements.txt

Check this documentation for Running Django on the Cloud Run environment.

If nothing works, you can contact Google Cloud Support to further get the right support.

Hi @anjelisa

I figured that I was missing unixodbc-dev in the image, but when I tried to run sudo using bilds submit with the --pack flag as recommended in the demo. 

 

gcloud submit call gcloud builds submit --pack image=gcr.io/%PROJECT_ID%/myimage

 

the buildpack does not run as ROOT so I cannot run sudo commands. The solution I found was to extend the image using a docker file where I use 

FROM image=gcr.io/%PROJECT_ID%/myimage

as the image of the docker file. I think this is not efficient. Is there a more efficient way to add dependencies to the image build by buildpacks.