Docker Important interview Questions.

Docker Important interview Questions.

#90 Days of DevOps Challenge - Day 21

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

  • Docker Image:- A Docker image is a read-only template that contains a set of instructions for creating a container that can run on the Docker platform. It provides a convenient way to package up applications and preconfigured server environments, which you can use for your own private use or share publicly with other Docker users.

  • Container:- A Docker container image is a lightweight, standalone, executable package of software that includes everything needed to run an application: code, runtime, system tools, system libraries and settings.

  • Engine:- Docker Engine is an open-source containerization technology for building and containerizing your applications. Docker Engine acts as a client-server application with: A server with a long-running daemon process dockerd . APIs which specify interfaces that programs can use to talk to and instruct the Docker daemon.

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

COPYADD
COPY is a docker file command that copies files from a local source location to a destination in the Docker container.ADD command is used to copy files/directories into a Docker image.
Syntax: COPY <src> <dest>Syntax: ADD source destination
It only has only one assigned function.It can also copy files from a URL.
Its role is to duplicate files/directories in a specified location in their existing format.ADD command is used to download an external file and copy it to the wanted destination.
If we want to avoid backward compatibility, we should use the COPY command.ADD command is less usable than the COPY command.

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

RUN:-The RUN command always gets executed in a new layer. It allows you to install packages and applications on top of an existing image layer and creates a new layer on top of it.

Ex:- RUN apt-get install docker.io -y

CMD:-Using a CMD command, you will be able to set a default command. This will be executed if you run a particular container without specifying some command. In case you specify a command while running a docker container, the default one will be ignored. Note that if you specify more than one CMD instruction in your docker file, only the last one will be executed.

Ex:- CMD echo “Saikat”

How Will you reduce the size of the Docker image?

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

  1. Using distroless/minimal base images:- Your first focus should be on choosing the right base image with a minimal OS footprint. One such example is alpine base images. Alpine images can be as small as 5.59MB. It’s not just small; it’s very secure as well.

  2. Multistage builds:- The multistage build pattern is evolved from the concept of builder pattern where we use different Dockerfiles for building and packaging the application code. Even though this pattern helps reduce the image size, it puts little overhead when it comes to building pipelines. In multistage build, we get similar advantages as the builder pattern. We use intermediate images (build stages) to compile code, install dependencies, and package files in this approach. The idea behind this is to eliminate unwanted layers in the image.

  3. Minimizing the number of layers:- Use only the required layers otherwise image size will increase.

  4. Understanding caching:- Often, the same image has to be rebuilt again & again with slight code modifications. Docker helps in such cases by storing the cache of each layer of a build, hoping that it might be useful in the future. Due to this concept, it’s recommended to add the lines which are used for installing dependencies & packages earlier inside the Dockerfile – before the COPY commands. The reason behind this is that docker would be able to cache the image with the required dependencies, and this cache can then be used in the following builds when the code gets modified.

  5. Using Dockerignore:- As a rule, only the necessary files need to be copied over the docker image.Docker can ignore the files present in the working directory if configured in the .dockerignore file.This feature should be kept in mind while optimizing the docker image.

  6. Keeping application data elsewhere:-Storing application data in the image will unnecessarily increase the size of the images. It’s highly recommended to use the volume feature of the container runtimes to keep the image separate from the data.

Why and when to use Docker?

Good reasons to use Docker

  1. The development team is not set in stone

  2. The software runs in different environments

  3. Software grows and consists of many bits and pieces

  4. Want your software to be scalable and handle more users

  5. Hosting infrastructure may change in the future and you don’t want to be locked

  6. Want to test a new technology

  7. Cost-Effective

  8. In-Built Version Control System

  9. Security

  10. Better Portability

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

  1. Docker Daemon:- Docker daemon runs on the Host O.S and It is responsible for running containers and managing docker services. Docker daemons can communicate with other daemons.

  2. Docker Client:- Docker users can interact with the docker daemon through a client and the Docker client uses commands and Rest API to communicate with the docker daemon. Docker clients can communicate with more than one daemon.

  3. Docker Host:- Docker Host is used to provide an environment to execute and run applications. It contains the docker daemon, images, containers, networks, and storage.

  4. Docker Hub/Registry:- Docker registry manages and stores the docker images. and there are 2 types of Registries are there 1> Public Registry and 2> Private Registry

  5. 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 a program.

  6. Docker Container:- 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.

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

  • Docker Compose:- Docker Compose is a tool that assists in defining and sharing multi-container applications. By using Compose, we can define the services in a YAML file, as well as spin them up and tear them down with one single command.

  • Dockerfile:-Dockerfile is a text document containing all the commands the user requires to call on the command line to assemble an image. With the help of a Dockerfile, users can create an automated build that executes several command-line instructions in succession

  • 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 a program.

  • Docker Container:- 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.

