Day-21 Docker Important Interview Questions !

Day-21 Docker Important Interview Questions !

#docker#devops

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

    Docker images:

    Docker images are the read-only binary templates used to create docker containers or a single file with all dependencies and configurations required to run the program.

    Ways to create an images

    1. Take an image from the docker hub

    2. Create an image from the docker file

    3. create an image from existing docker containers.

Docker containers:

  • The container holds the entire package that is needed to run the application or

  • In other words, we can say that the image is a template and the container is a copy of that template.

  • container is like virtualization when they run on the docker engine.

  • Images become containers when they run on the docker engine.

Docker engine:

The Docker Engine (DE) is installed on the host machine and represents the core of the Docker system. It is a lightweight runtime system and the underlying client-server technology that creates and manages containers. Server - the Docker daemon (dockerd), which is responsible for creating and managing containers.

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

COPY: Copy files from the local system (docker VM)

we need to provide a source and destination

ADD: Similar to copy, it provides a feature to download files from the internet also we extract the file from the docker image

An additional feature is that it copies compressed files, automatically extracting the content to the given destination. This feature only applies to locally stored compressed files/directories.

Unlike its closely related ADD command, COPY only has only one assigned function. Its role is to duplicate files/directories in a specified location in their existing format. This means that it doesn’t deal with extracting a compressed file, but rather copies it as-is

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

CMD: Execute commands but during container creation.

RUN: To execute commands, it will create a layer in the image.

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

  2. The following are the methods by which we can achieve docker image optimization.

    1. Using distroless/minimal base images

    2. Multistage builds

    3. Minimizing the number of layers

    4. Understanding caching

    5. Using Dockerignore

    6. Keeping application data elsewhere

  3. Why and when to use Docker?

    why? - Docker is used to containerise an application that can be run in any system with any configuration at any time.

    when? - let us consider two developers who designed a build and one of them transfers the new version of applications to their teammate but now it does not work. This situation came due to differences in the dependencies of their system. The best way to overcome the situation is by using containers which can be done using docker.

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

    • Docker uses a client-server architecture to carry out all of its operations. The three major components that become an integral part of the Docker architecture are - The Docker Daemon (Server) REST API (Docker Engine) Docker CLI (Client) These components work together to allow communication between the client and the server.

Docker daemon (dockerd)*: The Docker daemon is the core component of the* Docker architecture. It runs inside the docker engine in the background and manages the container lifecycle. The Docker daemon is responsible for creating, starting, stopping, and deleting containers, as well as managing the storage and networking for those containers (handling docker client requests).

Docker client: The Docker client is a command-line tool that allows users to interact with the Docker daemon. Users can issue commands to the Docker client, which then communicates with the Docker daemon to perform the requested actions.

Containerd: The containerd is a container runtime for managing container lifecycle operations, such as creating, starting, stopping, and deleting containers. Docker uses containerd as its default container runtime to manage the lifecycle of containers, providing low-level functionality for container management.

Runc. runc is a portable implementation of the Open Container Initiative (OCI) runtime specification. It is used by containerd to run containers (Only creates containers ).

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

Docker Compose: Docker Compose is a tool that was developed to help define and share multi-container applications.

With Compose, we can create a YAML file to define the services and with a single command, can spin everything up or tear it all down.

Docker Image: A docker image is an executable package of software that includes everything needed to run an application. docker image is used to build the container

  1. All the code, configuration settings, environmental variables, libraries, and runtime are included in a Docker image.

  2. Docker images are platform-independent.

Lightbox

Dockerfile:

Dockerfile is a text file it contains some set of instructions.

Automation of docker image creation.

Docker Container:

  • It is a runtime instance of an image

  • Allows developers to package applications with all parts needed such as libraries

  • the container is like virtualization when they run on the Docker engine.

  • Images become containers when they run on the docker engine.

    1. In what real scenarios have you used Docker?

      Docker is commonly used for deploying and running applications in production environments. I have used docker to containerize an application by writing a docker file including the base image and its dependencies and then using the image to build the container. I stored the images on my docker hub account so that they can be pulled on any device.

    2. Docker vs Hypervisor?

      Hypervisor

      Docker

      Hypervisors can be made to work on software and hardware where it works on the operating system or on the CPU and storage services of the system.

      Dockers work only on the software of the operating system and not on the hardware side. It takes the host kernel and works on the principle of virtualization.

      In a single system, we can use multiple operating systems with the help of Hypervisor. This makes the system to work with multiple users with different methods even for the same program. Hence the same operation is done by different operating systems. 

      Docker does not allow users to create multiple instances of operating systems in the same computer but it makes virtualization by making containers in the same system. Containers help users to work separately on different or the same applications. The same operations are carried out by containers in the system.

      More power and resources are required by the systems using hypervisors as different programs are being run on the same system with different operating systems.

      Resource requirement is low as containers are working on the same operating system and this makes the system share resources within the containers.

      Boot time is high for hypervisors as different operating systems are used. It may take some minutes to start the system and users can resume their work only after booting the machine.

      Boot time is low for dockers as all the containers work on the same machine. Users can start the system in seconds and can start working on the same machine.

      We cannot test the same application with different parameters in Hypervisor as there is no container method available. This application needs to be developed and tested in the system. If the parameters must be changed, they should be modified in the same operating system itself.

      If the same application needs to be tested in the system with different instances, we can use containers as different parameters can be given to the application in the same container and can be tested at the same time. Dockers support this method of working which is called an agile model.

      Hypervisor works with the host OS and guest OS which creates layers that run the hardware. We cannot create different instances for the same application in the system but we can control the hardware and make the system work with both OS.

      Docker does not have an OS for itself and thus it creates instances and parameters by sitting on top of the OS. This helps in modifying the instances if needed. It works solely on the host OS and does not control the hardware of the system.

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

