DOCKER Interview Questions and Answers- Part 3

Containerization has transformed software development, and at the heart of it all is Docker. This powerful tool enables seamless application deployment, ensuring consistency across environments.  

As organizations prioritize scalability and reliability, Docker expertise has become a must-have skill for DevOps engineers and software developers. Preparing for a Docker interview means mastering container fundamentals, orchestration techniques, and troubleshooting strategies.  

This page compiles essential Docker interview questions to help candidates navigate both technical and conceptual aspects of the tool. Whether you’re a beginner or an experienced professional, these questions will refine your understanding, enhance your problem-solving abilities, and equip you to ace your next interview. 

Answer:

Yes, you can utilize JSON instead of YAML for the Docker compose file. To deploy a JSON file with composing, you need to specify the filename.

Answer:

  • CMD offers executable defaults for an executing container. If the executable needs to be omitted, incorporate ENTRYPOINT instruction with the JSON array format.
  • ENTRYPOINT denotes that the instruction within will always run when the container starts. This command gives an option to configure the executables & the parameters. If the DockerFile doesn’t have this command, it would still get inherited from the base image mentioned in the FROM instruction.

Answer:

Yes, Docker containers can restart by themselves. It defines some policies to restart the container. These are

  • Off: container won’t be restarted if it fails or stops:
  • On-failure: The container restarts only when a failure is not occurred by the user.
  • Unless-stopped: The container restarts only when a user executes a command to stop it,
  • Always: The container is always restarted despite an error or other issues.

Answer:

Container orchestration helps you manage the containers running in a large & dynamic environment. It can automate & control the following tasks:

  • Load balancing,
  • Deploying & provisioning of containers,
  • Allocation of resources between the containers,
  • Monitoring the health of hosts & containers,
  • Scaling the containers,
  • Switching containers from one host to other when the host is lacking resources or is unavailable.

Answer:

Docker volumes are managed by the Docker (which is not accessed by the Non-Docker processes). They are stored in the part of Docker host filesystem: /var/lib/docker/volumes/. Volumes are the most effective way to persist data in the Docker.

Answer:

Some important Docker Advanced Commands:

  • docker-version: To understand the installed docker version. Syntax, docker–version
  • docker ps: It lists all the docker containers are running with container details. Syntax: docker ps
  • docker ps -a: It lists all the containers, including those running, exited, stopped with the details. Syntax: docker ps -a
  • docker exec: It accesses the container & runs commands inside that container. Syntax: docker exec (options)
  • docker build: It builds an image from the Dockerfile. Syntax: docker build [options] path|URL
  • docker rm: It removes the container with a mentioned container id. Syntax: docker rm
  • docker rmi: It removes the docker image with a mentioned image id. Syntax: docker rmi
  • docker info: It gets detailed information about the Docker installed on the system like the no. of images, containers, paused, running, server version, stopped, volume, runtime, total memory, kernel version, etc. Syntax: docker info
  • docker cp: The copies a file from the docker container to the local system. Syntax: docker cp
  • docker history: It displays the history of a docker image with a mentioned image name. Syntax: docker history

Answer:

These are three mount types in the Docker

  • Volume Mount-It is stored in a part of the host filesystem & managed by Docker (/var/lib/docker/volumes/ on Linux). Non-Docker processes must not modify this part of the filesystem. Volumes are the best way to persist.
  • Bind Mount– It may be stored anywhere in the host system & can even be important system directories or files. Non-Docker processes on a Docker container or Docker host can modify them at any time.
  • tmpfs Mount- It is only stored in the host system’s memory & is never written to the host system’s filesystem.

Answer:

Yes, we can run multiple copies of a compose file on the same host using the docker-compose. Through it, you can use a YAML file to configure the applications’ services. Thereon, with a single command, all the services can be started & created. To use Docker Compose, follow these steps:

  • Define an application environment in the Dockerfile so it can be replicated anywhere
  • Define the services of your application in the docker-compose.yml file.
  • Run docker-compose up to start & create the entire application.

Answer:

Virtual Machines or VMs virtualize the underlying hardware. They run on the physical hardware through an intermediation layer called a hypervisor. It requires additional resources to scale up VMs. Virtual machines are more suitable for monolithic applications. However, Docker is OS-level virtualization. Docker containers user space ace on top of the host kernel, making them fast & lightweight. It makes up-scaling simpler, needed to create another container from the image. Docker is usually more suitable for Microservices-based cloud apps.

