Day 16 : Docker for DevOps Engineers

Day 16 : Docker for DevOps Engineers

What is docker?

Docker is an open-source platform that enables developers to build, ship, and run applications in lightweight, portable, and isolated environments called containers. Containers package an application along with all its dependencies (libraries, binaries, and configuration files), ensuring it runs consistently across different computing environments.

Key Features of Docker

  1. Containerization: Encapsulates applications and their dependencies into containers, which are isolated and portable.

  2. Portability: Containers can run on any system that supports Docker, ensuring a consistent environment across development, testing, and production.

  3. Resource Efficiency: Containers share the host operating system kernel, making them more lightweight and faster than virtual machines.

  4. Scalability: Docker makes it easy to scale applications horizontally by running multiple containers.

  5. Version Control: Using Docker images, you can version and roll back application updates.


Why Use Docker?

  • Consistency: Ensures applications work the same way in development, staging, and production environments.

  • Speed: Containers start in seconds, allowing rapid development and testing cycles.

  • Isolation: Containers run independently, preventing conflicts between applications.

  • Resource Optimization: Containers use fewer resources compared to virtual machines since they don’t require a full operating system.

  • Ecosystem: Docker provides a vast library of pre-built images via Docker Hub.


How Docker Works

  1. Docker Images:

    • A read-only template containing the application and its dependencies.

    • Built using a Dockerfile, which specifies the steps to assemble the image.

  2. Docker Containers:

    • A running instance of a Docker image.

    • Isolated environments where the application runs.

  3. Docker Engine:

    • The runtime environment that manages containers.
  4. Docker Hub:

    • A cloud-based registry for sharing Docker images.

Use Cases of Docker

  1. Microservices Architecture: Simplifies building and deploying services independently.

  2. Continuous Integration/Continuous Deployment (CI/CD): Automates testing, building, and deployment pipelines.

  3. Development Environment: Quickly set up consistent environments for developers.

  4. Application Modernization: Migrate legacy apps into containers for better performance.

  5. Testing and Debugging: Test applications in isolated environments without affecting the host system.

Pre-requisite to complete the below tasks

Install docker in the system

  1. Update the existing package list

    sudo yum update -y

  2. Install required dependencies

    sudo yum install -y yum-utils device-mapper-persistent-data lvm2

  3. Add docker official repository

    sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo

  4. Install docker

    sudo yum install -y docker-ce docker-ce-cli containerd.io

  5. Start and Enable Docker

    sudo systemctl start docker

    sudo systemctl enable docker

  6. Verify Installation

    docker --version

Post Installation Step

Allow Non-Root User to Use Docker : This is required to run docker as non-root user

sudo usermod -a -G docker $user

Reboot your system and log in again, go to /etc/group and check whether your user is added to docker or not

Tasks

  1. Use the docker run command to start a new container and interact with it through the command line

  2. Use the docker inspect command to view detailed information about a container or image

  3. Use the docker port command to list the port mappings for a container

    command syntax : docker port container-name/container id

    The hello-world container does not expose any ports. This is why even if the docker port command runs successfully, it won't return any output. The hello-world container simply runs a short-lived command and exits without requiring port mapping

  4. Use the docker stats command to view resource usage statistics for one or more containers

  5. Use the docker top command to view the processes running inside a container

    command syntax : docker top container id/container name

  6. Use the docker save command to save an image to a tar archive

  7. Use the docker load command to load an image from a tar archive