Advantages of Docker:

  • Compatibility and Maintainability.

  • Rapid Deployment.

  • Continuous deployment and testing environment.

  • It uses less memory.

  • Simplicity and Faster Configurations.

Disadvantages of Docker:

  • Containers consume resources more efficiently than virtual machines.

  • Persistent data storage is complicated

  • It increases complexity due to an additional layer.

  1. What is a Docker namespace?

    Docker uses a technology called namespaces to provide an 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. Each aspect of a container runs in a separate namespace and its access is limited to that namespace.

  2. What is a Docker registry?

    The Docker registry manages and stores the Docker images

    there are two types of registries in the docker

    Public Registry: Public registry also called docker hub

    Private Registry: It is used to share images within the enterprise.

  3. What is an entry point?

    Similar to CMD, but has higher priority over CMD, the first commands will be executed by ENTRYPOINT only.

    1. How to implement CI/CD in Docker?

Implementing CI/CD (Continuous Integration and Continuous Deployment) in Docker involves automating the build, test, and deployment processes of Docker images and containers. Here's an overview of how to implement CI/CD in Docker:

  • Set up a source code repository: Store your application's source code in a version control system like Git.

  • Write a Dockerfile: Create a Dockerfile that defines the environment, dependencies, and configuration of your application.

  • Configure the build pipeline: Set up a CI/CD pipeline to automatically build, test, and deploy your Docker images. This can be done using tools like Jenkins, GitLab CI/CD, or Circle CI.

  • Build the Docker image: Use the Dockerfile to build a Docker image of your application.

  • Test the Docker image: Run automated tests against the Docker image to ensure that it meets the required quality standards.

  • Push the Docker image to a registry: Push the Docker image to a Docker registry, such as Docker Hub or a private registry, to make it available for deployment.

  • Deploy the Docker image: Deploy the Docker image to the production environment using tools like Kubernetes, Docker Swarm, or plain Docker.

  • Monitor and update the Docker image: Monitor the Docker container in production and update it as necessary to fix bugs or add new features.

By automating these steps, you can ensure that your Docker containers are always up-to-date and deployed in a consistent, reproducible manner.

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

There is no loss of data when any of your Docker containers exits as any of the data that your application writes to the disk to preserve it. This will be done until the container is explicitly deleted. The file system for the Docker container persists even after the Docker container is halted.

The container data will be lost if the container is not tagged to a volume.

Docker volumes are a widely used and useful tool for ensuring data persistence while working in containers. Docker volumes are file systems mounted on Docker containers to preserve data generated by the running container.

  1. What is a Docker swarm?

Docker swarm is a container orchestration tool.

Deployment and maintenance of multiple containers and services for an application manage a cluster of docker nodes.

It helps end-users in creating and deploying a cluster of Docker nodes. Each node of a Docker Swarm is a Docker daemon, and all Docker daemons interact using the Docker API. Each container within the Swarm can be deployed and accessed by nodes of the same cluster.

Benefits:

  • Deployment

  • Scaling

  • Resource allocation

  • Load balancing

  • Health monitoring

    1. What are the docker commands for the following:

view running containers

docker ps -a

command to run the container under a specific name

docker run --name 'container-name' 'image-name'

command to export a docker

docker export 'container-name' 'path-to-save-file>.tar'

command to import an already existing docker image

docker import 'path-to-docker-image' 'image-name'

commands to delete a container

docker stop <container_name/ID> && docker rm <container_name/ID>

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

docker system prune

Thank you for reading this Blog. Hope you learned something new today! If you found this blog helpful, please like, share, and follow me for more blog posts like this in the future😊

Happy Learning :)