Getting Started with Flask "Hello, World!" on Docker
In the dynamic world of web development, Docker has emerged as a powerful tool for containerizing applications. This guide will walk you through creating a simple Flask "Hello, World!" application and running it on Docker. By the end of this article, you'll have a solid understanding of how to get a Flask app up and running in a Docker container.
Setting Up Your Environment
Before we dive into creating our Flask application, ensure you have the following tools installed on your system:
- Python (3.8 or later)
- Docker (20.10.7 or later)
- Docker Compose (1.29.2 or later)
Installing Flask
First, let's install Flask using pip, Python's package installer:

pip install flask
Creating the Flask "Hello, World!" Application
Create a new directory for your project and navigate into it. Then, create a new file named app.py with the following content:
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello():
return "Hello, World!"
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000)
Understanding the Code
The code above creates a simple Flask application with a single route ('/') that returns the message "Hello, World!". The app.run method is used to run the application, with the host and port specified for Docker compatibility.

Creating the Dockerfile
In the project directory, create a new file named Dockerfile (with no file extension) and add the following content:
FROM python:3.8-slim-buster
WORKDIR /app
COPY requirements.txt .
RUN pip install -r requirements.txt
COPY . .
CMD ["python", "app.py"]
Understanding the Dockerfile
The Dockerfile above uses the official Python 3.8 image as its base. It then sets the working directory to /app, copies the project files into the container, and specifies the command to run the Flask application.
Running the Flask Application on Docker
Create a new file named docker-compose.yml in the project directory and add the following content:

version: '3'
services:
web:
build: .
ports:
- "5000:5000"
Now, run the following command to build and start the Docker container:
docker-compose up -d
Once the container is up and running, open your web browser and navigate to http://localhost:5000. You should see the message "Hello, World!".
Conclusion and Next Steps
In this article, we've created a simple Flask "Hello, World!" application and containerized it using Docker. This is a great starting point for building more complex Flask applications and running them in Docker containers. Next, consider exploring Flask's features, such as routing, templates, and static files, and learn how to integrate them with Docker.





















