1

I'm deploying django app with GCP Cloud build service. In cloudbuild.yaml I define step to apply migrations with gcr.io/google-appengine/exec-wrapper tool:

- id: "apply migrations"
    name: "gcr.io/google-appengine/exec-wrapper"
    args:
      [
          "-i",
          "gcr.io/$PROJECT_ID/${_SERVICE_NAME}",
          "-s",
          "${PROJECT_ID}:${_REGION}:${_DB_INSTANCE_NAME}",
          "-e",
          "GOOGLE_CLOUD_SECRETS_NAME=${_GOOGLE_CLOUD_SECRETS_NAME}",
          "-e",
          "DJANGO_SETTINGS_MODULE=${_DJANGO_SETTINGS_MODULE}",
          "--",
          "python",
          "manage.py",
          "migrate",
      ]

Everything is working fine with one "default" database. Then:

DATABASES = {
    "default": env.db("DATABASE_URL"),
    "replica": env.db("REPLICA_DATABASE_URL"),
}

When I added new mysql replica database with name gcp-replica-2015-07-08-1508, Cloud build service starting failing with error:

django.db.utils.OperationalError: 

(2002, "Can't connect to local MySQL server through socket 

'/cloudsql/myproject-228819:us-central1:gcp-replica-2015-07-08-'

I checked my db config by printing DATABASES variable in Cloud build and config have correct data, but if you look on error you will notice that error returns with cut string at the end ! without -1508

If I skip this step and deploy my app with same config to Cloud Run everything is working fine.

Service account have following roles:

enter image description here

Replica DB Version MySQL 5.7

Arti
  • 7,356
  • 12
  • 57
  • 122

1 Answers1

0

According to the guide of Running Django on the Cloud Run environment provided by Google, you should use environ to save the database configuration.

In a secure env file, save the settings needed to launch Django. The secret value is retrieved via the Secret Manager API, and the values are loaded into the Django environment using the django-environ package.

The setup for your SQL database is stored in the settings.py file. The DATABASES setting is updated to infer use of the Cloud SQL Auth proxy if the USE CLOUD SQL AUTH PROXY setting is applied.

When you use gcr.io/google-appengine/exec-wrapper, and it is designed to be run as a step in a Cloud Build job, you must send, as arguments, the path of the deployed application image, any environment variables to set, any Cloud SQL instances to make available, and the command to run. Here is an example Cloud Build configuration:

steps:

name: "gcr.io/google-appengine/exec-wrapper"
  args: ["-i", "gcr.io/my-project/appengine/some-long-name",
         "-e", "ENV_VARIABLE_1=value1", "-e", "ENV_2=value2",
         "-s", "my-project:us-central1:my_cloudsql_instance",
         "--", "bundle", "exec", "rake", "db:migrate"]

You can find the image path using gcloud app versions described.

Here are some related question with the same error message you are getting that could probably help: