Skip to main content

Command Palette

Search for a command to run...

Containerizing a Python Web Application: A Docker Project

Updated
4 min read

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

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.

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.

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:

python manage.py runserver 0.0.0.0:8000

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

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 :

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 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:

docker build -t <image-name> .

for our project:

sudo docker build -t studybud .

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

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:

docker run -p 8000:8000

for our project :

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. 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.

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

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:

docker tag mywebapp johndoe/mywebapp

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

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.

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.

More from this blog

Ajay Patel

116 posts

Containerizing a Python Web Application: A Docker Project