Docker Interview Questions And Answers


Introduced in 2013, Docker impacted the IT industry. It turned out to be a big hit, with more than 10 million container image downloads each month. This growth has significantly increased the job openings, which has led to the generation of Docker Interview Questions And Answers. In this post, we have provided a thorough list of the best interview questions for it.

It is a free and open-source platform that allows developers to deploy, build, run, manage, and update containers which is a standardized, executable component that will combine application source code with the OS libraries and dependencies needed to run that code in all environments. Containers will simplify the development and deliver distributed applications. It has become popular since companies move to cloud-native development and hybrid multi-cloud environments. 

Developers can generate containers without Docker when you work directly with capabilities built into Linux and other OS. But the help of it makes containerization easier, safer, and faster. At the writing, It has more than 13 million developers using the platform.

Docker, Inc., is a company known for selling the platform’s commercial and for the Docker open-source project to which the company and other organizations contribute.

It also offers the capability to run and package an application in an isolated environment known as a container. This security and isolation allow you to run most containers simultaneously on any given host. Containers are lightweight and equipped with everything you need to run any application so that you do not have to rely on what is currently installed on the host. 

These Docker Interview Questions have questions you should know to secure a good job. These are the primary questions that interviewers will start with and increase the difficulty level as you progress.

What is a Hypervisor?

A hypervisor is a software that makes virtualization possible. It is also called Virtual Machine Monitor. It divides the host system and allocates the resources to each divided virtual environment. You can basically have multiple OS on a single host system. There are two types of Hypervisors:

  • Type 1: It’s also called Native Hypervisor or Bare metal Hypervisor. It runs directly on the underlying host system. It has direct access to your host’s system hardware and hence does not require a base server operating system.
  • Type 2: This kind of hypervisor makes use of the underlying host operating system. It’s also called Hosted Hypervisor.

What is virtualization?

Virtualization is the process of creating a software-based, virtual version of something(computer storage, servers, application, etc.). These virtual versions or environments are created from a single physical hardware system. Virtualization lets you split one system into many different sections which act like separate, distinct individual systems. A software called Hypervisor makes this kind of splitting possible. The virtual environment created by the hypervisor is called Virtual Machine.

What is containerization?

Let me explain this with an example. Usually, in the software development process, code developed on one machine might not work perfectly fine on any other machine because of the dependencies. This problem was solved by the containerization concept. So basically, an application that is being developed and deployed is bundled and wrapped together with all its configuration files and dependencies. This bundle is called a container. Now when you wish to run the application on another system, the container is deployed which will give a bug-free environment as all the dependencies and libraries are wrapped together. Most famous containerization environments are Docker and Kubernetes.

Difference between virtualization and containerization

Once you’ve explained containerization and virtualization, the next expected question would be differences. The question could either be differences between virtualization and containerization or differences between virtual machines and containers. Either way, this is how you respond.

Containers provide an isolated environment for running the application. The entire user space is explicitly dedicated to the application. Any changes made inside the container are never reflected on the host or even other containers running on the same host. Containers are an abstraction of the application layer. Each container is a different application.

Whereas in Virtualization, hypervisors provide an entire virtual machine to the guest (including Kernal). Virtual machines are an abstraction of the hardware layer. Each VM is a physical machine.

How do you estimate the Docker server version and the client?

$ docker version

command will give the requisite information regarding the Docker client and server version.

What is Docker?

It is a containerization platform which packages your application and all its dependencies together in the form of containers so as to ensure that your application works seamlessly in any environment, be it development, test or production. Docker containers, wrap a piece of software in a complete filesystem that contains everything needed to run: code, runtime, system tools, system libraries, etc. It wraps basically anything that can be installed on a server. This guarantees that the software will always run the same, regardless of its environment.

How to pull Docker Image from Private Registry

First login to Private Registry

$ docker login myrepo.fosstechnix.com:9000

Then pull  your images

Syntax:

$ docker pull [OPTIONS] ADDRESS:PORT[/PATH]/IMAGE_NAME[:TAG]

