This project demonstrates how to create a simple Flask application that displays "Hello, World!" and containerize it using Docker.
docker-flask-hello-world/
├── app.py
├── Dockerfile
└── requirements.txt
- Docker installed on your system
- Basic knowledge of Python and Flask
-
Clone the repository or create the project structure as shown above.
-
Build the Docker image:
docker build -t flask-hello-world .
-
Run the Docker container:
docker run -p 5000:5000 flask-hello-world
-
Open a web browser and navigate to
http://localhost:5000
to see the "Hello, World!" message.
Create a new directory and navigate into it:
mkdir docker-flask-hello-world
cd docker-flask-hello-world
Create app.py
:
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello_world():
return '<h1>Hello, World!</h1>'
if __name__ == '__main__':
app.run(debug=True, host='0.0.0.0')
Create a virtual environment and install Flask:
python -m venv venv
source venv/bin/activate
pip install Flask
pip freeze > requirements.txt
Create a file named Dockerfile
:
FROM python:3.9-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
CMD ["python", "app.py"]
docker build -t flask-hello-world .
docker run -p 5000:5000 flask-hello-world
Open a web browser and go to http://localhost:5000
To stop the running container, use Ctrl+C
in the terminal where the container is running.
If you encounter any issues:
- Ensure Docker is running on your system.
- Check that port 5000 is not in use by another application.
- Verify that all files are in the correct location and have the correct content.