Answer:

The .dockerignore file enables you to mention the list of files or directories you want to ignore when building the image. It will reduce the image’s size & help to speed up the docker build process.

Answer:

The Dockerfile means the source code of the image. After creating the Dockerfile, you build the image of the container. The image is just like the “compiled version” of the “source code.” Once you have the image, you need to redistribute it using the registry & use the image to run containers.

Answer:

  • Process in a Virtual Machine can’t see processes in other Virtual Machines. On the other hand, Process in a Docker Container can’t see the processes in other Docker Containers.
  • Each Virtual Machine has its own root filesystem while Each Docker Container has its own root file system (not Kernal)
  • Each Virtual Machine gets its own virtual network adapter. In contrast, Docker can get a virtual network adapter. It has separate ports & IP.
  • A virtual machine is a running instance of the physical files Whereas Docker Containers are running instances of a Docker Image.
  • Host OS can be different from the guest OS. On the other hand, Host OS can be different from the Container OS.

Answer:

Docker Container can run both Linux and Windows programs. It runs natively on Linux (on x86-64, ARM & several other CPU architectures) & on Windows (x86-64). Docker Inc. builds products that help you create & run containers on Linux, Windows & macOS.

Answer:

  • A Dockerfile can be defined as a text document that contains all the instructions or commands for users to assemble the image.
  • Docker Compose refers to a tool for running & defining multi-container Docker apps. You can use a YAML file to configure your app’s services through the Docker Container. Thereby, with a single command, you can start & create all services from your configuration. By default, docker-compose expects the Compose file’s name as docker-compose.yml. If the compose file has different names, you can specify it with the -f flag.

Answer:

Below are two ways to configure the Docker daemon:

  • Use a JSON configuration file (it a preferred option, as it keeps all configurations in one place).
  • Use flags when starting the dockerd.

You can use both the above options together if you don’t specify the same option both as a flag & in the JSON file. If it happens, the Docker daemon will not start & prints an error message.

Answer:

A docker-compose stop attempts to stop a specific Docker container by sending the SIGTERM message. Once the message is delivered, it waits for a default time-out of 10 seconds; after that, it sends a SIGKILL message to the container to kill it forcefully. If you’re waiting for the time-out period, it means that the containers aren’t shutting down on receiving SIGTERM messages or signals.

Answer:

In a Docker Composer, use up command to restart or start the services defined in the docker-compose.yml. The Docker Composer run command is used to perform administrative tasks like removing or adding data to a data volume container. The docker-compose start command only helps to restart containers that were previously created but got stopped. It never produces new Docker containers.

  • Up Command: It keeps a Docker Compose up; one can restart or start all the drivers, networks, & services associated with the application specified in the docker-compose.yml file. If the docker-compose is run in the “detached” mode, it exits & shows no logs once the containers are started.
  • Run Command– Through this command, the docker-compose can run ad-hoc tasks based on the business needs. Here, the service name needs to be provided & the docker only starts that specific service and other services to which the target service is dependent. It helps to test the containers & perform tasks such as adding or removing data to the container volumes.
  • Start Command– This command helps to restart those containers which were already created & stopped. It does not help to create new containers on its own.

Answer:

Windows Server Container shares the underlying Operating System kernel, making it smaller than the Virtual Machine as both don’t need a copy of the OS. Security can be a concern in Windows Server; however, if one container is compromised, the Operating System and all the other containers could be at risk.Hyper-V Containers & their dependencies reside in the Hyper-V Virtual Machines. It provides an additional layer of isolation. Containers are used for stateless & microservices applications as they are deposable and don’t store persistent data. Hyper-V VMs are equipped with virtual hard disks & are best-suited to mission-critical applications.

Answer:

Docker daemon refers to a service that manages Docker images, container, network & storage volumes. It consistently follows Docker API requests & processes them. A daemon can communicate with other daemons to manage Docker services.

Answer:

An image is built upon a series of layers, wherein each layer represents an instruction in the image’s Dockerfile. The layers get stacked on top of each other. When building a new container, you add a new writable layer on the top of an underlying layer. It is called the “container layer.” All changes made to a running container are written to this writable container layer.