Day 17 - Embracing DevOps with Docker  (Dec 13, 2023)

Day 17 - Embracing DevOps with Docker (Dec 13, 2023)

Hello DevOps enthusiasts! 🚀 We hope you're enjoying the #90daysofdevops journey as much as we are. Today's challenge is extra special because it involves diving into the world of Docker—a powerful tool for containerization. Get ready for some hands-on action as we embark on a Docker project designed for DevOps engineers.

Understanding Dockerfile

At the heart of Docker is the Dockerfile—a set of instructions that guides the creation of containers. Containers are lightweight, portable, and contain everything an application needs to run. In essence, it's like packaging your application and its dependencies into a neat, isolated box.

What's Inside a Dockerfile?

A Dockerfile typically includes instructions like selecting a base image, copying files, running commands, and setting up the environment. For instance, if you're creating a container for a web application, your Dockerfile might instruct Docker to use an official web server image, copy your website files, and start the server when the container launches.

👉 For a deep dive into Dockerfiles, check out the official documentation here.

Today's Task: Building a Simple Web App Container

Steps to Conquer the Challenge:

  1. Create a Dockerfile:

    • Choose a web application framework (e.g., Node.js, Python).

    • Write a Dockerfile with instructions for building a container.

    # Use an official base image
    FROM node:14

    # Set the working directory
    WORKDIR /app

    # Copy package.json and package-lock.json to the working directory
    COPY package*.json ./

    # Install dependencies
    RUN npm install

    # Copy the rest of the application files
    COPY . .

    # Expose a port (e.g., 3000) that the app will run on
    EXPOSE 3000

    # Command to run the application
    CMD ["npm", "start"]

  1. Build and Run the Container:

    • Use the Dockerfile to build an image.
    docker build -t my-web-app .

  • Run a container based on the created image.
    docker run -p 3000:3000 my-web-app

  1. Verify Your Application:

    • Access the running application in a web browser at http://localhost:3000 to ensure everything is working smoothly.
  2. Push to a Repository:

    • Share your creation with the world! Push the image to a public or private repository like Docker Hub.
    docker login
    docker tag reddit:latest hassanb111/reddit:latest
    docker push hassanb111/reddit:latest