# Containerizing a Python Web Application: A Docker Project

As a challenge for day17 , we are going to create a Dockerfile for a simple web application, build the image, run the container, and push it to a public or private repository.

## **Prerequisites**

To follow this tutorial, you will need the following:

* A computer running a Linux-based operating system (e.g., Ubuntu, CentOS)
    
* Docker installed on the system
    
* A web application (e.g., Node.js, Python app) that you want to containerize
    

we will create Dockerfile for this git repository : [https://github.com/patelajay745/StudyBud.git](https://github.com/patelajay745/StudyBud.git)

## **Step 1: Clone the project**

The first step is to clone the project repository. For this example, we will be using a sample Python web application called "StudyBud". You can replace it with your own application.

```bash
git clone https://github.com/patelajay745/StudyBud.git
cd StudyBud
```

## **Step 2: Set up a virtual environment**

Next, we will set up a virtual environment to install the dependencies for the application. This is done to avoid dependency conflicts with other Python applications installed on the system.

```bash
pip install virtualenv
virtualenv envname
envname\scripts\activate
pip install -r requirements.txt
```

## **Step 3: Test the application**

Before containerizing the application, we need to ensure that it is working as expected. Run the following command to start the Django development server:

```bash
python manage.py runserver 0.0.0.0:8000
```

Open your web browser and navigate to [`http://localhost:8000`](http://localhost:8000). If everything is working correctly, you should see the StudyBud application running.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1683650188791/1275962a-3c10-494e-abe4-a74958d24fc5.png align="center")

## **Step 4 - Creating a Dockerfile**

The first step in containerizing a web application is to create a Dockerfile. A Dockerfile is a text file that contains a series of instructions used by Docker to build an image.

here is Docker file for it :

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1683648605743/2eb9f693-7020-445e-b0d7-305a4a646270.png align="center")

In this Dockerfile, we start with the base image `python:3.9-slim-buster`, which is a minimal image of Python 3.9 installed on the slim version of the Debian Buster operating system.

We then set the working directory to `/app`, copy the `requirements.txt` file into the container, and install the dependencies using pip.

Next, we copy the contents of the current directory into the container, expose port 8000 for the web application, and start the web application using the `CMD` instruction.

Note that the `CMD` instruction specifies the command to run when the container starts. In this case, we are starting the Django web application by running the `python` [`manage.py`](http://manage.py) `runserver 0.0.0.0:8000` command.

## **Step 5 - Building the Docker Image**

Once we have created the Dockerfile, we can use it to build the Docker image.

To build the Docker image, navigate to the directory containing the Dockerfile and run the following command:

```bash
docker build -t <image-name> .
```

for our project:

```bash
sudo docker build -t studybud .
```

it will start building image of it (like below screenshot)

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1683648867066/e4235b51-b4a0-4a1e-912e-9299fcdf6ea8.png align="center")

## **Step 6 - Running the Docker Container**

Once we have built the Docker image, we can use it to run a Docker container.

To run the Docker container, use the following command:

```bash
docker run -p 8000:8000
```

for our project :

```bash
sudo docker run -p 8000:8000 studybud
```

This will start the container and map port 8000 on the container to port 8000 on the host machine. Open your web browser and navigate to [`http://yourpublicIP:8000`](http://localhost:8000). If everything is working correctly, you should see the StudyBud application running.

ps: Don't forget to add 8000 port in inbound security if you are using AWS.

## **Step 7- Push the Image to a Repository**

The final step in our Docker project is to push the Docker image that we built to a public or private repository. In this example, we'll use Docker Hub, a popular public Docker registry.

To push the image to Docker Hub, we first need to create a Docker Hub account and login to Docker Hub from the command line. You can create a Docker Hub account at [**https://hub.docker.com/signup**](https://hub.docker.com/signup).

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1683650577209/7c9f4e63-8b94-4c4a-9b17-2e7898a6179c.png align="center")

Once you have created a Docker Hub account, you can login to Docker Hub from the command line using the following command:

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1683650722546/71e6ee75-f810-458e-b34f-ba3317e59554.png align="center")

After logging in, we need to tag our Docker image with a name that includes our Docker Hub username. For example, if your Docker Hub username is `johndoe`, you can tag your image with the following command:

```bash
docker tag mywebapp johndoe/mywebapp
```

Finally, we can push our tagged Docker image to Docker Hub using the following command:

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1683650816998/53eb814c-04f9-49fc-8d33-58c090c8069c.png align="center")

This will upload the Docker image to Docker Hub, where it will be publicly available for others to use. You can check by login into to your docker hub account.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1683651022904/341ddcfc-041e-47eb-a9c4-7e70eb9d98dc.png align="center")

That's it. We have created our docker file and have built our image out of it and uploaded it to public domain.

If you have a query/suggestion please comment below.

Happy coding.
