Search code examples
mysqldjangogoogle-cloud-platformdockerfileport

How to fix port error when creating Django backend service on Google Cloud Run?


I have a Django REST backend image in which I tried to create a service for it on Google Cloud Run. In the Google Cloud Run, I set the port to 6000, and when I tried to create it, it showed these errors:

RuntimeWarning: Got an error checking a consistent migration history performed for database connection 'default': (2002, "Can't connect to MySQL server on '35.226.227.51' (115)")

The user-provided container failed to start and listen on the port defined provided by the PORT=6000 environment variable.

Default STARTUP TCP probe failed 1 time consecutively for container "backend" on port 6000. The instance was not started.

The last 2 layers on the Dockerfile of my backend are:

EXPOSE 6000

CMD python manage.py makemigrations && python manage.py migrate && python manage.py runserver 0.0.0.0:6000

I am not sure how to fix this for this is my first time trying to deploy a website.

For the database error, I have checked the database credentials and configured my Django settings.py to connect to my Google Cloud SQL, MySQL instance with the public IP address as shown and made sure the configuration is correct, but not really sure why it is showing that error.

Any help would be much appreciated!


Solution

  • The issue has now been solved and here's what I did:

    To solve for the database connection error, I configured my settings.py as such:

    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.mysql',
            'HOST': '/cloudsql/<instance connection name>',
            'USER': 'root',
            'PASSWORD': '<root password>',
            'NAME': '<database name>',
        }
    }
    

    And to solve the port errors, I changed my port from 6000 to 9000 (I found out that some browsers block port 6000 specifically), and changed the last 2 lines of my Dockerfile from this:

    EXPOSE 6000
    
    CMD python manage.py makemigrations && python manage.py migrate && python manage.py runserver 0.0.0.0:6000
    

    To this:

    CMD python manage.py makemigrations && python manage.py migrate && python manage.py runserver 0.0.0.0:9000