Yves
Yves

Reputation: 301

Flask app deployment on Google App Engine Flex using Docker

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

Upvotes: 2

Views: 1520

Answers (2)

Yves
Yves

Reputation: 301

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"]

Upvotes: 4

Disuan
Disuan

Reputation: 343

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?

Upvotes: -1

Related Questions