2

I want to deploy a Flask app on Google App Engine Flex using Docker.

I set up the Docker image locally and managed to run a Flask app using the commands docker build -t myreponame . and then docker run -p 4000:80 myreponame. I was then able to see my app on the URL http://localhost:4000 and it worked fine.

However when I try and deploy it on Google App Engine using gcloud app deploy, and go to the URL http://YOUR_PROJECT_ID.appspot.com, I get a "502 Server Error".

I am suspecting maybe a port configuration. Should I define the following differently in my Flask code?

if __name__ == '__main__': HOST = '0.0.0.0' PORT = 80 app.run(HOST, PORT, debug=True) 

Or should I define my app.yaml file differently?

runtime: custom env: flex entrypoint: gunicorn -b :$PORT app:app 

My Dockerfile contains the following:

FROM python:2.7 WORKDIR /app COPY . /app RUN pip install --trusted-host pypi.python.org -r requirements.txt EXPOSE 80 CMD ["python", "app.py"] 

Any help would be very welcome. Many thanks

2 Answers 2

4

The issue was indeed in the port configuration. The App Engine front end routes incoming requests on port 8080 (reference: https://cloud.google.com/appengine/docs/flexible/custom-runtimes/build?authuser=0), so I changed my Flask code to:

if __name__ == '__main__': HOST = '0.0.0.0' PORT = 8080 app.run(HOST, PORT, debug=True) 

I removed the entrypoint config from app.yaml:

runtime: custom env: flex 

and changed Dockerfile to:

FROM python:2.7 WORKDIR /app COPY . /app RUN pip install --trusted-host pypi.python.org -r requirements.txt EXPOSE 8080 CMD ["gunicorn", "app:app", "-b", ":8080", "--timeout", "300"] 
Sign up to request clarification or add additional context in comments.

Comments

-1

Why don't you use docker run with '-p 80:80' instead of '-p 4000:80'?

Or you also have a Nginx or sth to making port forwarding?

2 Comments

I was actually following the instructions in the Docker tutorial docs.docker.com/get-started/part2/#build-the-app I don't use Nginx or any tool to making port forwarding.
I actually thought gcloud app deploy would take care of the port forwarding for me. Is there any option I should add to that command?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.