Example:

$ docker pull repo.fosstechnix.com:9000/demo-image

What is a Docker Container?

Docker containers include the application and all of its dependencies. It shares the kernel with other containers, running as isolated processes in user space on the host operating system. The containers are not tied to any specific infrastructure: they run on any computer, on any infrastructure, and in any cloud. The containers are basically runtime instances of images.

What are Docker Images?

Docker image is the source of the container. In other words, Docker images are used to create containers. When a user runs a image, an instance of a container is created. These images can be deployed to any Docker environment.

What is Docker Hub?

Docker images create docker containers. There has to be a registry where these images live. This registry is Docker Hub. Users can pick up images from the Hub and use them to create customized images and containers. Currently, the Hub is the world’s largest public repository of image containers.

Explain Docker Architecture?

The Architecture consists of an Engine which is a client-server application with three major components:

  1. A server which is a type of long-running program called a daemon process.
  2. A REST API which specifies interfaces that programs can use to talk to the daemon and instruct it what to do.
  3. A command line interface (CLI) client (the docker command).
  4. The CLI uses the Docker REST API to control or interact with the Docker daemon through scripting or direct CLI commands. Many other Docker applications use the underlying API and CLI.

What is a Dockerfile?

Docker can build images automatically by reading the instructions from a file called Dockerfile. A Dockerfile is a text document that contains all the commands a user could call on the command line to assemble an image. Using docker build, users can create an automated build that executes several command-line instructions in succession.

Tell us something about Docker Compose.

Docker Compose is a YAML file which contains details about the services, networks, and volumes for setting up the Docker application. So, you can use Docker Compose to create separate containers, host them and get them to communicate with each other. Each container will expose a port for communicating with other containers.

What is Docker Swarm?

You are expected to have worked with Docker Swarm as it’s an important concept of Docker.

Docker Swarm is native clustering for Docker. It turns a pool of Docker hosts into a single, virtual Docker host. Docker Swarm serves the standard Docker API, any tool that already communicates with a Docker daemon can use Swarm to transparently scale to multiple hosts.

Can you please write Dockerfile to create Ubuntu Docker Image

FROM ubuntu:18.04
MAINTAINER FOSS TECHNix support@fosstechnix.com
LABEL version="1.0" \
RUN apt-get update && apt-get install -y apache2 && apt-get clean
ENV APACHE_RUN_USER www-data
ENV APACHE_RUN_GROUP www-data
ENV APACHE_LOG_DIR /var/log/apache2
EXPOSE 80
COPY index.html /var/www/html
CMD ["/usr/sbin/apache2", "-D", "FOREGROUND"]

Can you please write Dockerfile to create Node Js Docker Image

FROM node:10
RUN mkdir -p /home/nodejs/app
WORKDIR /home/nodejs/app
COPY package.json .
RUN npm install
COPY . .
CMD [“node.js”, “index.js”]
EXPOSE 3000

What is the command to build docker images from Dockerfile?

$ docker build -t image_name

What is the command to run the image as a container?

$ docker run -it ubuntu /bin/bash

Here i -> interactive, t -> terminal

What is command to list docker images

$ docker images

What is command to list specific docker image

$ docker image ls <imagename>

How to apply port mapping to a running container?

We can apply port forwarding while running a container using below command.

$ docker run -p <public_port>:<private_port> -d <image>

What is command to login docker container

$ docker exec -it <container_Name> /bin/bash

What is command to stop a docker container

$ docker stop <container_id>

How Can You start a docker container?

$ docker start <container_id>

What is command to remove a Docker container

$ docker rm <container_id>

How Can You list out Running Docker containers

$ docker ps

What are the common instructions in Dockerfile

Below are some common instructions in Dockerfile

FROM, MAINTAINER, RUN, CMD, LABEL, EXPOSE, ENV, ADD, COPY, ENTRYPOINT, VOLUME, USER, WORKDIR, ARG, ONBUILD, STOPSIGNAL, SHELL, HEALTHCHECK

