nickjj / docker-flask-example

A production ready example Flask app that's using Docker and Docker Compose.
MIT License
618 stars 102 forks source link

Tips on deploying #6

Closed shivam-grover closed 2 years ago

shivam-grover commented 2 years ago

Hi, First of all thank you for the great repository. I am new to docker and this repository is very helpful to me.

I have used the repository to dockerise one of my flask APIs and I am trying to deploy it on a VPS (racknerd to be more specific). I want to expose the API endpoint to the public so that anyone over the internet can use it.

So if I have to use localhost:8000/api_endpoint to hit the endpoint on my own system, the internet users should be able to use public.ip.address.of.vps:8000/api_endpoint. However I am unable to do this.

What have I tried?

  1. In the dockerfile, I changed the CMD gunicorn line to CMD ["gunicorn", "-c", "python:config.gunicorn", "-b", "0.0.0.0", "hello.app:create_app()"]

  2. In config/settings.py, I changed the ip in the SERVER_NAME from localhost to 0.0.0.0 "SERVER_NAME", "0.0.0.0:{0}".format(os.getenv("PORT", "8000"))

After doing the above changes, locally if I do 0.0.0.0:8000/api_endpoint it works fine. But if I try to do < public ip address of vps >:8000/api_endpoint, it gives the following 404 error on the browser:

Not Found The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again.

And in the ssh terminal where docker-compose up is running, the logs say this as a warning:

home/python/.local/lib/python3.10/site-packages/flask/app.py:1781: UserWarning: Current server name '<public ip of vps>:8000' doesn't match configured server name '0.0.0.0:8000' and also /home/python/.local/lib/python3.10/site-packages/flask/sessions.py:228: UserWarning: The session cookie domain is an IP address. This may not work as intended in some browsers. Add an entry to your hosts file, for example "localhost.localdomain", and use that instead.

After this I tried changing the 0.0.0.0 to in settings.py (also tried in the dockerfile gunicorn command, but that gave an error Invalid Address, so in the dockerfile gunicorn command the host was 0.0.0.0). This gave the following error on the browser:

Bad Request
The browser (or proxy) sent a request that this server could not understand.

and the following warning in the ssh terminal running docker-compose: /home/python/.local/lib/python3.10/site-packages/flask/sessions.py:228: UserWarning: The session cookie domain is an IP address. This may not work as intended in some browsers. Add an entry to your hosts file, for example "localhost.localdomain", and use that instead.

This is most probably just my lack of knowledge and I do not know what to tweak to get the app on the VPS exposed to the internet. Please guide me in the right direction.

Thank you

shivam-grover commented 2 years ago

I got it working right after posting this. Turns out what I did was fine, there was a small error with the params I was passing in the url. It's just what 12 hours of coding does to you.

nickjj commented 2 years ago

Hi,

You technically don't need to edit the Dockerfile or settings.py file.

You can just set your SERVER_NAME environment variable to whatever hostname you want to serve your site on such as public.ip.address.of.vps:8000.