Docker Important Interview Questions

Docker Important Interview Questions

Day 21: #90DaysOfDevOps Challenge

Table of contents

No heading

No headings in the article.

Within a Docker container, applications and dependencies are bundled into a single image, which can operate on any Docker-enabled system. This facilitates the deployment of applications to different environments, such as development, testing, and production, without the need for complicated configuration or concerns about compatibility.

Moreover, Docker containers are scalable, which means that the number of instances running can be easily adjusted in response to changing demand. This characteristic makes Docker containers an excellent choice for modern microservices architectures that rely on small, independent services that can be scaled individually.

Basic Interview Questions

1. What is a Docker Container ?

A Docker container is a lightweight and standalone executable package that encapsulates an application and all its dependencies, allowing it to run in a self-contained environment. Containers are created from Docker images, which are preconfigured templates that include all the necessary files, libraries, and configurations required to run the application.

Docker containers offer several benefits over traditional application deployment methods. They provide isolation and portability, making it easy to move applications between different environments without worrying about compatibility issues. Additionally, they allow for efficient use of resources, as multiple containers can be run on a single host without conflicts.

2. What is a docker image ?

Docker images are preconfigured templates that include all the necessary files, libraries, and configurations required to run an application inside a Docker container. They serve as the foundation for Docker containers, enabling applications to be easily moved between different environments and deployed consistently across multiple systems.

3. What is a DockerFile?

A Dockerfile is a text file that contains a set of instructions used to build a Docker image. It specifies the application to be built, the base image to use, and the dependencies required to run the application.

4. What is Docker compose?

Docker Compose is a tool for defining and running multi-container Docker applications. It allows you to define the services, networks, and volumes required for your application in a simple YAML file, and then use a single command to start and stop all the containers at once.

With Docker Compose, you can easily manage complex applications consisting of multiple containers, each running a different component of the application. You can also configure network and volume settings, and define dependencies between services to ensure that they are started in the correct order.

5. What is the docker namespace?

A Docker namespace is a mechanism for ensuring the uniqueness of names used for Docker images, containers, volumes, and networks. Each namespace has its own set of names, preventing conflicts and ensuring that resources are uniquely identified. Docker uses a default namespace called “docker” but users can create their own namespaces for better organization and management of resources.

6. docker command that lists the status of all docker containers?

docker ps -a // for all containers

docker ps // for all started containers

7. Data stored in a Docker container can be lost under various circumstances. For example, if the container is deleted or recreated, any data stored inside it will be lost unless it explicitly persisted outside of the container. Additionally, if a container crashes or is stopped unexpectedly, any unsaved data will be lost?

To prevent data loss, it is recommended to store any important data outside of the container, using volumes or bind mounts to mount the data into the container. This allows data to persist even if the container is deleted or recreated. It is also important to regularly back up any important data to ensure that it can be recovered in the event of data loss or corruption.

8. What is a docker image registry?

A Docker image registry is a centralized repository for storing and distributing Docker images. It allows users to upload and share images with others, as well as download images from a central location. Popular public image registries include Docker Hub, Google Container Registry, and Amazon Elastic Container Registry, while private registries can be set up for secure internal use.

9. What are Docker components?

Docker is composed of several components that work together to enable the creation, deployment, and management of containerized applications. The main components of Docker are:

  • Docker Engine: The core component of Docker that runs and manages Docker containers on a host system.

  • Docker Hub: A public registry that stores and distributes Docker images.

  • Docker CLI: A command-line interface that allows users to interact with Docker and manage containers, images, and other resources.

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

  • Docker Swarm: A native clustering and orchestration solution for Docker that enables the deployment and management of containerized applications across multiple hosts.

  • Docker Registry: A tool for storing and distributing Docker images, either publicly or privately.

10. What is a Docker Hub?

Docker Hub is a public cloud-based registry service that allows users to store, share, and distribute Docker images. It provides a centralized location for users to upload and access images, as well as tools for managing image versions, collaborating with other users, and automating workflows. Docker Hub also offers a private registry service for users who need to store images behind their own firewall.

