Day 17 Task: Docker Project for DevOps Engineers

Day 17 Task: Docker Project for DevOps Engineers

Day 17

Dockerfile📄

Dockerfile is like a recipe📜 for your computer program. It's a special set of instructions that tells a program called Docker🐳 exactly how to set up and run your application. It includes everything your program needs to work correctly, like specific tools and settings. When you use Dockerfile, it ensures that your program runs smoothly on any computer, no matter where it is🌐. It's like a handy guide to make sure your application behaves the same way wherever you decide to run it.🚀

Common Dockerfile Commands

  1. FROM:

    • Specifies the base image from which you are building🏗️.
  2. RUN:

    • Executes commands during the image build process🛠️.
  3. COPY:

    • Copies files or directories from the build context (local machine) into the image📄.
  4. WORKDIR:

    • Sets the working directory for subsequent commands📂.
  5. CMD:

    • Provides a default command to run when the container starts🚀.
  6. EXPOSE:

    • Informs Docker that the container listens on specified network ports at runtime🌐.
  7. ENV:

    • Sets environment variables in the image⚙️.
  8. ARG:

    • Defines build-time variables that users can pass to the builder with the docker build command🛠️.
  9. VOLUME:

    • Creates a mount point and designates it as externally mountable for persisting data💾.
  10. ENTRYPOINT:

    • Configures the container to run as an executable🚀.
  11. LABEL:

    • Adds metadata to an image. Labels are key-value pairs🏷️.
  12. USER:

    • Sets the user or UID to use when running the container👤.
  13. SHELL:

    • Specifies the default shell for the shell form of commands🐚.
  14. ADD:

    • Copies files, directories, or remote URLs from the build context or a URL to the image📄.
  15. COPY --from:

    • Copies files or directories from another stage in a multi-stage build🔄.
  16. RUN --mount:

    • Allows the use of the new mount type for better security and performance🛠️.

These commands are used to define the steps and configuration for building a Docker image in a Dockerfile.

Docker Image🖼️

A Docker image is a lightweight, standalone, executable package that includes everything needed to run a piece of software, including the code, a runtime, libraries, environment variables, and system tools. Docker images are used to create and run containers, which are instances of the Docker image.🐳

Key concepts related to Docker images:

  1. Layers🧱: Docker images are built using a layered file system. Each instruction in the Dockerfile adds a new layer to the image. Layers are cached, making subsequent builds faster if the Dockerfile hasn't changed.

  2. Dockerfile📄: A Dockerfile is a text file that contains instructions for building a Docker image. It specifies a base image, sets up the environment, copies files into the image, and defines how the application should run.

  3. Registry🏰: Docker images can be stored and shared through registries. Docker Hub is a popular public registry, but organizations often use private registries for proprietary or sensitive images.

  4. Base Image🏞️: The base image is the starting point for an image. It provides a minimal operating system and environment necessary for the application to run. Common base images include Alpine Linux, Ubuntu, and others.

  5. Tag🏷️: Images can have tags to identify different versions or configurations. For example, an image might have tags like latest, v1.0, or development.

  6. Repository🗄️: A repository is a collection of related Docker images with the same name, organized by different tags. For example, the repository nginx might have tags for various versions.

Docker Container🚢

A Docker container is like a lightweight, portable box that holds everything your software needs to run.🚢🐳

  1. 🧰What's Inside?

    • It contains your application's code, its tools, libraries, and settings — everything it needs to work.
  2. 🚀Why Containers?

    • Containers help keep things tidy and separate. They run independently, so one doesn't mess with another. They're like little software islands.
  3. 🔧How Does it Work?

    • You make a container from a Docker image. An image is like a recipe that tells Docker what to put in the container.
  4. 🏁Starting a Container:

    • You use a command like docker run to start a container from an image. It's like saying, "Hey Docker, make me a box with my stuff inside and start it up!"
  5. 🌐Portability:

    • Containers are super portable. You can move them anywhere that has Docker, and they'll run the same way. Like a gift that works the same no matter where you open it.
  6. 💡Resource Efficiency:

    • Containers use resources efficiently because they share the good stuff with the computer they're on. They're like guests at a buffet who don't need their own kitchen.
  7. 🔄Managing Lifecycle:

    • You can tell a container to start, stop, or do other things using Docker commands. It's like telling it to wake up, take a break, or go to sleep.
  8. 🖼️Big Picture:

    • Think of containers as smart, portable boxes that make your software easy to move, efficient to run, and simple to manage.

📦Docker containers are a way to package and run software that's neat, tidy, and easy to carry around. They've become really popular in building and running modern applications.

Relation between Dockerfile, Docker Image, Docker Container

Dockerfile -> docker image -> docker container

The Dockerfile serves as the 📝blueprint or set of instructions for building a Docker image. The Docker image, once built, becomes a 📦standalone package that contains the application and its dependencies. When you run a Docker container, it is an instance of a specific Docker image. Multiple containers can be created from the same image, each running independently of the others.

The process usually goes like this: You write a Dockerfile to describe how your application should be set up. Then, you use that Dockerfile to build a Docker image. Finally, you run instances of Docker containers based on that image. The Docker image is like a 📸snapshot of your application, and the Docker container is the running instance of that snapshot.

Benefits of using Docker

  1. Consistency:

    • Docker makes sure that your application runs the same way everywhere, from your computer to the server, avoiding the "it works on my machine" problem.🔄
  2. Isolation and Portability:

    • Docker puts your application and all its needed stuff in a neat box called a container. This container can run the same way on any computer, making it easy to move around.📦
  3. Saves Resources:

    • Docker containers use resources more efficiently, allowing you to run more of them on the same computer.🚀
  4. Quick Deployment:

    • With Docker, you can quickly and easily send your application to any computer. Just package it in a container, ship it to a server, and run it – no complex setup needed.📤
  5. Easy Version Control:

    • Docker helps you keep track of different versions of your application. It's like taking snapshots, so you can go back to a specific version if needed.📸
  6. Good for Small Services:

    • Docker works well for breaking down big applications into smaller, manageable parts. Each part can run in its own container, making things easier to handle.🧩
  7. Automated Testing:

    • Docker makes it easy to test your application automatically. You can set it up to test your code as you develop, catching issues early.🤖
  8. Sharing Made Simple:

    • Docker lets you easily share your application with others. You can package it into an image and share that image so others can run your app just like you do.🤝
  9. Easy to Roll Back:

    • If a new update causes problems, Docker makes it simple to go back to the previous version. It's like an undo button for your application.↩️⏪
  10. Safe and Secure:

    • Docker keeps your application safe by keeping it isolated. It's like putting your app in a protective bubble, so it can't mess with other stuff on the computer.🔒
  11. Easy to Grow:

    • Docker helps you scale your application up or down based on demand. If you need more power, Docker makes it easy to add more containers.📈

Steps:

Create EC2 Instance on your AWS account

Install docker

  1. sudo apt-get update

  2. install apt-get docker.io

Add user to docker group, so that not required to use sudo for every command

  1. sudo usermod -aG docker $USER

Reboot Instance after adding user

  1. sudo reboot

Clone code from github repository for requirements

Tasks

  1. Create a Dockerfile for a simple web application (e.g. a Node.js or Python app)

  2. Build the image using the Dockerfile and run the container

  3. Verify that the application is working as expected by accessing it in a web browser

  4. Push the image to a public or private repository