In what real scenarios have you used Docker?

  1. Microservices: Docker is often used to deploy microservices-based architectures, where each service runs in its own container. This allows for easy scaling, deployment, and management of individual services.

  2. DevOps and Continuous Integration/Deployment (CI/CD): Docker can be used to create consistent development, testing, and production environments, which makes it easier to manage dependencies and ensure that code is deployed consistently across different environments.

  3. Cloud and Multi-Cloud Deployment: Docker containers can run on any infrastructure and can be easily moved between different cloud providers, making it an ideal solution for multi-cloud deployments.

  4. Virtualization: Docker can be used as an alternative to traditional virtualization, allowing multiple isolated environments to run on a single host.

  5. Testing and QA: Docker can be used to create isolated testing environments that can be easily recreated and destroyed, which helps to ensure that tests are repeatable and reliable.

  6. Big Data and Analytics: Docker can be used to deploy big data and analytics tools, such as Hadoop and Spark, in a consistent and scalable way.

Docker vs Hypervisor?

DockerHypervisor
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.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.
Resource requirement is low as containers are working on the same operating system and this makes the system share resources within the containers.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.
Boot time is low for dockers as all the containers work on the same machine.Boot time is high for hypervisors as different operating systems are used.

What are the advantages and disadvantages of using docker?

Docker Advantages:-

  • Rapid Deployment

  • No pre-allocation of RAM

  • CI Efficiency , Build App only once

  • Less Cost and light weight

  • It can run on the Physical H/W ,VM

  • You can reuse the image

  • Less time to create container (VM)

  • Version Controlling

  • Portability

  • Isolation

Docker Disadvantages:-

  • Docker is not a good solution for application that requires a rich GUI.

  • Difficult to manage a large number of containers.

  • Docker does not provide cross-platform compatibility means if an application is designed to run in a docker container on windows, then it can’t run on Linux or vice-versa.

  • Docker is suitable when the development O.S and testing O.S are the same, if the O.S is different, we should use Virtual Machine.

  • No solution for Data Recovery & Backup.

What is a Docker namespace?

Namespaces are one of a feature in the Linux Kernel and a fundamental aspect of containers on Linux. On the other hand, namespaces provide a layer of isolation. Docker uses namespaces of various kinds to provide the isolation that containers need to remain portable and refrain from affecting the remainder of the host system. Each aspect of a container runs in a separate namespace and its access is limited to that namespace.

What is a Docker registry?

A Docker registry is organized into Docker repositories, where a repository holds all the versions of a specific image. The registry allows Docker users to pull images locally, as well as push new images to the registry.

What is an entry point?

ENTRYPOINT is one of the many instructions you can write in a docker file. The ENTRYPOINT instruction is used to configure the executables that will always run after the container is initiated. For example, you can mention a script to run as soon as the container is started

How to implement CI/CD in Docker?

To implement CI/CD (Continuous Integration/Continuous Deployment) in Docker, you can follow these steps:

  1. Set up a version control system (e.g., Git) to manage your codebase.

  2. Configure a CI/CD tool (e.g., Jenkins, GitLab CI/CD, CircleCI) to monitor the repository for changes.

  3. Create a Dockerfile to define the image build process and environment.

  4. Use the CI/CD tool to build the Docker image, run tests, and push the image to a Docker registry.

  5. Configure deployment pipelines to deploy the Docker image to your target environments (e.g., staging, production) using container orchestration tools like Kubernetes or Docker Compose.

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

By default, data inside a Docker container will be lost when the container exits. Docker containers are designed to be stateless and ephemeral. However, you can use Docker volumes or bind mounts to persist data outside the container, ensuring that it is retained even when the container is stopped or removed.

What is a Docker swarm?

  • Swarm is Docker’s built-in container orchestrator solution, its main purpose is to manage containers in a cluster, i.e. a set of connected machines that work together.

  • When a new machine joins the cluster, it becomes a node in that swarm.

  • Using Docker Swarm we can achieve high availability, load balancing and Decentralized access to our applications.

  • Swarm comes built into the Docker Engine, you don’t need to install anything to get started.

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 save -o <output.tar> <image_name>
  • command to import an already existing docker image
docker load -i <input.tar>
  • commands to delete a container
docker rm <conatiner_name>
  • command to remove all stopped containers, unused networks, build caches, and dangling images?
docker rm -f $(sudo docker ps -a -q)
docker system prune -a

Devops#devops,#90daysofDevOps

Thank you for reading!! I hope you find this article helpful!!

if any queries or corrections to be done to this blog please let me know.

Happy Learning!!

Saikat Mukherjee

Did you find this article valuable?

Support Saikat Mukherjee's blog by becoming a sponsor. Any amount is appreciated!