11. What command can you run to export a docker image as an archive?

To export a Docker image as an archive, you can use the “docker save” command followed by the image name and pipe it to the “gzip” utility to compress the output. The command will create a compressed archive of the image and its dependencies, which can be saved to a file or transferred to another system for import using the “docker load” command.

docker save -o <exported_name>.tar <container-name>

12. Is it possible to remove a paused container from Docker?

No, it is not possible!

Intermediate Interview Questions

1. Differentiate between virtualization and containerization?

Virtualization and containerization are two different approaches to running multiple applications or services on a single host system. Here are some key differences:

  • Virtualization uses a hypervisor to create multiple virtual machines (VMs) on a host system, with each VM running a separate guest operating system. Containerization, on the other hand, uses the host system’s operating system and kernel to create multiple isolated containers.

  • Virtual machines require a significant amount of resources, as each VM runs a complete operating system and associated libraries and services. Containers are much lighter weight, as they share the host system’s operating system and can use shared resources, making them more efficient and scalable.

  • Virtual machines are generally more secure, as each VM has its own operating system and kernel, providing an additional layer of isolation between applications. Containers can also be secure, but rely on the host system’s security measures to isolate containers from one another.

  • Virtual machines can run a wide variety of operating systems and applications, while containers are typically limited to applications that can run on the host system’s operating system and kernel.

2. COPY and ADD commands that are used in a Dockerfile?

In a Dockerfile, the COPY and ADD commands are used to copy files and directories from the host machine to the Docker image.

The syntax for the COPY command is:

Copy codeCOPY <src> <dest>

where <src> is the path to the file or directory on the host machine, and <dest> is the path to the destination directory in the Docker image.

For example, to copy a file called app.py from the current directory on the host machine to a directory called app in the Docker image, you would use the following command:

Copy codeCOPY app.py /app/

The syntax for the ADD command is similar to that of the COPY command, but it also supports some additional features, such as URL support and automatic file extraction.

Copy codeADD <src> <dest>

For example, to add a file called data.tar.gz from the current directory on the host machine to a directory called data in the Docker image, you would use the following command:

Copy codeADD data.tar.gz /data/

Note that while ADD can be used to achieve similar functionality as COPY, it is recommended to use COPY for most use cases as it is simpler and more explicit.

3. Can a container restart by itself?

Yes.

  1. Off: In this, the container won’t be restarted in case it’s stopped or it fails.
  2. On-failure: Here, the container restarts by itself only when it experiences failures not associated with the user.
  3. Unless-stopped: Using this policy, ensures that a container can restart only when the command is executed to stop it by the user.
  4. Always: Irrespective of the failure or stopping, the container always gets restarted in this type of policy.

These policies can be used as:
docker run -dit — restart [restart-policy-value] [container_name]

4. differences between a docker Image and a Layer?

In Docker, an image is a read-only template used to create one or more container instances, while a layer is a file system delta that represents a change or modification made to an image.

Here are some key differences between a Docker image and a layer:

  • An image is composed of one or more layers, with each layer representing a change or modification made to the image. When a new layer is added to an image, it is stacked on top of the previous layer, resulting in a chain of layered file system changes.

  • An image is a static entity that does not change once it has been built, while layers can be added, modified or removed as needed.

  • An image can be used to create one or more container instances, while a layer is a building block that is used to construct images.

  • Images are created using Dockerfiles or by importing them from a registry, while layers are created by making changes to a base image or other existing layers.

  • Images are identified by a unique tag or digest, while layers are identified by their content-based address using a SHA256 hash.

5. What is volume in docker ?

In Docker, a volume is a mechanism for persisting data generated by containers or shared between containers and the host machine. A volume is a named filesystem entity that can be mounted to one or more containers, allowing data to be stored outside the container’s filesystem.

Volumes can be created using the docker volume command, and they are managed by Docker’s volume drivers, which provide access to a variety of storage backends such as local filesystems, network filesystems, cloud storage, and more.

docker run -v host_path:docker_path <container_name>

docker run -v /data/app:usr/src/app myapp

