Introduction
- I started late, but I believe it's okay - there are no age limit for learning new things. I am not a Python or Flask developer, but my main interest is how I can start or prepare a Docker Container for Python developers an app development environment. I learn all by practice with the help of Google, YouTube, ChatGPT and KodeKloud tutorial.
Environment Setup
- You will need to have a Linux server and where Docker must be installed; I am using my local Proxmox server, where I installed Ubuntu OS on the VM and I start by typing docker --version to check the current Docker version.
Step-by-Step Building Python Flask App on Docker Container
- At the biggening we need to pull the ubuntu image from Docker Hub (Docker image repositories) with bash prompt to install all the required dependencies.
- docker run -it ubuntu bash
- by this you will be able to check your Flask server from the host OS only
- docker run -it -p 5000:5000 ubuntu bash
- If you wants to access the app outside the host machine - I will use this
- apt-get update
- to update all dependencies
- In this stage we will pre-configure a noninteractive file which will avoid the interaction during Python installation and it's dependencies.
- export DEBIAN_FRONTEND=noninteractive
apt-get update
apt-get install -y tzdata
ln -fs /usr/share/zoneinfo/Asia/Dhaka /etc/localtime
dpkg-reconfigure -f noninteractive tzdata
apt-get install -y python3 python3-setuptools python3-dev build-essential python3-pip default-libmysqlclient-dev - apt install python3-venv -y
- by this we will install and setup virtual environment dependencies for Python
- Now we will make a folder where we will keep all of our dev environment files and folders.
- mkdir my-first-flask-app
- to make a new folder where we will build our app
- cd my-first-flask-app
- to get into the app folder
- It's time to make the virtual environment folder and activate the source and we will finally install the Flask.
- python3 -m venv flaskenv
- this is the step where we are creating the virtual environment and give flaskenv name of the directory - you can use any name
- source flaskenv/bin/activate
- we need to activate the virtual environment source
- pip install flask
- python flask will be install
- Time to create the Flask app folder to /opt/ , you can make the folder anywhere you wish.
- cat > /opt/app.py
- type this command and then immediately paste the below simple Python Flask code to app.py - where we will import from flask library and give a name and then use route to return some text. at the end of the like we expose the port 5000 by this we will be able to access the web app; remember to save the file press - CTL + D two times
- Please click download the app.py code from here
from flask import Flask
app = Flask(name)
@app.route("/")
def main():
return "Welcome!"
@app.route("/how are you")
def hello():
return "I am good, how about you?"
if name == "main":
app.run(host="0.0.0.0", port=5000)- Finally my first Python flask app is ready for checking
- python3 /opt/app.py
- this will start the Flask app server
- Here is my final output
- I am able to access through external computer from same network; like mine is: http://192.168.22.172:5000
Challenges & Lessons Learned
- Challenge I have face during this was some of the tutorial and articles from the web was written and test with older version of software and their dependencies which might works perfectly during their time but in 2025 the software version and dependencies changes.
- Lessons I learnt is that, no matter the time when I am trying or following any documents and instructions or tutorials must read the error code and find out the solutions by keep trying in different ways.
Conclusion
- Finally I am happy to finish and see the output of my first simple Python flask web app which I ran on my local Ubuntu server inside Docker Container. Now I am enough confident to start or build the dev environment for any developer team.
Comments
Post a Comment