Docker Project

Docker Project

Introducing "DockFlask": Revolutionize web development with Docker and Flask's simplicity, creating dynamic and scalable applications effortlessly. Unleash the power of containerization and Python's elegance to elevate your web projects to new heights.

Project Description

The project is a simple Flask web application that displays a "Hello, World!" message.

Step-by-Step Guide

  1. Create a Project Directory: Create a new directory for the project, and navigate to that directory using the command line.

     mkdir docker-project
     cd docker-project
    
  2. Create a Flask App: Create a new file called app.py in the project directory, and add the following code to it:

     from flask import Flask
     app = Flask(__name__)
    
     @app.route('/')
     def hello_world():
         return 'Hello, World!'
    
  3. Create a Dockerfile: Create a new file called Dockerfile in the project directory, and add the following code to it:

     # Use an official Python runtime as a parent image
     FROM python:3.8-slim-buster
    
     # Set the working directory to /app
     WORKDIR /app
    
     # Copy the current directory contents into the container at /app
     COPY . /app
    
     # Install any needed packages specified in requirements.txt
     RUN pip install --trusted-host pypi.python.org -r requirements.txt
    
     # Make port 80 available to the world outside this container
     EXPOSE 80
    
     # Define environment variable
     ENV NAME World
    
     # Run app.py when the container launches
     CMD ["python", "app.py"]
    
  4. Create a Requirements File: Create a new file called requirements.txt in the project directory, and add the following code to it:

     Flask
    
  5. Build the Docker Image: Use the docker build command to build the Docker image from the Dockerfile.

     docker build -t my-flask-app .
    
  6. Run the Docker Container: Use the docker run command to run the Docker container from the built image and specify the port mapping between the container and the host.

     docker run -p 4000:80 my-flask-app
    
  7. Test the Flask App: Open a web browser and navigate to http://localhost:4000/ to see the "Hello, World!" message displayed.

  8. Stop the Docker Container: Use the docker stop command to stop the running Docker container.

     docker stop <container-id>
    
  9. Remove the Docker Container: Use the docker rm command to remove the stopped Docker container.

     docker rm <container-id>
    
  10. Remove the Docker Image: Use the docker rmi command to remove the Docker image.

    docker rmi my-flask-app
    

That's it! You've now completed a simple Docker project that utilizes various Docker commands.