Day 21 : Docker Important interview Questions

Day 21 : Docker Important interview Questions

  • What is the difference between a Docker Image, Container and Engine?

    Docker Image: Docker Image is an executable package of software that includes everything needed to run an application. This image informs how a container should instantiate, determining which software components will run and how.

    A docker is an immutable file, which is a snapshot of a container. We create an image with a build command.

    Docker container: Docker container is simply an instance of a Docker image.

    When we use the run command, an image will produce a container.

    A Docker container is a virtual environment that bundles application code with all the dependencies required to run the application. The application runs quickly and reliably from one computing environment to another.

    Docker engine: Docker Engine is an open-source containerization technology for building and containerizing your applications. Docker Engine acts as a client-server application.

  • What is the Difference between the Docker command COPY vs ADD?

    COPY command: Copies files or directories from the host system into the container. It's simple and more straightforward.

    ADD command: Similar to COPY, but it also supports automatic extraction of compressed files and remote URL downloading. Use COPY for copying local files and ADD when you need extra functionality.

  • What is the Difference between the Docker command CMD vs RUN?

    RUN command: Executes a command during the image build process, typically used for installing packages, setting up configurations, and preparing the environment.

    CMD command: Specifies the default command to run when a container starts from an image. It's used to define the primary purpose of the container.

  • How Will you reduce the size of the Docker image?

    1. Change our Dockerfile to use smaller base images.

    2. Use a multistage build to exclude unnecessary artifacts from earlier stages in the final image.

  • Why and when to use Docker?

    Docker is used to create, deploy, and run applications in isolated environments. It provides consistency across different environments (development, testing, production) and simplifies deployment. Docker is valuable when you want to ensure application portability and scalability, maintain a consistent environment throughout the application lifecycle.

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

    Docker Daemon : The Docker Daemon is the core component of Docker, responsible for building, running, and managing containers. It communicates with other components through the Docker API.

    Docker CLI: The Docker CLI is a command-line interface for interacting with the Docker Daemon. Users can use the CLI to build, run, and manage containers.

  • Explain the terminology: Docker Compose, Docker File, Docker Image, Docker Container?

    • Docker Compose: A tool for defining and running multi-container Docker applications.

    • Docker File: A script that contains instructions to build a Docker image.

    • Docker Image: A lightweight, standalone, and executable software package.

    • Docker Container: An isolated runtime instance of an image

  • In what real scenarios have you used Docker?

    • Deploying microservices applications.

    • Setting up development environments.

    • Continuous integration and continuous deployment (CI/CD) pipelines.

    • Isolating and managing dependencies for various applications.

    • Scaling applications quickly in response to demand.

  • Docker vs Hypervisor?

    • Docker: Uses containerization to provide lightweight, isolated environments for applications, sharing the host OS kernel. It's more efficient in terms of resource usage and faster to start compared to traditional virtualization.

    • Hypervisor: Creates multiple virtual machines (VMs), each with its own guest OS and separate kernel. This approach is heavier in terms of resource usage and slower to start.

  • What are the advantages and disadvantages of using docker?

    Advantages of using Docker:

    1. Portability: Docker containers can run on any system that supports the Docker engine, making it easy to move applications between environments.

    2. Isolation: Docker containers provide a level of isolation between applications, which can help prevent conflicts between dependencies and reduce the risk of security vulnerabilities.

    3. Resource efficiency: Docker containers are lightweight and consume fewer resources than virtual machines, which can help reduce costs and improve performance.

    4. Scalability: Docker makes it easy to scale applications by adding or removing containers as needed.

    5. Automation: Docker allows you to automate the process of building, testing, and deploying applications, which can help to improve the efficiency of the software development life cycle.

    6. Microservices: Docker helps in implementing a microservices architecture, allowing you to break down a monolithic application into smaller, loosely-coupled services.

    Disadvantages of using Docker:

    1. Complexity: Setting up and managing a Docker environment can be complex, especially for large, complex applications.

    2. Security: Docker containers share the host’s kernel, which can make it easier for attackers to compromise the host and gain access to the containers.

    3. Volume management: Docker does not provide built-in volume management, which can make it difficult to manage data volumes across containers.

    4. Limited Windows support: Windows support for Docker is not as robust as Linux support, which can make it more difficult to run Windows-based applications in a container.

    5. Networking: Docker networking can be complex and may require additional configuration to work properly.

  • What is a Docker namespace?

    Docker uses a technology called namespaces to provide the isolated workspace called the container. When you run a container, Docker creates a set of namespaces for that container. These namespaces provide a layer of isolation.

  • What is a Docker registry?

    A Docker registry is a repository for Docker images. It's a central location where you can store, share, and manage Docker images. Docker Hub is a popular public registry, and you can set up private registries as well.

  • What is an entry point?

    An entry point in Docker is the command that's executed when a container starts. It's specified in the Dockerfile and can be overridden at runtime. It's often used to set the default process for the container.

  • How to implement CI/CD in Docker?

    Integrate Docker into your CI/CD pipeline by:

    • Using Docker images as build environments.

    • Running tests and validations in Docker containers.

    • Building Docker images for different stages (e.g., development, testing, production).

    • Pushing images to a Docker registry.

    • Orchestrating deployments using tools like Kubernetes or Docker Swarm.

  • Will data on the container be lost when the docker container exits?

    Data in a container is generally stored within the container's writable layer. When a container exits, data in the writable layer is lost unless it's explicitly saved outside the container (e.g., in a volume or mounted directory).

  • What is a Docker swarm?

    Docker Swarm is a container orchestration platform that allows you to create and manage a cluster of Docker nodes. It provides features for deploying, scaling, and managing containerized applications across a cluster of machines.

  • 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 <container_name> <image_name>

    • command to export a docker:

      docker export <container_id> > exported-container.tar

    • command to import an already existing docker image:

      docker import exported-container.tar

    • commands to delete a container:

      docker rm <container_id>

    • command to remove all stopped containers, unused networks, build caches, and dangling images?

      docker system prune -a

  • What are the common Docker practices to reduce the size of Docker Images?

    • Use smaller base images.

    • Minimize layers by combining commands.

    • Remove unnecessary files after installation.

    • Optimize dependencies.

    • Use multi-stage builds.

    • Compress images with efficient algorithms.

    • Avoid installing unnecessary packages and libraries.

    • Keep the number of layers to a minimum.