Building and Deploying a Simple Web Application using Docker

Building and Deploying a Simple Web Application using Docker

A Step-by-Step Guide to Dockerizing and Deploying a Python Web Application

Introduction: In this blog post, we will walk through the process of Dockerizing a simple web application written in Python. Docker is a powerful tool that allows developers to package applications and their dependencies into a container, ensuring consistent and reliable deployment across different environments. We will create a Dockerfile, build the Docker image, run a container, and finally, push the image to a public or private repository like Docker Hub.

Prerequisites: Before we begin, make sure you have Docker installed on your system. You can download and install Docker from the official website

Step 1: Create the Python Web Application: For this tutorial, let's create a basic Python web application using the Flask framework. Save the following code in a file named app.py .

from flask import Flask

app = Flask(__name__)

@app.route('/')
def hello():
    return 'Hello, Docker World!'

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

Step 2: Write the Dockerfile: A Dockerfile is a script that defines how the Docker image will be built. Create a file named Dockerfile in the same directory as your app.py file with the following content:

# Use the official Python base image
FROM python:3.9

# Set the working directory inside the container
WORKDIR /app

# Copy the current directory contents into the container at /app
COPY . /app

# Install application dependencies
RUN pip install --no-cache-dir -r requirements.txt

# Expose port 5000
EXPOSE 5000

# Define the command to run the application
CMD ["python", "app.py"]

Step 3: Build the Docker Image: To build the Docker image, open a terminal (or command prompt) in the directory containing the Dockerfile and app.py files. Run the following command:

docker build -t python-web-app .

The -t option tags the image with the name python-web-app.

Step 4: Run the Docker Container: After the image has been built, you can run a container using the following command:

docker run -d -p 8080:5000 python-web-app

The -d flag runs the container in detached mode, and -p maps port 5000 of the container to port 8080 on the host. Now, your Python web application should be accessible at localhost:8080 in your web browser.

Step 5: Verify the Application: Open a web browser and navigate to localhost:8080. You should see the message "Hello, Docker World!" displayed, indicating that your web application is working as expected.

Step 6: Push the Image to Docker Hub (or other registry): To share your Docker image with others or use it on other machines, you can push it to a public or private Docker registry. In this example, we'll use Docker Hub.

First, create a Docker Hub account if you don't already have one (hub.docker.com).

Next, log in to Docker Hub using the following command:

docker login

Enter your Docker Hub credentials when prompted.

Finally, tag your image with your Docker Hub username and the desired repository name:

docker tag python-web-app:latest  <pranay1023.docker.hub>/python-web-app:latest

Now, push the image to the repository:

docker push <pranay1023.docker.hub>/python-web-app:latest

Conclusion: Congratulations! You have successfully Dockerized a Python web application, built a Docker image, ran a container, and pushed the image to a Docker registry. Docker allows for easy and consistent deployment of applications, making it a valuable tool for any DevOps engineer. You can now share your containerized application with others, ensuring seamless deployment across different environments. Happy coding!