3

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).

5

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.

1
  • 1
    Note: this option is only available in the "flex" environment, and can't be used with the "standard" environment – kbuilds Jan 13 '20 at 7:34
2

You are supposed to run manage.py locally, in your development environment, to organize files in view of deployment, not once deployed on servers. You may gather more detail from the "Running Django in the App Engine Flexible Environment" online document.

5
  • 1
    I've read that documentation. The way they configure database credentials there is very unsafe (committing them to the app repo? sigh). Once you do it safely, with django-dotenv for example, it's really cumbersome to connect to the production database from my development workstation and I still need to run migrations (which should happen automatically) and create a superuser (which should be a manual task). – Pablo Oct 17 '18 at 6:07
  • In App Engine, the local filesystem that your application is deployed to is not writeable. This behavior ensures the security and scalability of your application. – George Oct 18 '18 at 22:57
  • That's fine, I don't need it to be writable. – Pablo Oct 19 '18 at 6:39
  • You may consider implementing a special service that you may call, to do the work you need done for you and run manage.py on those servers. This is the preferred way to access your app, once deployed in the Cloud. If you need to write to persistent storage, it can't be on local file systems, but to Google Cloud Storage instead. – George Oct 19 '18 at 21:16
  • Running manage.py on the server is totally fine with the use cases mentioned : running database migrations and creating a super user. – Ponytech Aug 12 '20 at 7:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.