# Containerizing Joy: Building and Dockerizing Your Python Hello World Web App

In this blog, we will discuss building a simple web app dockerized it, and running it into the container with the practical demo. By the end of this guide, you'll not only have a Dockerized web app but also a grin of accomplishment.

## **Why Docker?**

Docker provides a standardized environment, ensuring your app runs consistently across different machines. It encapsulates your application and its dependencies into a container, making deployment a breeze. Plus, it plays well with Hello World projects—simple yet profound.

### Prerequisite

* **Docker**
    

But before building the dockerized web app, let's try to build the non-dockerized web app so that we can see the cons of it and it will help us to know the benefits of building the dockerized applications from now.

Let's write the code in app.py file:

```plaintext
from flask import Flask

app = Flask(__name__)

@app.route("/")

def hello():
    return "Hello World from DevOps Learner"

if __name__ == "__main__":
    app.run(host="0.0.0.0", port=5000)
```

My above code defines a basic Flask web app with a single route ("/") that returns the message "Hello World from DevOps Learner." The `if __name__ == "__main__":` block ensures the app runs only when the script is executed directly, starting the Flask development server on `http://0.0.0.0:5000` .

We need to install the flask in the machine if it is not already present, since <mark> Flask </mark> is required to run this web app.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1696563717044/ed8c8482-f80f-4cd3-b110-c7861af5b720.png align="center")

We have successfully installed the flask in our system. Now lets run the app

```plaintext
python3 app.py
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1696563899055/bef3b6fd-d2f9-4c67-a49d-389b249a45ae.png align="center")

Hurray 🥳🥳, we have successfully launched our web app.

Before executing this straightforward <mark>Hello World web app</mark>, we had to set up several dependencies on our system, such as Python3, the pip module, and Flask. Surprisingly, even for a basic Python application, we found ourselves installing various prerequisites. The hassle didn't stop there; when working with code in other languages, the cycle repeated—we had to install their specific dependencies. This routine became cumbersome, demanding programmers to install different dependencies every time they ran an application.

In response to this challenge, we transitioned to Dockerization. Here, we encapsulate our code along with its dependencies in a Dockerfile. By building an image from this file, we create a portable environment that can be effortlessly launched in any setting. This approach eliminates the need for repetitive manual installations, providing a streamlined solution for developers.

## **Dockerizing the Magic**

### **Dockerfile:**

Create a file named `Dockerfile` (no file extension) in your project directory. This file contains instructions for building a Docker image. Add the following:

```plaintext
FROM python:3.8-slim

COPY app.py .

RUN python3 -m pip install flask

CMD ["python3","app.py"]
```

Here this **Dockerfile** uses the <mark>Python 3.8-slim</mark> base image, copies the "[app.py](http://app.py)" file into the container, installs <mark>Flask</mark> using **pip**, and sets the command to run "[app.py](http://app.py)" upon container start.

### Build the docker image

```plaintext
docker build -t hellopython . #"." for current directory
```

> \-t: tagging the image with the name "hellopython". You can replace "hellopython" with any other name that you find suitable for your image.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1696565738015/8aeaf98e-7c26-41ee-aaea-c16981d4a8a3.png align="center")

The docker image is built successfully. Let's verify it using the command:

```plaintext
docker images | grep "hellopython"
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1696565849498/8b4b2bab-fc71-44a0-847b-46f1932fd858.png align="center")

### **Now, run your Dockerized app:**

> <mark>syntax: </mark> docker run -itd --name container\_*name -p host\_port:container\_port docker*\_image\_name

```plaintext
docker run -itd --name devops-hello -p 5000:5000 hellopython
```

### **Verify:**

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1696567386882/e2c5b582-e6af-4b83-941e-2e920aac01a0.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1696567649918/099f8d3b-ec5a-4d78-8768-15729cc6d2fc.png align="center")

Here, we observe the creation of two containers: one named "devops-hello," running on the host port 5000, and another labeled "devops-hello-world," operating on the host port 5080. Remarkably, this flexibility extends beyond just two instances—we can generate multiple containers using the same Docker image. The only adjustments required are specifying a distinct container name and assigning a unique host port for each instance. This versatility allows for the seamless scaling of containers while maintaining a straightforward and consistent process.

### Push Docker Image to DockerHUb

In order to push the docker image into the docker hub, run the following commands in the terminal.

1. **Docker login**
    
    ```plaintext
    docker login
    ```
    
2. **Tag Your Docker Image**
    
    ```plaintext
    docker tag docker_image username/repository-name
    ```
    
    1. **Push the Docker Image**
        
        ```plaintext
        docker push username/repo-name
        ```
        
        ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1696569049610/2bcd2a27-3b1b-4df8-984d-c10055dd5ebf.png align="center")
        

Let's see whether our image is pushed into the docker hub or not.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1696569108350/188f2ef3-d719-4c4b-8d30-1d6c0ab97dc1.png align="center")

Hurray 🥳🥳, we have successfully pushed our image into the docker hub.

In this manner, Dockerization provides a universal solution for packaging and deploying applications across diverse environments. By encapsulating both the application and its dependencies in a Docker image, we gain the flexibility to seamlessly utilize them wherever needed. Furthermore, the convenience of pushing and pulling these images from Docker Hub ensures effortless sharing and deployment. This streamlined process not only enhances portability but also simplifies the management of applications, making Docker a powerful tool for developers across various scenarios.

Happy Learning!!
