๐Ÿณ Getting Started with Docker: Building and Running a Simple Flask Web Application | #Day17 #90daysofdevops

ยท

4 min read

๐Ÿณ Getting Started with Docker: Building and Running a Simple Flask Web Application | #Day17 #90daysofdevops

Introduction

In the world of DevOps, containerization has revolutionized the way applications are developed, deployed, and managed. Docker, a leading containerization platform, has made it incredibly easy to package applications and their dependencies into a single unit called a container. In this tutorial, we'll dive into the basics of Docker by creating and deploying a simple web application using a Docker container.

๐Ÿ“ฆ What is a Dockerfile and Why is it Important?

A Dockerfile is a text file that contains a set of instructions for building a Docker image. An image is a lightweight, standalone, and executable software package that encapsulates everything needed to run an application, including code, runtime, libraries, and environment variables. Dockerfiles allow developers to automate the image creation process and ensure consistent deployments across different environments.

๐Ÿ”ง Step-by-Step Guide: Creating and Deploying a Simple Flask Web Application

Step 1: Create a Project Directory

Start by creating a new directory for your project:

mkdir my_python_app
cd my_python_app

Step 2: Write the Flask Application

Next, let's create a simple Flask web application. Create a file named app.py within your project directory and add the following code:

from flask import Flask

app = Flask(__name__)

@app.route('/')
def hello():
    return "Hello, Devops ki duniya!"

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

Step 3: Create a Dockerfile

Now, let's define the Dockerfile to build an image for our Flask app. Create a file named Dockerfile (no file extension) in the project directory and add the following content:

# Use an official Python runtime as a parent image
FROM python:3.9

# Set the working directory to /app
WORKDIR /app

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

# Install any needed packages specified in requirements.txt
RUN pip install Flask

# Make port 80 available to the world outside this container
EXPOSE 5000

# Define environment variable
ENV NAME World

# Run app.py when the container launches
CMD ["python", "app.py"]

Step 4: Build the Docker Image

In the same directory as your Dockerfile, build the Docker image using the following command:

docker build . -t my_python_app

Step 5: Run the Docker Container

Once the image is built, run a container based on this image:

docker run -d -p 5000:5000 my_python_app

Step 6: Verify the Application

Open your web browser and navigate to http://localhost:5000. You should see the message "Hello, Devops ki duniya!".

This means that your web application is running inside a Docker container and responding to your requests. You can also check the logs of your container by using the docker logs command, which shows the output of the app.js file:

docker logs <container_id>

Push the Image to Docker Hub

To make your image accessible to others, you can push it to a container registry like Docker Hub. Here's how:

1. Tag the Image
docker tag my_python_app avanishnit08/first-repo:my_python_app

2. Log in to Docker Hub

First of all you have to login into your docker hub account using the following command:

docker login -u avanishnit08
#it will ask for password provide it and then use push command
3. Push the Image

Push your image to the repository using the docker push command, which will upload your image and its layers to the repository. For example, if you want to push your image to a repository called first-repo on Docker Hub, you can use this command:

docker push avanishnit08/first-repo:my_pyhton_app

#avanishnit08 = dockerhub userid
#my_python_app = docker image
#first-repo = repository name

output:

๐Ÿš€ Conclusion

Congratulations! You've successfully created a Dockerized Flask web application, built a Docker image, ran a container, verified the application's functionality, and pushed the image to a container registry. Docker's simplicity and flexibility have empowered developers and DevOps engineers to streamline the deployment process and ensure consistent environments across different stages of development. This tutorial provides a solid foundation to further explore Docker's capabilities and integrate it into your DevOps practices. Happy containerizing! ๐ŸŽ‰

ย