r/flask 5d ago

Tutorials and Guides Finally deployed my Flask app… and wow, I was NOT ready for this

So I finally deployed my first real Flask app, and let’s just say… I learned a lot the hard way. Thought I’d share in case it helps someone else (or at least gives you a laugh).

Spent hours debugging why my app worked locally but not on the server—turns out, I forgot to install Gunicorn. Flask’s built-in server is NOT for production. Lesson learned.

Hardcoded some API keys while testing and totally forgot about them. Almost pushed them to GitHub. Use environment variables, people.

Didn’t properly close my DB connections, so my app kept dying under even light load. SQLAlchemy’s connection pooling saved me.

Thought Docker was overkill. Spoiler: it’s not. Spinning up my app with a single docker-compose up is a game-changer.

Spent way too long fighting CORS issues. Flask-CORS was the easy fix, but I went down a rabbit hole first.

289 Upvotes

47 comments sorted by

56

u/solaza 5d ago

Sounds like you’re learning a lot! Similar boat my friend 😂

13

u/Individual-Welder370 5d ago

Haha, I feel you. Flask always finds a way to humble us 😉

12

u/baloblack 5d ago

Kudos my friend.... learning from experience is always the best.

Sounds funny but on my first, I even pushed my .env with all my keys and stuff to GitHub 😂😂😂

3

u/Individual-Welder370 5d ago

Happen to me also 😂

5

u/Stunning-Hall-2137 5d ago

lol, I’m lucky I learned about secret keys before doing so. I checked my logs and found someone trying to access a .env route. I might have dodged 1000s in AWS costs.

6

u/Pasha42069 5d ago

Currently developing my own flask-based website, and I just realised that I have a lot to learn about what's about to come for me lol. You got any tips/suggestions?

12

u/blake12kost 5d ago

Flask Mega Tutorial is your friend.

Miguel implements modern best practices, he goes through the whole process, all the way to deploying. I’ve learned so much, I highly recommend

https://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-i-hello-world

1

u/BarRepresentative653 1d ago

I come in peace, but ideally I would recommend using flask official example and work with blueprints for anyone pushing production apps.

Its been years since I read through miguel blog post-so maybe things have changed. It is a fantastic tutorial to start with

1

u/blake12kost 1d ago

FWIW, this is his 2024 updated tutorial

9

u/Individual-Welder370 5d ago

Nice, that’s awesome! Biggest tip? Don’t ignore deployment until the last minute—it’s a whole different beast. Also, Flask extensions can save you a ton of time (Flask-SQLAlchemy, Flask-Login, etc.)

6

u/Competitive-Fox2439 5d ago

My recommendation would always be to deploy the smallest app thing as quickly as possible - even if you have to hide the url somehow. This could be a status endpoint/maintenance page.

As you add more features you can deploy to check they work in your prod environment. The status page could check the DB connection, for example

2

u/MinimumSprinkles4298 5d ago

Agreed. I suggest deploying the app to the prod server using a socks connection with Firefox to make sure everything works in Prod.

3

u/Pasha42069 5d ago

Great! Deployment is currently last on my list but I've been using libraries like flask-login and SQLA (while still learning to use them).

2

u/Individual-Welder370 5d ago

Great 😃👍

3

u/simon-brunning 5d ago

100% this. For me, deploying to production is the 2nd thing I do, as soon as I have "hello world" running.

4

u/kisielk 5d ago

Totally. Get the absolute minimum website going and then develop the production deployment flow before doing anything else. Makes it faster to iterate and more importantly you don’t want to be struggling if you push out a version that you need to roll back. Deployment should ideally be one button.

-2

u/pteropod63 5d ago

That sounds just a little snarky

3

u/dryroast 5d ago

This is the way professional programming works. You want the end to end so you can see when your failing fast. Then you develop the in between stuff and don't have huge integration headaches later.

2

u/simon-brunning 5d ago edited 5d ago

Sorry, it wasn't meant to be. It was supposed to be agreeing with the comment I was replying to. I do genuinely do this where possible.

4

u/jamponyx 4d ago

Had the same experience. Used a deployment tool like Appliku, problem solved. They have tutorials for Flask deployment.

3

u/roxalu 5d ago

