I'm trying to connect to my cloudsql database in Docker via the cloud proxy, but Django keeps throwing a connectionerror.
docker-compose.yml:
version: '3.8'
services:
web:
build: .
depends_on:
- tom_postgres_db
command: python3 manage.py runserver 0.0.0.0:8080
volumes:
- .:/app
ports:
- 8080:8000
- 8000:8000
env_file:
- .env
tom_postgres_db:
image: gcr.io/cloudsql-docker/gce-proxy:1.16
ports:
- 5432:5432
command: ./cloud_sql_proxy -instances=${INSTANCE_CONNECTION_NAME}=tcp:0.0.0.0:5432 -credential_file=/config
volumes:
- ${GOOGLE_APPLICATION_CREDENTIALS}:/config
restart: always
.env:
DEBUG=1
DJANGO_SETTINGS_MODULE=my-settings
SECRET_KEY=my-secret-key
DJANGO_HOST=127.0.0.1
GOOGLE_APPLICATION_CREDENTIALS=path-to-credentials
INSTANCE_CONNECTION_NAME=my-connection-name
SQL_ENGINE=django.db.backends.postgresql
SQL_DATABASE=my-db-name
SQL_USER=my-db-user
SQL_PASSWORD=my-db-pw
SQL_PORT=5432
django.db.utils.OperationalError: could not connect to server: Connection timed out
Is the server running on host "127.0.0.1" and accepting
TCP/IP connections on port 5432?
docker-compose up from project root../cloud_sql_proxy -version): 1.16Hi @jlibermann, I'm happy to help you troubleshoot this. Can you verify that the Cloud SQL Proxy is running and ready for connections? My suspicion is that the proxy isn't running, possibly because of an authentication issue. From briefly looking at your docker-compose.yaml file, the issue could have something to do with volume mapping the credential file, but since I don't know what the actual value of $GOOGLE_APPLICATION_CREDENTIALS is, I can't say for sure. Can you post the logs for your tom_postgres_db service?
Could you also post your settings.py just in case it's an issue with how you've configured the database in Django?
Shubha's suspicion seems like a good one to me, I would definitely start with the things mentioned in the preceding comment.
Another thing I just happened to notice, but I'm not sure whether it's relevant/important:
The error message from Django says it was trying to connect to 127.0.0.1, but the docker-compose.yml configured the proxy to listen on 0.0.0.0 (which means: accept connections from outside the server). If the Django app is not on the same virtual server(/host) as the Proxy, then connecting to 127.0.0.1 (aka localhost) will not work since the "local" host is not the same host as where the Proxy is running; you'll have to instead provide the address of where the Proxy is running somehow. (And if the Proxy and Django app are indeed on the same host, then the Proxy's config probably doesn't need to specify that it wants to listen on 0.0.0.0, though it's not "wrong".)
From these docs I stumbled upon, it seems like you should be having Django try to access the proxy via a hostname rather than 127.0.0.1, i.e. tom_postgres_db; but I admit I've never used Docker Compose before so this is a shot in the dark :)
From looking at those docs, I think @Carrotman42 is right that using the hostname would fix the issue. A possibility is that you didn't specify the host in your Django database settings, and your application defaulted to using localhost. Try adding
{'HOST': 'tom_postgres_db'} to your database settings in settings.py and see if that fixes it.
Hi, after adding {'HOST': 'tom_postgres_db'} and fixing the ports, I was able to run my development server. Thank you for the help!
Most helpful comment
From looking at those docs, I think @Carrotman42 is right that using the hostname would fix the issue. A possibility is that you didn't specify the host in your Django database settings, and your application defaulted to using localhost. Try adding
{'HOST': 'tom_postgres_db'}to your database settings in settings.py and see if that fixes it.