DEV Community

Cover image for Creating a Docker Image for a Simple python-flask "hello world!" application.
Tandap Noel Bansikah
Tandap Noel Bansikah

Posted on • Updated on

Creating a Docker Image for a Simple python-flask "hello world!" application.

Building a Docker Image for a Simple Hello World Flask Application

Introduction to Docker

Docker is a platform for developing, shipping, and running applications. It enables developers to package their code and dependencies into a standardized unit called a container. Containers are lightweight and portable, making them ideal for deploying applications across different environments.

What is an Image?

An image is a static representation of a container. It contains all the necessary code and dependencies required to run an application. Images can be created from scratch or by extending existing images.

Building a Hello World Flask Application Image

To build a Hello World Flask application image, follow these steps:

1. Create a Dockerfile*:

A Dockerfile is a text file that contains instructions for building a Docker image. For a simple Hello World Flask application, the Dockerfile might look like this:



FROM python:3.12-slim-buster

WORKDIR /usr/src/app

COPY requirements.txt .
RUN pip install -r requirements.txt

COPY . .

CMD ["python", "app.py"]


Enter fullscreen mode Exit fullscreen mode

2. Create the Python Flask Application Code:

Create a file named app.py with the following code:



from flask import Flask

app = Flask(__name__)

@app.route("/")
def hello_world():
    return "Hello, World!"

if __name__ == "__main__":
    app.run(host='0.0.0.0')


Enter fullscreen mode Exit fullscreen mode

3. Build the Image:

To build the image, run the following command:



docker build -t hello-world-flask .


Enter fullscreen mode Exit fullscreen mode

Expected results:

Image description
4. Run the Image:

To run the image, run the following command:



docker run -p 5000:5000 hello-world-flask


Enter fullscreen mode Exit fullscreen mode

This will start a container based on the Hello World Flask application image and expose port 5000 of the container to port 5000 of the host machine.

5. Access the Application:

To access the application, open a web browser and navigate to http://localhost:5000 . You should see the Hello World message.

Image description

So, that's the end of this project, in case of any questions or errors, please write in the comments and i will resolve them.

Also i have the repo for the github actions you can check it.
github actions for flask application and Docker

Top comments (2)

Collapse
 
tpoisson805 profile image
Tim Poisson

Seems there is an error in your Dockerfile. buster, bullseye, and bookworm are the 3 alternative Docker images (beginning with the letter B) for Linux Distros. "burster" (with an 'r') is a typo.

Also, Python 3.8 was release in 2019 and scheduled for end of life this year (2024). Python 3.12 is the most current package. Any particular reason you are using an outdated version for this example?

Collapse
 
bansikah profile image
Tandap Noel Bansikah • Edited

Thank you for pointing that out Sir @tpoisson805 , i will update.