If you have a NAS, then you’ve probably already tried out a bunch of different projects. There’s a lot you can do with one, especially if you built your own and control the entire machine and operating system. Even if you don’t, though, most at least support Docker. Docker is a container system that allows you to run different applications separated from each other in a controlled environment, and it’s a great and easy way to deploy applications.
One project you may be interested in trying is hosting your own website on your NAS, and we’ll show you how to do exactly that! You’ll be able to write a basic web server in Flask, containerize it using Docker, and then transfer that container to your NAS.
We’ve published the source code for this application on GitHub, and you can check it out at the end of this article! You’ll need to have Docker installed for this tutorial.
Related
Linux containers guide: An introduction to containers
Containers have been a buzzword for years… but what are they exactly?
How to host a Python Flask app on your NAS using Docker
Step 1: Write the code
Writing the code for your Python Flask application is pretty easy. All you need to do is create a program that can listen on a port, identify as a Flask app, and then serve a HTML template. This HTML file will need to go in a /templates folder inside of your project folder.
You will need to make sure that these files are called app.py for your Python code and index.html for your HTML code. Place them in the appropriate folders, and make sure that your code is the same as (or similar to) what’s in the above screenshot. You will need to pay special attention to your indentation in Python. As well, make sure that you have the Flask module installed by running the following command:
pip3 install flask
Once installed, this should already be in a working state on your machine!
Step 2: Creating your Docker container
There are two ways to create your Docker container, and we recommend using the Docker build command. Create a Dockerfile and populate it, and you can use all of the parameters in the above screenshot. You can change “World” to be any name that you want, and not having this variable defined means that Docker will name the environment for you.
Step 3: Create your requirements file
Your requirements file will contain the necessary Python modules that your container will need to run the application. For this basic program, all you’ll need is Flask and, in my case, I needed to use an older version of Werkzeug. Create a requirements.txt file in the same folder as your app.py and Dockerfile, and make sure it matches the screenshot above.
I am using an older version of Flask for this tutorial, but Flask 3.0 is currently out. You can update the dependency here if you know what you’re doing.
Step 4: Build your Docker container and save it
You’re now ready to go! Run the following command in a terminal in the same window to build your Docker container.
docker build -t my-flask-app .
This will build your Docker application. Now you can run it on your machine to make sure that it works with:
docker run -p 5000:5000 my-flask app
All going well, you can navigate to 127.0.0.1:5000 in your browser and it should work! If it didn’t, go back over this tutorial and see if you missed out on any steps.
Now you can save your Docker container with the following command to output it to a file, that you can transfer to your NAS.
docker save -o my-flask-app.tar my-flask-app
That’s it, you’ve now created your first Docker application!
Step 5: Transfer your Docker image to your NAS
Source: TrueNAS
Finally, you’ll need to transfer this Docker image to your NAS. Depending on the NAS that you’re using, this should be easy but will have different steps. In the above screenshot, you can see the “Launch Docker image” option on TrueNAS, and it’s as simple as copying your Docker file over and passing the right parameters to it in order to launch it.
For Synology and even Ugreen, though, we recommend checking the instructions on using Docker with those machines. You’ll be able to get your container running, but how exactly depends on the steps the company outlines to do it.
Docker is a safe and easy way to run programs on your NAS
Play around with docker-compose, too
We included a docker-compose file in the GitHub for this project, and you can play around with trying to get it working! It’s another way to pass the parameters needed to execute the container to Docker when it’s building it. It’s worth getting to grips with as a docker-compose file can be used for multiple different projects, something that may be useful if you start playing around with more Flask applications.
Related
8 incredible projects I completed with just my NAS
A NAS is a gateway into the world of computing, and it can teach you a lot.