Skip to main content

Command Palette

Search for a command to run...

Day 17 Task: Docker Project for DevOps Engineers

Updated
4 min read

Hello, team! 🌟 You’re all doing an incredible job with #90daysOfDevOps, and today’s challenge is a thrilling one: a hands-on Docker project! Ready to enhance your DevOps skills? 😍

What is Docker?

Docker is a platform that allows you to develop, ship, and run applications inside containers. Containers are lightweight, portable, and self-sufficient units that package your application and its dependencies, ensuring it runs consistently across different computing environments.

What is a Dockerfile?

A Dockerfile is a script that contains a set of instructions on how to build a Docker image. Think of it as a recipe for creating containers. It specifies the base image to use, the application dependencies to install, configuration steps to perform, and the commands to execute when the container starts.

Key Concepts in a Dockerfile

  1. Base Image:

    • The FROM instruction sets the base image for your Docker image. This is the starting point for building your container. Base images could be as simple as an operating system (e.g., Ubuntu) or more complex images with pre-installed software (e.g., Python, Node.js).

    • Example: FROM python:3.9 specifies that the container should be built using the Python 3.9 image.

  2. Working Directory:

    • The WORKDIR instruction sets the working directory inside the container. This is where subsequent commands will be executed.

    • Example: WORKDIR /app sets the working directory to /app inside the container.

  3. Copy Files:

    • The COPY instruction copies files or directories from the host machine into the container’s file system.

    • Example: COPY requirements.txt . copies the requirements.txt file from your host into the container’s current directory.

  4. Install Dependencies:

    • The RUN instruction executes commands inside the container during the image build process. This is often used to install software or dependencies.

    • Example: RUN pip install --no-cache-dir -r requirements.txt installs the Python dependencies listed in requirements.txt.

  5. Application Code:

    • Another COPY command is typically used to add the rest of the application code into the container.

    • Example: COPY . . copies all files from the host’s current directory into the container’s current directory.

  6. Command to Run:

    • The CMD instruction specifies the command that will be executed when the container starts. It defines the default behavior of the container.

    • Example: CMD ["python", "app.py"] runs the app.py script using Python when the container starts.

Why Use Dockerfiles?

  1. Consistency:

    • Dockerfiles ensure that every instance of your application runs in a consistent environment, regardless of where the container is deployed.
  2. Reproducibility:

    • By using a Dockerfile, you can reproduce the exact environment needed for your application, making it easier to test and debug.
  3. Version Control:

    • Dockerfiles can be versioned alongside your application code, making it easy to track changes and maintain different versions of your application.
  4. Portability:

    • Dockerfiles allows you to package your application and its dependencies into a container that can run on any system with Docker installed, whether it’s your local machine, a staging environment, or a production server.

For more details on Dockerfiles, check out the Dockerfile Documentation.

Today’s Task

1. Create a Dockerfile:

Set up a Dockerfile for a simple web application, such as a Node.js or Python app. Here’s a basic example of a Python app:

# Use the Python 3.9 image as the base
FROM python:3.9

# Set the working directory in the container
WORKDIR /app

# Copy the requirements file into the container
COPY requirements.txt .

# Install the Python dependencies
RUN pip install --no-cache-dir -r requirements.txt

# Copy the rest of the application code
COPY . .

# Specify the command to run on container start
CMD ["python", "app.py"]

2. Build the Docker Image:

Use your Dockerfile to create a Docker image. Run:

docker build -t my-web-app .

3. Run the Docker Container:

Start the container from your image and check that it’s working by visiting it in a web browser. Use:

docker run -p 8000:8000 my-web-app

4. Push the Image to a Repository:

Upload your Docker image to Docker Hub or another container registry. First, tag the image:

docker tag my-web-app myusername/my-web-app

Then, push it:

docker push myusername/my-web-app

Wrap-Up

By completing today’s task, you’ll gain practical experience in creating Dockerfiles, building Docker images, running containers, and managing image repositories. Enjoy the challenge, and keep up the fantastic work! 🚀

Feel free to share your progress and any questions you have in the comments below. Happy Dockering! 🎉

More from this blog

#90DaysOfDevOps Challenge - Day 1 Devops

21 posts