Day 16 Task: Docker for DevOps Engineers.

🔹What is Docker?

Docker is an open-source containerization platform that allows you to package applications and their dependencies into isolated containers. It provides a consistent and reproducible environment, making it easier to deploy and scale applications across different environments. In DevOps, Docker is commonly used for containerization, enabling consistent and efficient deployment and ensuring consistency between development, testing, and production environments.

Key aspects of Docker include:

  1. Containerization: Docker enables you to encapsulate an application and its dependencies into a container. Containers provide a consistent runtime environment, ensuring that applications behave the same way regardless of the underlying infrastructure.

  2. Image-Based Packaging: Docker uses images to package applications. An image is a read-only template that contains everything needed to run an application, including the code, runtime, libraries, and system tools. Images can be versioned, shared, and distributed across different environments.

  3. Isolation and Portability: Containers in Docker offer process-level isolation, ensuring that applications and their dependencies are encapsulated and do not interfere with each other. Containers are portable and can run on any system that supports Docker, providing consistent behavior across different platforms.

  4. Efficiency and Resource Utilization: Docker optimizes resource utilization by sharing the host system's kernel and resources among containers. This reduces overhead and allows for efficient utilization of computing resources, enabling more applications to run on the same infrastructure.

  5. Easy Deployment and Scalability: Docker simplifies the deployment process by providing a consistent environment for applications. Applications packaged as containers can be easily deployed, scaled, and managed using container orchestration platforms like Kubernetes or Docker Swarm.

  6. Docker Hub and Registries: Docker Hub is a cloud-based registry that hosts a vast collection of pre-built Docker images. It serves as a central repository where developers can share, discover, and pull images for their applications. Additionally, you can create private Docker registries to store and distribute custom images within your organization.

Docker has revolutionized application deployment by providing a standardized and efficient approach to containerization. It offers a flexible and scalable solution for managing applications, enabling faster development cycles, improved portability, and simplified operations.

🔹Docker Installation

On Ubuntu (using apt): -Update the apt package index and install packages to allow apt to use a repository over HTTPS:

sudo apt-get update
sudo apt-get install ca-certificates curl gnupg

Add Docker’s official GPG key:

sudo install -m 0755 -d /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
sudo chmod a+r /etc/apt/keyrings/docker.gpg

Use the following command to set up the repository:

echo \
  "deb [arch="$(dpkg --print-architecture)" signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu \
  "$(. /etc/os-release && echo "$VERSION_CODENAME")" stable" | \
  sudo tee /etc/apt/sources.list.d/docker.list > /dev/null

Update the apt package index:

sudo apt-get update

Install Docker Engine, containerd, and Docker Compose.

sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin

Verify that the Docker Engine installation is successful by running the hello-world image.

sudo systemctl enable docker
sudo systemctl status docker
sudo docker run hello-world

🔹Docker Commands

  1. docker run: Create and start a new container from an image.

  2. docker ps: List all running containers.

  3. docker ps -a: List all containers, including stopped ones.

  4. docker images: List all downloaded images on the system.

  5. docker pull <image_name>: Download an image from a Docker registry.

  6. docker build: Build a new image from a Dockerfile.

  7. docker start <container_id>: Start a stopped container.

  8. docker stop <container_id>: Stop a running container.

  9. docker restart <container_id>: Restart a running container.

  10. docker exec: Run a command inside a running container.

  11. docker rm <container_id>: Remove a stopped container.

  12. docker rmi <image_id>: Remove an image.

  13. docker logs <container_id>: View the logs of a container.

  14. docker inspect <container_id>: View detailed information about a container.

  15. docker network: Manage Docker networks.

  16. docker volume: Manage Docker volumes.

  17. docker-compose: Manage multi-container applications with a YAML file.

  18. docker info: Display system-wide information about Docker.

  19. docker version: Show the Docker version information.

  20. docker pull <image_name:tag>: Download a specific image version from a Docker registry.

  21. docker push <image_name:tag>: Upload an image to a Docker registry.

  22. docker build -t <image_name:tag>: Build an image and tag it with a custom name and version.

  23. docker tag <source_image:source_tag> <target_image:target_tag>: Create a new tag for an existing image.

  24. docker rename <container_name> <new_container_name>: Rename a running or stopped container.

  25. docker cp <container_id>:<source_path> <destination_path>: Copy files/folders between a container and the host.

  26. docker pause <container_id>: Pause a running container.

  27. docker unpause <container_id>: Unpause a paused container.

  28. docker prune: Remove all stopped containers, unused networks, and dangling images.

  29. docker system prune: Remove all stopped containers, unused networks, dangling images, and build cache.

  30. docker stats: Display a live stream of container resource usage.

  31. docker events: Monitor Docker events in real-time.

  32. docker login: Log in to a Docker registry.

  33. docker logout: Log out from a Docker registry.

Tasks

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

      sudo docker run -d -p 80:80 nginx 
      sudo docker ps
    

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

      sudo docker inspect CONTAINER_ID
    

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

      sudo docker port CONTAINER_ID
    

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

      sudo docker stats CONTAINER_ID
    
  • Use the docker top command to view the processes running inside a container.

      sudo docker top CONTAINER_ID
    

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

      docker save -o OUTPUT_FILE IMAGE_NAME:TAG
    
  • Use the docker load command to load an image from a tar archive.

      docker load -i INPUT_FILE
    

These tasks involve simple operations that can be used to manage images and containers.