What is a Docker Namespace?

A namespace is one of the Linux features and an important concept of containers. Namespace adds a layer of isolation in containers. It provides various namespaces in order to stay portable and not affect the underlying host system. Few namespace types supported by Docker – PID, Mount, IPC, User, Network

What is the lifecycle of a Docker Container?

This is one of the most popular questions asked in Docker interviews. Docker containers have the following lifecycle:

  • Create a container
  • Run the container
  • Pause the container(optional)
  • Un-pause the container(optional)
  • Start the container
  • Stop the container
  • Restart the container
  • Kill the container
  • Destroy the container

What is Docker Machine?

Docker machine is a tool that lets you install Docker Engine on virtual hosts. These hosts can now be managed using the docker-machine commands. It also lets you provision Docker Swarm Clusters.

Docker Basic Commands

Once you’ve aced the basic conceptual questions, the interviewer will increase the difficulty level. So let’s move on to the next section of this Docker Interview Questions article. This section talks about the commands that are very common amongst the users.

Where do you think Docker is being used?

When asked such a question, respond by talking about applications. It is being used in the following areas:

  • Simplifying configuration: It lets you put your environment and configuration into code and deploy it.
  • Code Pipeline Management: There are different systems used for development and production. As the code travels from development to testing to production, it goes through a difference in the environment. It also helps in maintaining the code pipeline consistency.
  • Developer Productivity: Using Docker for development gives us two things – We’re closer to production and the development environment is built faster.
  • Application Isolation: As containers are applications wrapped together with all dependencies, your apps are isolated. They can work by themselves on any hardware that supports Docker.
  • Debugging Capabilities: It supports various debugging tools that are not specific to containers but work well with containers.
  • Multi-tenancy: Docker lets you have multi-tenant applications avoiding redundancy in your codes and deployments.
  • Rapid Deployment: It eliminates the need to boost an entire OS from scratch, reducing the deployment time.

How is Docker different from other containerization methods?

Docker containers are very easy to deploy in any cloud platform. It can get more applications running on the same hardware when compared to other technologies, it makes it easy for developers to quickly create, ready-to-run containerized applications and it makes managing and deploying applications much easier. You can even share containers with your applications.

Will cloud overtake the use of Containerization?

Docker containers are gaining popularity but at the same time, Cloud services are giving a good fight. In my personal opinion, It will never be replaced by Cloud. Using cloud services with containerization will definitely hype the game. Organizations need to take their requirements and dependencies into consideration into the picture and decide what’s best for them. Most of the companies have integrated Docker with the cloud. This way they can make the best out of both the technologies.

How many containers can run per host?

35. How many containers can run per host?

There can be as many containers as you wish per host. Docker does not put any restrictions on it. But you need to consider every container need storage space, CPU and memory which the hardware needs to support. You also need to consider the application size. Containers are considered to be lightweight but very dependent on the host OS.

Is it a good practice to run stateful applications on Docker?

The concept behind stateful applications is that they store their data onto the local file system. You need to decide to move the application to another machine, retrieving data becomes painful. I honestly would not prefer running stateful applications on Docker.

State some important Docker commands.

Some important commands include the following:

  • Build: Builds an image file for Docker
  • Create: Creates a new container
  • Kill: Kills a container
  • Dockerd: Launches the Docker daemon
  • Commit: Creates a new image from container changes

What are Docker objects?

Docker images, services, and containers are termed Docker objects.

  • Image: Contains instructions to create a Docker container 
  • Containers: A runnable instance of an image
  • Service: Container scaling across various Docker Daemons as a swarm

Other Docker objects include networks and volumes.

Which is more suitable for a Docker container: a stateless or stateful application?

Stateless applications are more suitable for Docker containers because we can create a single, reusable container for our application, which allows us to detach state configuration from the app and the container. 

By doing this, we can run multiple instances of the same container with varying production parameters, etc. 