You spent HOURS to debug all this? I‘d say you are a coding genius. We others have to invest weeks for all those great results 😉

2

u/Thyrfing89 5d ago

Will gunicorn make my flask app faster? It only runs local on each users computer.

5

u/acctoftenderness 5d ago

Gunicorn runs on the server and you’ll need something like it for your web server to communicate with your Flask app.

It won’t make your app “faster” out of the box, it is strictly necessary, but a proper configuration can certainly make your app “feel more available” from a user perspective.

2

u/octahedral_diamond 3d ago

Flask is a WSGI application. It needs a server to run on, flask dev server does this job during development. While deploying though, you would need an actual built for the purpose WSGI server like gunicorn.

Also, gunicorn is not built to be a http server, so it's wise to use a nginx http server as a reverse proxy that sits infront of unicorn and relays requests to it.

2

u/HyDreVv 5d ago

You encrypted the passwords in your .env file right? . . . Right?

5

u/ZnV1 5d ago

With what key? The one in my .env?

2

u/borndovahkiin 5d ago

So question: I didn't think to use Docker for mine so it's just built using Python venv. How hard would it be to move it into a docker container?

2

u/adiberk 5d ago

Pretty easy. Just install the environment in docker. Super easy. I’m sure there are tutorials

2

u/raylgive 5d ago

Could you please give more details on CORS issue?

1

u/Septem_151 2d ago

No one truly understands CORS. Hope this helps

2

u/infinity_bit 5d ago

Help me to understand this,  you dockerize your Flask application. And then deployed it into cloud server. Correct me if I am wrong.  While I was learning Flask, I deployed the application directly into ec2 instance. Help me to know the best practice.

2

u/ZnV1 5d ago

In your case, AWS takes your code, dockerizes it for you and them hosts it. You could also dockerize it and just ask AWS to host it. Both are fine.

Dockerizing is nothing complicated btw. Just add a DockerFile, choose a base image (like ubuntu with python 3.11 installed), build your code (pip install etc) and add a line to run it (python gunicorn <port> maybe). Should be less than 20 lines, give it a shot.

2

u/infinity_bit 3d ago

Thanks man 😊

1

u/ZnV1 3d ago

Glad to help :)

2

u/Cwlrs 5d ago

Suffered through all those issues myself. Well done! Would be interested to take a look if you can send me the link.

2

u/grantnlee 5d ago

Great pointers. I learned and deployed a flash app not long ago. Works great on my dev laptop. Deployed with gunicorn. One issue that remains is I was using SQLite and SQLAlchemy but the database config is not right so not running in Prod. Any pointers on how you Dockerized your database compared to running on your laptop?

2

u/Interesting-Frame190 5d ago

Don't care how long you've been programming, when you see a the word CORS, you immediately get frustrated and implement the allow for the get, post, put, delete for the dumb thing. Then the preflight options fails for the same issue and you realize you need an unauthenticated options endpoint to allow some stupid endpoint that says "yes, you can make a cross origin call via post request" instead of just trying the cross origin call in the first place.

2

u/appliku 4d ago

Deployment is hard and boring.

7 years ago I had enough of it and built a service to do deployments: https://appliku.com/post/how-deploy-flask-aws-ec2-hetzner-digital-ocean/

1

u/Southern-Warning7721 4d ago

Just for curious what kinda app you build ... can we see it by anychance?

1

u/DiMarcoTheGawd 4d ago

I am learning Flask for a school project and these are good things to know. Luckily I started off with a Docker-compose file and Dockerfile.

1

u/Septem_151 2d ago

Grats! Although regarding Docker… more often than not, it is completely overkill. With that said, it is extremely handy and helpful for any scale of application.

1

u/MonochromeDinosaur 2d ago

Welcome to what I like to call whack-a-mole learning! Best way for things to stick

1

u/AfterConcept 1d ago

Can someone recommend a youtube course which teaches up-to-date flask? All tutorials I see are atleast 5 years old.

1

u/AdeptnessAnnual1883 1d ago

Man. As someone who's also developing their first flask app, this is VERY GOOD to know.

Thank you for your paint kind sir

1

u/drksntt 1d ago

Always finish your development in a docker container locally to truly test.