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

bansikah

Tandap Noel Bansikah

Posted on January 29, 2024

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

💖 💪 🙅 🚩
bansikah
Tandap Noel Bansikah

Posted on January 29, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related