DEV Community

norsemanGrey
norsemanGrey

Posted on

Flask Web App on Raspberry Pi Using Docker

I hope this question is not to broad. If so I apologize. I would just like some opinions and input, and I have been struggling to find anything relevant elsewhere. Please feel free to correct me if my question does not make sense or I have issue with the terminology etc. I am quite new to all of this.

I want to to deploy my first attempt at a Flask web application. I have Raspberry Pi at home running Pi-Hole and PiVPN on Raspbian Lite, on which I would also like to run my web server. I wonder if there is any advantage of running the web server on a Docker container on the RPi?

Can / should Docker run along-side Pi-Hole and PiVPN on the RPi, and if so what kind of docker image / web server is it recommended that I use? Or should I skip using Docker and just deploy the Flask application and its requirements directly on Raspbian with Pi-Hole and PiVPN.?

Top comments (3)

Collapse
 
uminer profile image
Moshe Uminer

I don't have any experience with Raspberry Pi, but I'll offer my thoughts regardless, in the hope that it will be helpful.

First, is this a production deployment? If so, deploying to a Raspberry Pi is a bad idea, because it probably won't be able to handle the load. Additionally, deploying to production from your local network is in general a bad idea.

However, I think (or at least will assume for the purpose of this comment) that you don't intend for the application to be open to the world, but rather for it to be a learning experience, or to be used on a local network.

The next question is: are there a lot of complicated dependencies to the project? The purpose of docker (as far as I understand) is to provide a controlled, ship-able environment. Using docker means that you won't run into issues when deploying to the server, because everything is neatly packaged inside the docker image/container. This is important when you rely on a lot of different technologies. So, a lot of pip packages probably doesn't qualify.
For more tips on when to use Docker, see here.

I assume you want to use docker so as to separate the python dependencies, however, you can just use a python virtual environment to solve that issue. See here for more information.

You should also take into consideration whether your Raspberry Pi even has enough RAM to run the docker daemon and the containers. Although docker containers are smaller than full-blown virtual machines, they may still require enough memory to be an issue.

Finally, as to the web server, Flask comes with a built-in development server, but a more robust option (for if the server will be receiving more than a couple requests) is to use a WSGI server like Gunicorn.

In a real life deployment, there is generally a server like nginx that stands in front of your application server and acts as a proxy.

Some links that you may find useful:

About Python's WSGI servers
Python and Docker

Collapse
 
norsemangrey profile image
norsemanGrey

Thank you for a thorough and helpful reply. The links you provided was also very useful.

You are absolutely correct that this is not an application that is to be intended to be open to the world (though I would like it to be reachable by myself and perhaps some family/friends outside my LAN). This is for now mostly for learning and experience. However, I do have plans later for a site with specific purpose, but it will be used by only a few people and so the load should be minimal.

Currently there are not many dependencies, just a selection of pip packages for a typical web-site (Login, SQLAlchemy, SQLite etc.), but this might be expanded upon later.

I really do not have any concrete intentions of using Docker for this application, but I wanted to investigate if this was a good approach for several reasons:

1) To deploy the application on available hardware (RPi) without "disturbing" the other applications running there (Pi-Hole, PiVPN).
2) Easily remove, back-up, re-deploy the entire application environment if required.
3) I want to learn how to use Docker.

But if using Docker for this is not a good approach I am seeking advice on what is. I really would like to be able to use my RPi (3B+) at this stage, if feasible, but alternatively I could also set up an Ubuntu machine on which to deploy the application.

Thank you for the input on using a WSGI server / nginx. I definitely have some reading to do there. Although my application for now will not handle many requests, that might change in the future :)

Collapse
 
uminer profile image
Moshe Uminer • Edited

The issues that bring you to Docker are solvable with standard python tools (well, except for the one about wanting to learn docker).

I suggest reading the official tutorial on virtual environments.
Take note of the pip freeze and pip intall -r at the end of the tutorial, as they allow for easy setup in a new environment. Not as easy as docker, though to achieve similar functionality, I would suggest hosting your source code (with a requirements.txt file, generated by pip freeze) on a remote git server like github.
Create a virtual environment, use git to download the updated source code from github, pip install requirements.txt, and you'll be ready to start the server.

By the way, it turns out that there exist a very lightweight docker image for pi-hole. I assume it can run on RPi, so maybe you can use that for learning. In fact, if they can make an image so small, I wonder if it's possible to build a slim python/flask image that way (though it would be overkill).

As for disturbing other applications, if you plan on the flask app being exposed on port 80, you're going to run into problems with pihole regardless (i.e. even with a docker image), see the readme for the pihole docker image I linked above. I don't know more about solving this than what it says there. In terms of disturbing the python environment, however, the solution is a python virtual environment, as mentioned above.

[An alternative to the standard library venv, there is also pipenv, here is a tutorial for it]

EDIT: as for deploying for small time use from your local network, I am pretty sure that is not recommended, but I believe that I've seen tutorials for it.