6. Write docker volume path ?

/var/lib/docker/volumes/

7. Commonly used instructions in Dockerfile?

There are several commonly used instructions in a Dockerfile, including:

  1. FROM: specifies the base image to use for the container.

  2. RUN: executes commands within the container to install or configure software packages and dependencies.

  3. COPY: copies files or directories from the host machine to the container.

  4. ADD: similar to COPY, but also supports downloading files from URLs and extracting archives.

  5. WORKDIR: sets the working directory for subsequent instructions.

  6. ENV: sets environment variables within the container.

  7. EXPOSE: documents the network ports that the container will listen on at runtime.

  8. CMD: specifies the default command to run when the container starts.

  9. ENTRYPOINT: specifies the executable that should be run when the container starts, along with any default command line arguments.

  10. VOLUME: creates a named volume that can be mounted from the host machine or shared between containers.

8. Differentiate between Daemon Logging and Container Logging?

Daemon logging refers to the logging of Docker’s internal processes, such as starting and stopping containers, network operations, and other low-level activities. Daemon logs are generated by the Docker daemon, which is responsible for managing containers, images, volumes, and other Docker objects. The Docker daemon logs can be viewed using the docker logs command or by accessing the Docker daemon log file directly.

Container logging, on the other hand, refers to the logging of events and output generated by containers themselves. Container logs are captured by the Docker logging driver, which is responsible for collecting and forwarding container logs to a logging endpoint or storage backend. The Docker logging driver supports several logging options, including JSON file logging, Syslog, and external logging services like Elasticsearch and Splunk.

9. How to communicate between the docker host and the Linux host?

To create an Ethernet adapter on the Docker host, you can use networking and identify the “ipconfig”. This command ensures that the Ethernet adapter is created as long as Docker is installed on the host.

10. Difference between CMD and ENTRYPOINT?

CMD: The CMD instruction specifies the default command to be run when a container starts. It is optional and can be overridden when running the container. If multiple CMD instructions are provided, only the last one is used. The CMD command is typically used to specify the main command or process that runs inside the container.

ENTRYPOINT: The ENTRYPOINT instruction specifies the executable that should be run when the container starts, along with any default command-line arguments. It is also optional, but unlike CMD, it cannot be overridden when running the container. If multiple ENTRYPOINT instructions are provided, only the last one is used. The ENTRYPOINT command is typically used to define a binary or script that performs some setup or initialization tasks before executing the main command or process.

Advanced Interview Question

1. Lifecycle of Docker Container?

  • Create: A container is created using a Docker image, which contains all the necessary dependencies and configurations to run an application.

  • Start: The container is started using the “docker run” command, which initializes the container and executes the specified command.

  • Run: The container runs the application or process specified in the Dockerfile.

  • Pause: The container can be paused using the “docker pause” command, which temporarily stops all processes in the container.

  • Unpause: The container can be unpaused using the “docker unpause” command, which resumes all processes in the container.

  • Stop: The container can be stopped using the “docker stop” command, which sends a signal to the container to terminate all running processes and stop the container.

  • Restart: The container can be restarted using the “docker restart” command, which stops and starts the container.

  • Remove: The container can be removed using the “docker rm” command, which deletes the container and its associated filesystem.

2. How to use docker for multiple application environments?

The Docker Compose feature can be used in this scenario. By defining multiple services, networks, containers, and volume mappings in a clean and organized manner within a docker-compose file, the command “docker-compose up” can be used to run the application. When dealing with multiple environments such as development, staging, user acceptance testing, or production servers, environment-specific dependencies and processes can be defined in separate docker-compose files named “docker-compose.{environment}.yml”. This enables the application to be set up and run according to the specific environment requirements.

3. How ensure that a container 1 runs before container 2 while using docker compose?

To ensure that container 1 is run before container 2 using Docker Compose, you can specify the dependency of container 2 on container 1 in the docker-compose file using the “depends_on” option. This will ensure that container 1 is started and ready before container 2 begins to execute.

Thank you for reading!!!
~Bhaarat