DAY 21 : Docker Important interview Questions.
Tags: devops, docker, aws, 90daysofdevops
Published: 2024-07-01 16:12:53

**What is the Difference between an Image, Container and Engine?**
Image: Docker image is a read-only package containing necessary dependencies, and commands that can be executed to run an application. It is the prerequisite for creating a container.

Container: It is the ability to package the application that has its own network, mounts and OS that can run anywhere, anytime and as many times as you want in any system.

Engine: It is an open-source containerization for building and containerization of your application. It enables you to separate your applications from infrastructure which helps in faster delivery of the software.



**What is the Difference between the Docker command COPY vs ADD?**
COPY and ADD are both Dockerfile instructions that serve a similar purpose. They let you copy files from a specific location into a Docker image.
**COPY takes in a source and destination. It only lets you copy in a local or directory from your host (the machine-building the Docker image) into the Docker image itself.

ADD does that same but in addition, it also supports 2 other sources.

A URL instead of a local file/directory.

Extract tar from the source directory into the destination.

Syntax: COPY <src> <dest>

Syntax: ADD source destination

**What is the Difference between the Docker command CMD vs RUN?**
RUN :- A Dockerfile can have many RUN steps that layer on top of one another to build the image.

CMD :- It is the command which the container executes by default when you launch the built image. A Dockerfile can only have one CMD. The CMD can be overridden when starting a container with docker run $image $other_command.

**How Will you reduce the size of the Docker image?**
yum update is strictly avoided

Using smaller images

Installed the only required packages within the image

Clubbed all the RUN instructions in a single layer.

MULTI-STAGE BUILDS IN DOCKER.

Use Docker Squash to reduce the size of the final image. This is effective if your image has multiple layers created using RUN clause. The Squash attempts to create a single layer out of all the layers and thus reduced the overall size.

**Why and when to use Docker?**
Docker is an open platform for developing, shipping, and running applications. Docker enables you to separate your applications from your infrastructure so you can deliver software quickly. With Docker, you can manage your infrastructure in the same ways you manage your applications.

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

Docker Containers

Docker images

Docker Client

Docker daemon

Docker instruct containerd to launch a container, containerd tell containerd-shim to launch a container, containerd uses runc to launch the container and then keep the signals line open with it while runc exits.



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

Docker-compose is designed for running multiple containers as a single service. It is a very useful concept to run the containers in isolation and allow them to interact with each other. This docker-compose is written in YAML.

**Dockerfile**

A Dockerfile is a plain text file that contains instructions for building Docker images. There's a Dockerfile standard they follow, and the Docker daemon is ultimately responsible for executing the Dockerfile and generating the image.

**Docker Image**

It is a template that contains the instructions for the Docker container. The image is written in a YAML language (Yet Another Markup Language) and then hosted as a file in the Docker registry. The image consists of different layers and each layer is dependent on the below it. These layers are created by executing the command in the Dockerfile.

**Docker Container**

Docker container is an executable package of the applications with necessary dependencies and configuration. It is lightweight due to the built-in structural redundancy. The containers run completely in isolation and are portable. The memories of a Docker environment can be shared across multiple containers, it is very useful for limited memory usage or storage.

**Docker vs Hypervisor?**


Hypervisors are of two types – the bare metal works directly on the hardware while type two hypervisor works on top of the operating system.

Docker, on the other hand, works on the host kernel itself. Hence, it does not allow the user to create multiple instances of operating systems.

Instead, they create containers that act as virtual application environments for the user to work on.

**How to implement CI/CD in Docker?**
Dockers help developers to build their code and test their code in any environment to catch bugs early in the application development life cycle. Dockers help streamline the process, save time on builds, and allow developers to run tests in parallel.

Dockers can integrate with source control management tools like GitHub and Integration tools like Jenkins. Developers submit the code to GitHub and test the code that automatically triggers a build using Jenkins to create an image. This image can be added to the Docker registry to deal with inconsistencies between different environment types.

One method to fit the Docker in the CI process is to have the CI server build the Docker Image after it has built the application. The application goes inside the image, and the image is then pushed to the Docker hub. On another host, either QA/Dev/Production environment, pull the nearly completed build from the Docker Hub and run the Container which will run your application. In the CI server, you could even have your Compile and Testing done as part of the Image build.

**What are the advantages and disadvantages of using docker?**
Advantages of Docker
It is light weight because it does not require any resource pre-allocation (RAM). Whenever it needs resources it acquires them from host OS. It is of less cost.

Continuous integration efficiency – Docker enables you to build a container image and use that same image across every step of the deployment process.

It can run on physical hardware, virtual hardware and on cloud.

You can re-use the image.

5. It takes very less time to create.

**Disadvantage of Dockers**
Docker is not good for application that requires rich GUI

It is difficult to manage large amount 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 cannot run on Linux Docker container

Docker is suitable when development OS and testing OS are same

It does not provide any solution for data backup and recover.

**What is a Docker namespace?**
Namespaces:

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

The pid namespace: Process isolation (PID: Process ID)

The net namespace: Managing network interfaces (NET: Networking)

The ipc namespace: Managing access to IPC resources (IPC: InterProcess Communication)

The mnt namespace: Managing filesystem mount points (MNT: Mount)

The uts namespace: Different host and domain names (UTS: Unix Timesharing System)

The user namespace: Isolate security-related identifiers (USER: userid, groupid)

**What is a Docker registry?**
A Docker registry is a system for storing and distributing Docker images with specific names. There may be several versions of the same image, each with its own set of tags. A Docker registry is separated into Docker repositories, each of which holds all image modifications. The registry may be used by Docker users to fetch images locally and to push new images to the registry (given adequate access permissions when applicable). The registry is a server-side application that stores and distributes Docker images. It is stateless and extremely scalable.

**What is an entry point?**
In a Dockerfile, we use the ENTRYPOINT instruction to provide executables that will always execute when the container is launched.

Will data on the container be lost when the docker container exits?
Yes, the data inside your container will be lost. It is ideal to have your data stored on the host system or persistent file system. This can be done be done by attaching your containers to volumes and mounting them.

**What is a Docker swarm?**
When the configuration of these machines gets complete and takes a form of cluster, we can still run the Docker commands that we're used to, but now they will be executed by the several machines present in the Cluster.

Here the term "Swarm" comes into play, it is the group that controls all machines available in the Cluster, and every machine that is present or joins the Cluster is considered as a Node.

**What are the docker commands for the following:**
view running containers- docker ps /docker ps -a

command to run the container under a specific name-

docker container run --name [container_name] [docker_image]

command to export a docker-docker export [OPTIONS] CONTAINER

e.g.docker export --output="latest.tar" red_panda

docker export will export the contents of the underlying directory, not the contents of the volume.

docker import – Import the contents from a tarball to create a filesystem image.e.g.docker import calc-container.tar calcfs:latest

commands to delete a container-docker rm [OPTIONS] CONTAINER [CONTAINER...]

command to remove all stopped containers, unused networks, build caches, and dangling images?-docker system prune -a WARNING! This will remove: - all stopped containers - all networks not used by at least one container - all images without at least one container associated to them - all build cache.

**What are the common docker practices to reduce the size of Docker Image?**
The following are the methods by which we can achieve docker image optimization.

Using distroless/minimal base images

Multistage builds

Minimizing the number of layers

Understanding caching

Using Dockerignore

Keeping application data elsewhere