0

I am using cloud_proxy to connect to google cloud postgres instance. I followed the steps in GCP website https://cloud.google.com/sql/docs/postgres/connect-admin-proxy. When I run it locally using python manage.py runserver with host for db as 127.0.0.1 and port as 5432, the program is working fine.

If I try to dockerize the application and run the program, I am facing the error

could not connect to server: Connection refused Is the server running on host "localhost" (127.0.0.1) and accepting TCP/IP connections on port 5432?

Docker file

services: web: build: . command: python manage.py runserver

volumes:
  - .:/code
ports:
  - 8000:8000

So I tried to dockerize the application using the stack overflow answer Is there a way to access google cloud SQL via proxy inside docker container modified the host in settings.py file too.

Now facing the error gcloud is not in the path and -instances and -projects are empty

services:

web:

build: .
command: python manage.py runserver
depends_on:
  - cloud-sql-proxy
volumes:
  - .:/code
ports:
  - 8000:8000
env_file:
  - ./.env.dev

cloud-sql-proxy:

image: gcr.io/cloudsql-docker/gce-proxy:1.16

command: /cloud_sql_proxy --dir=/cloudsql instances=abc:us-central1:def=tcp:0.0.0.0:5432 -credential_file=/secrets/cloudsql/credentials.json

ports:
  - 5432:5432

volumes:
  - credentials.json:/secrets/cloudsql/credentials.json

restart: always

Could you please help me with this issue. My requirement is to create a docker image with Django application so that it can be deployed to GCP.

Aji CS
  • 13
  • 3

2 Answers2

1

I think You are missing - It should be

command: /cloud_sql_proxy --dir=/cloudsql -instances=abc:us-central1:def=tcp:0.0.0.0:5432 -credential_file=/secrets/cloudsql/credentials.json

neeraj9194
  • 178
  • 9
0

I recommend you to follow the next documentation:

Connecting psql client using the Cloud SQL Proxy docker Image

This page describes how to connect a psql client to your Cloud SQL instance, from a client machine running Linux or Compute Engine Linux instance, using the Cloud SQL Proxy Docker image, I think this guide could meet your needs.

This guide mentions the way to start the proxy at point 9. Unix sockets:

docker run -d -v /cloudsql:/cloudsql \
  -v <PATH_TO_KEY_FILE>:/config \
  gcr.io/cloudsql-docker/gce-proxy:1.16 /cloud_sql_proxy -dir=/cloudsql \
  -instances=<INSTANCE_CONNECTION_NAME> -credential_file=/config

If you are using the credentials provided by your Compute Engine instance, do not include the credential_file parameter and the -v <PATH_TO_KEY_FILE>:/config line. If you are using a container optimized image, use a writeable directory in place of /cloudsql, for example:

-v /mnt/stateful_partition/cloudsql:/cloudsql

Additionally if you want to know more about Cloud SQL Proxy parameters and flags I recommend to take a look at this page

I hope this information would be useful to you

Jose Luis Delgadillo
  • 2,348
  • 1
  • 6
  • 16