So, stateless applications give us the freedom to use the same image in a range of scenarios, including various prod and test environments.

Which networks are available by default in Docker?

Default available networks include: 

  • Bridge: Default network that containers will connect to if the network has not been otherwise specified
  • None: Connects to a container-specific network stack that doesn’t have a network interface
  • Host: Connects to the host’s network stack

Mention the hardware & software requirements.

These types of questions are common in interview boards. The answer to this question is, you can install Docker UCP directly on your device or on a cloud provider. To install UCP, all nodes must have:

  • You will need to have Linux Kernel version 3.10 at least or higher.
  • RAM 8.00 GB; essential for manager nodes.
  • RAM 4.00 GB; essential for worker nodes.
  • Disk space should be available by 3.00 GB.
  • CS Docker Engine 1.13/ EE Daemon 17.03 or higher.
  • One static IP address.

What is a node?

Docker Swarms are made of single or multiple nodes. These nodes are of two types: Manager Node and Worker Node. Their functionalities vary based on their responsibilities.

Manager Node: Manager Nodes handle tasks that are related to cluster management. They offer maintenance of cluster environments, schedule Docker services, and also aid swarm mode HTTP API endpoints.

Worker Node: Worker Nodes are different from Manager Nodes. And they don’t participate in any of the tasks that Manager Nodes do. Their only purpose is to carry out containers.

The relation between a manager node and a worker node is, you can easily create a manager node. But to create a worker node, you will need at least one manager node.

What is the difference between a registry and a repository?

The Docker Registry hosts and distributes images and the Docker Hub is the default registry. The Docker repository (or repo) allows you to store a collection of image versions. This means that images will have the same names, but their tags will vary to represent the different versions.

Mention some benefits that Docker provides to IT Firms.

Docker enhances the functionalities of an IT firm by a constant of 10. It modernizes the IT environment and makes application updates and changes much easier. This is possible because containers run independently on any infrastructure. In a nutshell, It improves application reliability and availability, making it easier for IT firms to proactively manage security risks.

Can I run Docker on Windows?

These types of questions are enlisted under advanced Docker Interview Questions. However, you can still keep on your list to be on the safe side. The answer is, Docker Engine doesn’t natively run on Windows. A Linux virtual machine will be needed as Docker Engine uses Linux-specific kernel features. However, you will have to make use of the Docker Machine command. The “docker-machine” command helps you to build and tie to a small-scale Linux VM on the user device. 

Only a virtual machine can host Docker Engine on the Windows operating system. This is the case for the Windows versions that are prior to Windows 10. However, it has a desktop version for running as a native software in Windows 10.

What is the memory-swap flag?

The ‘memory-swap’ flag is a modifier that can be combined with the run command to give a container access to additional virtual memory when it has utilized all of its provisioned physical memory (RAM). This command requires that the ‘memory’ flag be preset when executing the run command. 

For example: –memory = “256m”; –memory-swap = “512m”; With this setup, a container is provisioned with 256MB of physical memory, and with an additional virtual swap space of 256MB (512m-256m).

What is CNM?

CNM, or Container Network Model, formally defines the steps for the networking of containers, while also maintaining the abstraction used to support multiple network drivers. Sandbox, endpoint, and network are the three components.

Can I lose data when Containers exit?

Every Docker user should be well aware of the data usage that comes along with it. The proper answer to this question is positive. You will lose a certain amount of data if the container exits or gets restarted.

This is not a bug, but it is actually a feature. By doing so, it ensures that you can knock down or rebuild your container back from its original state. However, to avoid the complete loss of data, you can use constant volumes for storing your data.

Does a Docker Container have any IP address?

Yes, the container has an IP address. In fact, the Docker container IP address can be easily visible if you apply specific commands in modern Docker.

What are Docker object labels?

This is a key-value pair stored as a string. We can apply metadata using labels, which can be used for images, containers, volumes, networks, local daemons, swarm nodes, and services. Every object should have a unique label, and these are static for an object’s entire lifetime.