Day 21 - Docker interview questions

Day 21 - Docker interview questions

  1. What is Dockerfile

    A Dockerfile is a text file that contains a set of instructions used to build a Docker image

  2. What is the Difference between an Image, Container and Engine?

    Docker images are the building blocks that contain all the necessary components to run an application, Docker containers are the running instances of Docker images that provide isolated runtime environments, and Docker Engine is the core software responsible for managing and executing containers based on Docker images.

  3. What is the Difference between the Docker command CMD vs RUN?

    RUN command is used during the image build process to execute commands in the image's file system, while the CMD command specifies the default command to be executed when a container is run from the Docker image.

  4. How Will you reduce the size of the Docker image?

    Use a smaller base image : Alpine linux

    Minimize layers: combining multiple commands into a single RUN

    Clean up package caches: use apt-get clean or apt-get autoclean to remove package caches.

    Remove unnecessary files : use .dockerignore

    Optimize dependencies:

  5. Why and when to use Docker?

    Docker is beneficial when you want to achieve application portability, enhance isolation and resource efficiency, enable scalability, automate CI/CD processes, adopt a microservices architecture, and facilitate collaboration and reproducibility. It is especially useful in scenarios where consistent and reliable deployment across different environments is required.

  6. Explain the Docker components and how they interact with each other.

    1. Docker Engine: Docker Engine is the core component of Docker. It provides the runtime environment for containers and manages container operations. It consists of three main parts:

      • Docker daemon (dockerd): The Docker daemon runs as a background service on the host machine. It is responsible for building, running, and managing containers. The daemon listens for Docker API requests and performs actions accordingly.

      • Docker CLI (Command-Line Interface): The Docker CLI is a command-line tool used to interact with the Docker daemon. It provides a user-friendly interface to manage Docker resources, such as images, containers, networks, and volumes. The CLI sends commands to the Docker daemon via the Docker API.

      • Container runtime: The container runtime is responsible for executing and managing containers' lifecycle. Docker typically uses containerd as the default container runtime, although other runtimes like runc can also be used. The container runtime interacts with the Docker daemon to start, stop, and manage container processes.

    2. Docker Images: Docker images are the building blocks of containers. An image is a lightweight, standalone, and executable package that includes everything needed to run an application, such as the code, runtime, libraries, and dependencies. Images are built based on instructions specified in a Dockerfile. They are stored in a registry, which can be either Docker Hub (public registry) or a private registry.

    3. Docker Container: A Docker container is a running instance of a Docker image. It is an isolated and lightweight runtime environment that encapsulates an application and its dependencies. Containers provide process isolation, have their own filesystem, network interfaces, and resource allocations. Multiple containers can be created from the same Docker image, and each container operates independently.

    4. Docker Registry: Docker Registry is a central repository that stores Docker images. It can be a public registry like Docker Hub, or you can set up a private registry within your organization. Docker images are pulled from a registry to run containers, and new images can be pushed to a registry after building.

    5. Docker Compose: Docker Compose is a tool for defining and running multi-container applications. It allows you to describe your application's services, networks, and volumes in a YAML file. With Docker Compose, you can easily orchestrate multiple containers, define their configurations, and manage their interconnections.

    6. Container Orchestration: Docker can be used in conjunction with container orchestration platforms like Docker Swarm or Kubernetes. These platforms provide advanced features for managing and scaling containerized applications across multiple hosts or clusters. They handle tasks such as load balancing, automatic container distribution, service discovery, scaling, and high availability.

The typical flow of interaction between Docker components involves using the Docker CLI to manage Docker resources. The CLI communicates with the Docker daemon via the Docker API. Docker images are pulled from a registry or built locally using the Docker daemon. Containers are created and managed by the Docker daemon based on the instructions provided in the Docker image and user commands.

Overall, Docker's components work together to simplify the process of building, deploying, and managing containerized applications, providing a consistent and efficient way to package and run applications across different environments.

  1. Explain the terminology: Docker Compose, Docker File, Docker Image, Docker Container?

    Docker Compose is used to define and manage multi-container applications, Dockerfile is a script-like file to build Docker images, Docker images are standalone packages with all dependencies, and Docker containers are runtime instances of Docker images that can be started, stopped, and managed independently.

  2. Docker vs Hypervisor?

    Docker focuses on containerization and running applications efficiently, while hypervisors provide full hardware virtualization and allow running multiple operating systems on a single physical machine. Docker is more lightweight and suitable for running applications, while hypervisors are more suitable for running complete virtual machines with different operating systems.

  3. What are the advantages and disadvantages of using docker?

    Docker offers numerous advantages in terms of portability, scalability, efficiency, and reproducibility. However, it's important to consider the learning curve, complexities for certain use cases, handling persistence, and ensuring proper security measures when using Docker.

  4. What is a Docker namespace?

    namespaces ensure that processes running inside containers are contained and isolated from the rest of the system, providing security and resource isolation.

  5. What is a Docker registry?

    Docker registries play a crucial role in the Docker ecosystem by providing a central location for storing, sharing, and distributing Docker images. They enable collaboration, version control, and efficient deployment of containerized applications across different environments.

  6. What is an entry point?

    The entry point is critical because it determines where the program's execution starts and defines the initial actions to be performed. It sets the stage for the rest of the program's logic and functionality.

  7. How to implement CI/CD in Docker?

    version control : Git and Github

    Build automation : CI/CD jenkins

    Build docker images

    test automation

    Artifact repo: To store docker images docker hub and ECR

    Continuous deployment : Docker Compose, Kubernetes, or AWS ECS to manage the deployment and orchestration of containers.

    roll back and recovery:

    Infrastructure Automation: Utilize infrastructure automation tools like Terraform, Ansible, or CloudFormation to automate the provisioning and configuration of the infrastructure

    Continuous Monitoring: Implement monitoring and logging solutions to gain insights into your application's performance and detect any issues. Tools like Prometheus, Grafana, ELK stack (Elasticsearch, Logstash, Kibana),

    notification and reporting

  8. Will data on the container be lost when the docker container exits?

    It's important to plan your data storage strategy based on the requirements of your application and choose the appropriate approach (volumes or bind mounts) to ensure data persistence and prevent data loss.

  9. What is a Docker swarm?

    Docker Swarm simplifies the deployment and management of containerized applications in a cluster of Docker nodes. It provides features for orchestration, scalability, load balancing, and service management, allowing you to leverage the full potential of Docker containers in a distributed environment.

  10. What are the docker commands for the following:

    • view running containers

    •   docker ps
      
    • command to run the container under a specific name

    •   docker run --name my-container nginx
      
    • command to import an already existing docker image

    • The syntax for the docker import command is as follows:

        phpCopy codedocker import <path_to_image_tarball> <repository:tag>
      

      Replace <path_to_image_tarball> with the path to the tarball file containing the Docker image you want to import. The <repository:tag> parameter represents the repository and tag you want to assign to the imported image.

      Here's an example command:

        arduinoCopy codedocker import myimage.tar my-repo/my-image:latest
      

      In this example, myimage.tar is the path to the tarball file, and my-repo/my-image:latest is the repository and tag assigned to the imported image.

      After running the docker import command, Docker will import the image and assign it the specified repository and tag. You can then use this imported image to run containers or push it to a Docker registry for further use or distribution.

    • commands to delete a container

    •   docker rm < container ID>
      
    • command to remove all stopped containers, unused networks, build caches, and dangling images?

    • To remove all stopped containers, unused networks, build caches, and dangling images in Docker, you can use the following command:

        docker system prune -a
      
    docker system prune -a -f