Docker Important Interview Questions

7 min read

Cover Image for Docker Important Interview Questions

Docker is a hot topic for interviewers to ask, Here are some questions that might help.

  1. what are the difference between an Image, Container and Engine?
  • Image: An Image is lightweight and software packagethat includes all information that needed to run an application, including code, runtime, libraries, environment and dependencies.

  • Containers: A container can be defined as an isolated environment where the images were run in order to run an application. Containers provide a consistent environment and ensure that the application behave the same way across computing environment.

  • Engine: Docker engine is the core component of Docker responsible for building and running the containers. Docker engine includes Docker Daemon which runs in the background and Docker Client( DockerCLI ) used to interact with docker daemon.k

  1. What is the Difference between the Docker command COPY vs ADD?

    Functionality:

    • COPY:The copy command is used to copy files and directories from the host machine to the container's file system. It supports copying local files and directories or files from a URL.

    • ADD: The Add command performs a similar function as COPY but has some additional features. In addition to copying files and directories, it can also automatically extract tar archives and retrieve files from remote URLs.

Remote URLs:

  • COPY: The Copy command does not support retrieving files from remote URLs. It is primarily used for copying files from the local filesystem.

  • ADD: The Add command can directly retrieve files from remote URLs and add them to the container. It supports URLs such as HTTP, HTTPS, and FTP.

Caching:

  • COPY: The Copy command takes advantage of Docker's build cache. If the contents of the source file(s) haven't changed, Docker will use the cached layer, avoiding the need to copy the files again.

  • ADD: The Add command does not use the build cache for retrieving files from remote URLs. Even if the URL hasn't changed, the file will still be downloaded and added to the container during every build.

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

    • CMD sets the default command or executable for the container at runtime, whereas RUN executes commands during the image build process.

    • CMD is used to define the behavior of the container when it starts, while RUN is used for performing actions during the image build.

    • CMD can be overridden at runtime, while RUN instructions are executed during the build and affect the resulting image.

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

    • .dockerignore: Create a .dockerignore file in your project directory to exclude unnecessary files and directories from being included in the Docker build context. This helps to reduce the size of the build context and, consequently, the size of the resulting image.

    • Combine multiple RUN commands into a single command to reduce the number of image layers created.

      Use --no-install-recommends or similar flags when installing packages to avoid unnecessary dependencies.

  3. Why and when to use Docker?

    • Docker allows devops engineers to run and test the application. sometimes app which run locally might not run on the server, that's why docker provided containerize environment to run application in any system environment.
  4. Explain the Docker components and how they interact with each other.

    • The Docker client interacts with the Docker daemon through the Docker API. It sends commands and requests to the daemon for various operations such as building images, creating containers, and managing Docker resources.

    • Docker images are created and managed using the Docker daemon. Images can be built from a Dockerfile, pulled from a Docker registry, or saved and loaded as tar archives.

    • Containers are created and managed by the Docker daemon. The Docker client communicates with the daemon to start, stop, inspect, and manage containers.

    • Docker Compose uses the Docker Engine API to orchestrate the creation and management of multiple containers as defined in the docker-compose.yml file.

    • Docker Swarm utilizes the Docker Engine API to manage a swarm cluster, deploy services, and handle scaling, load balancing, and fault tolerance across the cluster.

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

    • Docker Compose:

      • Docker Compose is a tool that allows you to define and manage multi-container applications. It uses a YAML file (usually named docker-compose.yml) to specify the services, networks, and volumes required for your application's deployment.
    • Dockerfile:

      • A Dockerfile is a text file that contains a set of instructions for building a Docker image.

      • Dockerfiles define the steps to create a reproducible and self-contained image that can be run as a container.

    • Docker Image:

      • A Docker image is a lightweight, standalone, and executable software package that contains everything needed to run an application, including the code, runtime, libraries, environment variables, and dependencies.
    • Docker container:

      • container can be defined as an isolated environment where the images were run in order to run an application. Containers provide a consistent environment and ensure that the application behave the same way across computing environment.
  6. Docker vs Hypervisor?

    • Docker:

      • Containerization: Docker is primarily focused on containerization, which provides lightweight and isolated environments for running applications. Containers share the host operating system's kernel, making them more lightweight and efficient compared to traditional virtual machines (VMs).
    • hypervisor:

      • Virtual Machine Isolation: Hypervisors provide full machine virtualization, allowing multiple virtual machines (VMs) to run on a single physical host. Each VM has its own complete operating system, providing stronger isolation and security between applications.
  7. What are the advantages and disadvantages of using docker?

    • Advantages:

      • Portability: Docker containers are highly portable, allowing you to run applications consistently across different environments.

      • Efficiency and Performance: Docker's lightweight nature and shared kernel approach enable efficient utilization of system resources.

      • Continuous Integration and Deployment: Docker integrates well with continuous integration and deployment workflows.

    • Disadvantages:

      • Security Considerations: While Docker provides isolation, security vulnerabilities within containers or misconfigurations can still pose risks.
  8. What is a Docker namespace?

    • Docker namespaces are a fundamental feature of the Docker platform that provide process isolation and resource control within containers. Namespaces allow different containers to have their own isolated view of system resources, such as process IDs, network interfaces, mount points, and more.
  9. What is a Docker registry?

    • Docker Registry: Docker Registry is a storage and distribution system for Docker images. It serves as a central repository for sharing and managing Docker images. Docker Hub is a popular public Docker registry, hosting a vast collection of pre-built images. Additionally, Docker allows you to run a private registry to store and manage your own Docker images within your organization.
  10. What is an entry point?

    • In Docker, the Entery point instruction is used in a Dockerfile to specify the command that should be run when a container based on that image is started.
  11. How to implement CI/CD in Docker?

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

    • No, data remain on the docker volumes attached to docker containers.
  13. What is a Docker swarm?

    • Docker provide inbuilt orchestration tool called Docker swarm which controls and manages a cluster of docker nodes.
  14. What are the docker commands for the following:

    • view running containers

        docker ps
      

    • command to run the container under a specific name

      • in interactive mode

          docker run -it <image_name>
        
      • in detach mode

          docker run -d <image_name>
        
      • command to export a docker

          docker export CONTAINER_ID > output.tar
        
      • command to import an already existing docker image

          docker import mycontainer.tar myimage:tag
        
      • commands to delete a container

          docker rm [OPTIONS] CONTAINER [CONTAINER...]
        
          # Remove a single stopped container
          docker rm mycontainer
        
          # Remove multiple stopped containers
          docker rm container1 container2 container3
        
          # Forcefully remove a running container
          docker rm -f mycontainer
        
          # Remove a container and its associated volumes
          docker rm -v mycontainer
        
      • command to remove all stopped containers, unused networks, build caches, and dangling images?

          docker system prune -a
          #or
          docker system prune -af
        

wants to connect with me ๐Ÿ’• LinkedIn: https://www.linkedin.com/in/srisanth-seth-51b54b225/

here check out my blog on linux commands:

https://medium.com/@srisanthseth28/cheat-sheet-linux-commands-git-5179f63b1fbc

Happy learning !!๐ŸŽ‰๐Ÿ’•