☑️Day 27: Exploring DockerFile & Real-World Use Cases🚀

☑️Day 27: Exploring DockerFile & Real-World Use Cases🚀

🔹Table of Contents :

  • Introduction to Dockerfile

  • Basic Dockerfile Structure

  • Real-World Scenario: Dockerizing a Node.js Web Application

  • Building and Running the Docker Image

  • Real-World Use Case: Handling Version Mismatches

  • Moderate-Level Example: Dockerizing a Python Flask App

  • Useful Dockerfile Commands Cheat Sheet

  • Key Takeaways


✅1. Introduction

Today, I took a deep dive into Dockerfile, a powerful tool that automates the creation of Docker images. Understanding how to write a Dockerfile is crucial for packaging applications and ensuring consistency across different environments. This newsletter will guide you through the basics of Dockerfile, relevant commands, and a real-world scenario that illustrates its usage.


✅2. What is a Dockerfile?

A Dockerfile is a simple text file that contains a series of instructions on how to build a Docker image. These instructions define the environment and application setup, including installing dependencies, copying files, and running commands.


✅3. Why Use Dockerfile?

  • Consistency: Every time you build an image, it’s the same.

  • Automation: Eliminates manual steps in setting up environments.

  • Reproducibility: Enables running the application the same way on different systems.


✅4. Basic Dockerfile Structure

A Dockerfile typically includes the following key instructions:

  1. FROM: Specifies the base image.

  2. COPY/ADD: Copies files from the host to the container.

  3. RUN: Executes a command inside the container.

  4. CMD/ENTRYPOINT: Specifies the command that runs when the container starts.

  5. EXPOSE: Opens a port.

  6. WORKDIR: Sets the working directory inside the container.


✅Real-World Scenario: Web Application Dockerization

Let’s take the example of a Node.js web application. You’re part of a DevOps team where a developer finishes coding, and your responsibility is to deploy this application on multiple servers using Docker.

Here’s how you can create a Dockerfile for a Node.js web app to make deployment easy and consistent:


✅5. Project 1: Creating a Dockerfile for Node.js Web App

  1. Create a Dockerfile in your project directory:

     bashCopy codetouch Dockerfile
    
  2. Write the Dockerfile:

     DockerfileCopy code# Step 1: Use a Node.js base image
     FROM node:16
    
     # Step 2: Set the working directory
     WORKDIR /usr/src/app
    
     # Step 3: Copy the package.json and install dependencies
     COPY package*.json ./
     RUN npm install
    
     # Step 4: Copy the rest of the application files
     COPY . .
    
     # Step 5: Expose the application port
     EXPOSE 3000
    
     # Step 6: Define the command to run the app
     CMD ["npm", "start"]
    
  3. Build the Docker image:

     bashCopy codedocker build -t my-node-app .
    
  4. Run the container:

     bashCopy codedocker run -p 3000:3000 my-node-app
    

✅Breakdown of the Dockerfile:

  • FROM node:16: Uses Node.js 16 as the base image.

  • WORKDIR /usr/src/app: Sets the working directory inside the container to /usr/src/app.

  • COPY package*.json ./: Copies the package.json and package-lock.json to the container.

  • RUN npm install: Installs dependencies inside the container.

  • COPY . .: Copies the rest of the application files to the container.

  • EXPOSE 3000: Exposes port 3000 for external access.

  • CMD ["npm", "start"]: Starts the Node.js app.


✅Handling Version Mismatches

Imagine you’re working in a large organization with multiple developers. One developer works on the project in 2021, while another joins in 2024. Over time, the Node.js version, package dependencies, and system settings may have changed.

Using Dockerfile:

  • You define the specific Node.js version (Node 16).

  • You ensure the application always runs the same way, even if the environment changes.

  • You avoid the "It works on my machine" problem by shipping the environment along with the code.


✅6. Project 2 : Dockerfile for a Python Flask App

Scenario: You’re working on a Python Flask app that needs to be deployed with a Redis database.

  1. Create the Dockerfile:

     DockerfileCopy code# Step 1: Use a Python base image
     FROM python:3.9-slim
    
     # Step 2: Set the working directory
     WORKDIR /app
    
     # Step 3: Install dependencies
     COPY requirements.txt .
     RUN pip install --no-cache-dir -r requirements.txt
    
     # Step 4: Copy the app code
     COPY . .
    
     # Step 5: Expose Flask app port
     EXPOSE 5000
    
     # Step 6: Define the entry point
     CMD ["python", "app.py"]
    
  2. Build and run the image:

     bashCopy codedocker build -t my-flask-app .
     docker run -p 5000:5000 my-flask-app
    

Scenario Benefits:

  • Ensures a consistent Python version across different environments.

  • Simplifies setting up the environment (installing packages, configuring the app).


✅7. Useful Dockerfile Commands Cheat Sheet:

  • FROM: Define the base image.

      bashCopy codeFROM node:16
    
  • COPY: Copy files or directories into the container.

      bashCopy codeCOPY package.json /app/
    
  • RUN: Run a command inside the container at build time.

      bashCopy codeRUN npm install
    
  • CMD: Define the default command that runs when the container starts.

      bashCopy codeCMD ["npm", "start"]
    
  • EXPOSE: Expose a port to the outside world.

      bashCopy codeEXPOSE 3000
    
  • WORKDIR: Set the working directory inside the container.

      bashCopy codeWORKDIR /app
    
  • ENV: Set environment variables inside the container.

      bashCopy codeENV NODE_ENV=production
    
  • ADD vs COPY:

    • COPY: Copies files from your local machine.

    • ADD: Adds files, but can also pull from URLs or extract tar files.


✅8. Key Takeaways:

  • Dockerfile is crucial for automating and simplifying the process of creating Docker images.

  • By using Dockerfile, you create a consistent, reproducible environment for your applications.

  • Whether you're packaging a Node.js, Python, or any other type of application, Dockerfile allows you to easily deploy and scale in different environments.


🚀Thanks for joining me on Day 26! Let’s keep learning and growing together!

Happy Learning! 😊

#90DaysOfDevOps

#DevOps #Docker #Dockerfile #Containerization #CloudComputing #TechLearning #NodeJS #PythonFlask #ShubhamLonde #Day27

💡
Follow for more updates on LinkedIn , Github and Twitter(X)