How to execute manage.py in Google App Engine

djangogoogle-app-enginegoogle-cloud-platform

I followed this tutorial:

https://codeburst.io/beginners-guide-to-deploying-a-django-postgresql-project-on-google-cloud-s-flexible-app-engine-e3357b601b91

to get a Django app deployed to Google App Engine. It's there, it's working, but I need to run migrations and create a user. How am I supposed to run manage.py on those servers? I found how to SSH to them from the Google Cloud Platform console, but once I'm in the server, I can't find how to load the correct Python environment and/or run manage.py.

I also read Running Django in the App Engine Flexible Environment which doesn't explain how to do it and their configuration file seems to assume database credentials will end up in the git repo for the application, which is a big nono. I'm not doing that, which means connecting to the production database from my workstation is cumbersome.

If this was Heroku, for example, I would do this on my dev workstation:

heroku run python manage.py migrate

or

heroku run python manage.py createsuperuser

and it would execute those commands in the server. I'm trying to do the same with Google App Engine Flexible (as well as Standard).

Best Answer

Perhaps this is useful for someone else who encounters this. It is certainly advisable to use @George's answer for normal operation, but in a pinch you can shell into run a python command from App Engine. I recently had to for debugging purposes. I used the SSH access in App Engine -> Instances in the GCP Control Panel. In my case I was using the flex environment which runs docker inside an VM instance so there are a few steps.

  1. Go to GCP -> Engine -> Instances and SSH into an instance.
  2. Once an SSH session is running make sure your containers are running: docker ps. In my case my Django app was running in a container called gaeapp.
  3. Docker exec into the container: docker exec -it gaeapp /bin/bash
  4. If that command works you are now in a running container for your app. Run your command. For example: python manage.py help

NOTE: These environments should be considered ephemeral and you should not make this part of any consistent workflow. When an App Engine instance is changed from debug mode back to normal operation it is very likely it will be destroyed and a new instance will replace it. This means any files generated will be lost. It also means any files generated will only exist on one of potentially many VM instances.

Related Topic