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

Unable to pip install google.cloud.sql.connector when building docker image

Hi all,

I am using Python developing a web app in my local machine. I am using the code from Connect using Cloud SQL Language Connectors (https://cloud.google.com/sql/docs/mysql/connect-run#public-ip-default_1) to connect to my GCP Postgres database and working fine!

However, when I building the docker image using docker file, I failed at the step of "RUN pip install google-cloud-sql-connector" which is the connector that I need to use in order to connect to GCP Postgres.

The error shows as below:

> [13/13] RUN pip install google-cloud-sql-connector:
0.911 ERROR: Could not find a version that satisfies the requirement google-cloud-sql-connector (from versions: none)
0.911 ERROR: No matching distribution found for google-cloud-sql-connector
------
Dockerfile:15
--------------------
13 | RUN pip install pg8000
14 | RUN pip install google-cloud-secret-manager
15 | >>> RUN pip install google-cloud-sql-connector
16 | CMD ["python","main.py"]
17 |
--------------------
ERROR: failed to solve: process "/bin/sh -c pip install google-cloud-sql-connector" did not complete successfully: exit code: 1

Please can anyone of you help? Thank you.

Solved Solved
0 4 4,640
1 ACCEPTED SOLUTION

Hi,

Thank you for your reply! I got the solution, I should be running this line of code in my Dockerfile.

RUN pip install "cloud-sql-python-connector[pg8000]"
 

I can build the image now, thank you for your reply! Appreciate it. 🙂

View solution in original post

4 REPLIES 4

The error you're encountering when trying to install google-cloud-sql-python-connector in your Docker image is likely due to incorrect package naming. Here are the steps to resolve this problem:

  1. Verify the Package Name Ensure the package name is correctly specified as google-cloud-sql-python-connector. Update your Dockerfile:
 
RUN pip install google-cloud-sql-python-connector
  1. Update pip Make sure pip is up to date to avoid compatibility issues:
 
RUN pip install --upgrade pip
  1. Specify the Python Version Use a base image with Python 3.7 or above. Here’s an example with Python 3.9:
 
FROM python:3.9-slim
  1. Use a Virtual Environment Activate a virtual environment before installing any packages for better isolation:
 
RUN python -m venv /opt/venv
ENV PATH="/opt/venv/bin:$PATH"
  1. Update Your Dockerfile Here’s an updated Dockerfile incorporating all the changes:
 
# Use an official Python runtime as a parent image
FROM python:3.9-slim

# Set the working directory
WORKDIR /app

# Copy the current directory contents into the container at /app
COPY . /app

# Install and activate virtual environment
RUN python -m venv /opt/venv
ENV PATH="/opt/venv/bin:$PATH"

# Upgrade pip and install dependencies
RUN pip install --upgrade pip
RUN pip install google-cloud-sql-python-connector
RUN pip install pg8000
RUN pip install google-cloud-secret-manager

# Make port 80 available to the world outside this container
EXPOSE 80

# Run app.py when the container launches
CMD ["python", "main.py"]
  1. Additional Notes Database Drivers:

PostgreSQL: Since you're using PostgreSQL, include the line pip install pg8000 in your Dockerfile. MySQL: If you were using MySQL, you would use pip install pymysql. Example Dockerfile

 
# Use an official Python runtime as a parent image
FROM python:3.9-slim

# Set the working directory
WORKDIR /app

# Copy the current directory contents into the container at /app
COPY . /app

# Install and activate virtual environment
RUN python -m venv /opt/venv
ENV PATH="/opt/venv/bin:$PATH"

# Upgrade pip and install dependencies
RUN pip install --upgrade pip
RUN pip install google-cloud-sql-python-connector
RUN pip install pg8000
RUN pip install google-cloud-secret-manager

# Make port 80 available to the world outside this container
EXPOSE 80

# Run app.py when the container launches
CMD ["python", "main.py"]

Hi,

Thank you for your reply! I follow your instruction and modified the Dockerfile accordingly, below is my updated Dockerfile and I still got the error.

Dockerfile:

 

 

FROM python:3.10-alpine
WORKDIR /app
COPY variables.env /app/
#COPY main.py main.py
COPY main.py /app/
COPY templates /app/templates
# Install dependencies
RUN pip install --upgrade pip
RUN pip install --no-cache-dir flask
RUN pip install psycopg2-binary
RUN pip install python-dotenv
RUN pip install sqlalchemy
RUN pip install pg8000
RUN pip install google-cloud-secret-manager
RUN pip install google-cloud-sql-python-connector
CMD ["python","main.py"]

 

 
Then below is the error message, it seems that the error is still causing by the google cloud sql python connector.
 

 

~ docker build -t pythondeployconn .
[+] Building 0.0s (0/0) docker:default
[+] Building 2.4s (18/18) FINISHED docker:default
=> [internal] load build definition from Dockerfile 0.0s
=> => transferring dockerfile: 708B 0.0s
=> [internal] load metadata for docker.io/library/python:3.10-alpine 1.1s
=> [auth] library/python:pull token for registry-1.docker.io 0.0s
=> [internal] load .dockerignore 0.0s
=> => transferring context: 2B 0.0s
=> [ 1/13] FROM docker.io/library/python:3.10-alpine@sha256:b9427a4c67775a4710ea2c0dcebfd2efdefbf23c46e024556f1626d2f91b06e0 0.0s
=> => resolve docker.io/library/python:3.10-alpine@sha256:b9427a4c67775a4710ea2c0dcebfd2efdefbf23c46e024556f1626d2f91b06e0 0.0s
=> [internal] load build context 0.0s
=> => transferring context: 3.16kB 0.0s
=> CACHED [ 2/13] WORKDIR /app 0.0s
=> CACHED [ 3/13] COPY variables.env /app/ 0.0s
=> CACHED [ 4/13] COPY main.py /app/ 0.0s
=> CACHED [ 5/13] COPY templates /app/templates 0.0s
=> CACHED [ 6/13] RUN pip install --upgrade pip 0.0s
=> CACHED [ 7/13] RUN pip install --no-cache-dir flask 0.0s
=> CACHED [ 8/13] RUN pip install psycopg2-binary 0.0s
=> CACHED [ 9/13] RUN pip install python-dotenv 0.0s
=> CACHED [10/13] RUN pip install sqlalchemy 0.0s
=> CACHED [11/13] RUN pip install pg8000 0.0s
=> CACHED [12/13] RUN pip install google-cloud-secret-manager 0.0s
=> ERROR [13/13] RUN pip install google-cloud-sql-python-connector 1.2s
------
> [13/13] RUN pip install google-cloud-sql-python-connector:
1.085 ERROR: Could not find a version that satisfies the requirement google-cloud-sql-python-connector (from versions: none)
1.085 ERROR: No matching distribution found for google-cloud-sql-python-connector
------
Dockerfile:15
--------------------
13 | RUN pip install pg8000
14 | RUN pip install google-cloud-secret-manager
15 | >>> RUN pip install google-cloud-sql-python-connector
16 | CMD ["python","main.py"]
17 |
--------------------
ERROR: failed to solve: process "/bin/sh -c pip install google-cloud-sql-python-connector" did not complete successfully: exit code: 1

 

 
I wonder what went wrong, please will you help? Thank you very much! 🙂

The issue you're encountering is due to the fact that the google-cloud-sql-python-connector is not available for the Alpine-based image because it requires specific system libraries and tools that are not present in the minimal Alpine Linux distribution.

To resolve this, you should either:

  • Switch to a Debian-based Python image, which comes with more pre-installed system libraries.
  • Install additional dependencies in your Alpine-based image to meet the requirements of the google-cloud-sql-python-connector.

Here's how to proceed with both approaches:

  1. Switching to a Debian-based Python Image Update your Dockerfile to use a Debian-based Python image. This is often simpler and can avoid the need to manually install missing dependencies:
 
FROM python:3.10-slim
WORKDIR /app
COPY variables.env /app/
COPY main.py /app/
COPY templates /app/templates

# Install dependencies
RUN pip install --upgrade pip
RUN pip install --no-cache-dir flask
RUN pip install psycopg2-binary
RUN pip install python-dotenv
RUN pip install sqlalchemy
RUN pip install pg8000
RUN pip install google-cloud-secret-manager
RUN pip install google-cloud-sql-python-connector

CMD ["python", "main.py"]
  1. Installing Additional Dependencies on Alpine If you prefer to stick with Alpine, you need to install GCC and other build dependencies required for compiling the native extensions:
 
FROM python:3.10-alpine
WORKDIR /app
COPY variables.env /app/
COPY main.py /app/
COPY templates /app/templates

# Install build dependencies
RUN apk add --no-cache \
    gcc \
    g++ \
    libffi-dev \
    musl-dev \
    postgresql-dev \
    && pip install --upgrade pip

# Install Python packages
RUN pip install --no-cache-dir flask
RUN pip install psycopg2-binary
RUN pip install python-dotenv
RUN pip install sqlalchemy
RUN pip install pg8000
RUN pip install google-cloud-secret-manager
RUN pip install google-cloud-sql-python-connector

CMD ["python", "main.py"]

Complete Dockerfile for Alpine Here's a complete Dockerfile with the necessary dependencies installed:

 
# Use an official Python runtime as a parent image
FROM python:3.10-alpine

# Set the working directory
WORKDIR /app

# Copy files to the working directory
COPY variables.env /app/
COPY main.py /app/
COPY templates /app/templates

# Install build dependencies and upgrade pip
RUN apk add --no-cache \
    gcc \
    g++ \
    libffi-dev \
    musl-dev \
    postgresql-dev \
    && pip install --upgrade pip

# Install Python packages
RUN pip install --no-cache-dir flask
RUN pip install psycopg2-binary
RUN pip install python-dotenv
RUN pip install sqlalchemy
RUN pip install pg8000
RUN pip install google-cloud-secret-manager
RUN pip install google-cloud-sql-python-connector

# Make port 80 available to the world outside this container
EXPOSE 80

# Run main.py when the container launches
CMD ["python", "main.py"]

Notes:

  • Debian-based images (like python:3.10-slim) come with more pre-installed libraries and tools, making it easier to install complex packages.
  • Alpine-based images are minimal and require you to manually install many dependencies needed to build and run Python packages that require native extensions.

Hi,

Thank you for your reply! I got the solution, I should be running this line of code in my Dockerfile.

RUN pip install "cloud-sql-python-connector[pg8000]"
 

I can build the image now, thank you for your reply! Appreciate it. 🙂