🐳Docker Project for DevOps Engineers

🐳Docker Project for DevOps Engineers

Diving into a hands-on Docker project to build and deploy a web application in containers.


🔧 What is a Dockerfile?

A Dockerfile is like a recipe 📜 that tells Docker how to cook up a container. It includes instructions like:

  • Which base image to use.

  • What files to include.

  • What commands to run.

Think of it as a step-by-step guide for creating a containerized version of your application.


🧱 Complete Dockerfile Example

Here’s what the Dockerfile for a Python-based web app looks like:

# 1] Use a lightweight Python base image - slim 
FROM python:3.8-slim  

# 2] Set the working directory  
WORKDIR /app  

# 3] Copy all application files into the container  
COPY . .  

# 4] Install app dependencies from requirements.txt  
RUN pip install -r requirements.txt  

# 5] Expose port 5000 for the application  
EXPOSE 5000  

# 6] Command to run the app when the container starts  
CMD ["python", "app.py"]

🛠️ Dockerfile Step-by-Step Explanation

1️⃣ Choose a Base Image

Start with a lightweight base image. This is the foundation of your container.

FROM python:3.8-slim

2️⃣ Set the Working Directory

Specify where the application files will reside inside the container.

WORKDIR /app

3️⃣ Copy Application Files

Move your application files from your local machine to the container.

COPY . .

4️⃣ Install Dependencies

Run a command to install all the necessary libraries or tools.

RUN pip install -r requirements.txt

5️⃣ Expose Ports (Optional)

Inform Docker about which ports the application will use.

EXPOSE 5000

6️⃣ Set the Default Command

Define the command that will run when the container starts.

CMD ["python", "app.py"]

🏗️ Steps to Build and Run Your App

  1. Build the Docker Image 🏗️
    Create a Docker image based on your Dockerfile.

     docker build -t my-python-app .
    

    📄 Explanation:

    • docker build: Command to create a Docker image.

    • -t my-python-app: Tags the image with the name my-python-app.

    • .: Refers to the current directory containing the Dockerfile.


  1. Run the Container 🚀
    Start your application inside a container.

     docker run -p 5000:5000 my-python-app
    

    📄 Explanation:

    • docker run: Starts a new container.

    • -p 5000:5000: Maps port 5000 of the container to port 5000 on your machine.

    • my-python-app: The name of the Docker image.


🐳 Important Docker Commands for This Project

Here are some useful Docker commands you’ll use in this project:

  1. View Running Containers 🖥️

     docker ps
    

    Lists all the currently running containers.

  2. View All Containers (Including Stopped) 🔍

     docker ps -a
    

    Shows both running and stopped containers.

  3. Stop a Running Container 🛑

     docker stop <container_name_or_id>
    

    Stops a container gracefully.

  4. Remove a Container 🗑️

     docker rm <container_name_or_id>
    

    Deletes a container from your system.

  5. Remove an Image 🗂️

     docker rmi <image_name_or_id>
    

    Deletes a Docker image.


🛠️ Advanced Concept: Push Your Image to Docker Hub

Once your app is running perfectly, you can share it with the world by pushing it to a Docker registry (like Docker Hub).

  1. Log in to Docker Hub

     docker login
    
  2. Tag Your Image

     docker tag my-python-app <your-dockerhub-username>/my-python-app
    
  3. Push the Image

     docker push <your-dockerhub-username>/my-python-app