Search Here

Docker Interview Questions

 

Basic Docker Interview Questions for Beginners

1. What is Docker?

Docker is an open-source containerization platform. It is used to automate the deployment of any application, using lightweight, portable containers.

2. What are Docker’s most notable features?

Docker’s most essential features include:

  • Application agility
  • Developer productivity
  • Easy modeling
  • Operational efficiencies
  • Placement and affinity
  • Version control

3. Why should anyone use Docker? What does it offer?

Docker gives users many incentives for adoption, such as:

  • An efficient and easy initial set up experience
  • The means to describe an application lifecycle in detail
  • Simple configuration and smooth interaction with Docker Compose
  • Complete and well-detailed documentation
  • Ability to run on a PC or enterprise IT system with equal ease

4. What about the opposite? Does Docker have any downsides?

Docker isn’t perfect. It comes with its share of drawbacks, including:

  • Lacks a storage option
  • Monitoring options are less than ideal
  • You can’t automatically reschedule inactive nodes
  • Automatic horizontal scaling set up is complicated

5. Name and explain the various Docker components.

The three main Docker components are:

  1. Docker Client. Performs Docker build pull and run operations to open up communication with the Docker Host. The Docker command then employs Docker API to call any queries to run.
  2. Docker Host. Contains Docker daemon, containers, and associated images. The Docker daemon establishes a connection with the Registry. The stored images are the type of metadata dedicated to containerized applications.
  3. Registry. This is where Docker images are stored. There are two of them, a public registry and a private one. Docker Hub and Docker Cloud are two public registries available for use by anyone.

6. What is a container?

Containers are deployed applications bundled with all necessary dependencies and configuration files. All of the elements share the same OS kernel. Since the container isn’t tied to any one IT infrastructure, it can run on a different system or the cloud.

7. Explain virtualization.

Virtualization is the means of employing software (such as Hypervisor) to create a virtual version of a resource such as a server, data storage, or application. Virtualization lets you divide a system into a series of separate sections, each one acting as a distinct individual system. The virtual environment is called a virtual machine.

8. What’s the difference between virtualization and containerization?

Virtualization is an abstract version of a physical machine, while containerization is the abstract version of an application.

9. Last simple question…Describe a Docker container’s lifecycle.

Although there are several different ways of describing the steps in a Docker container’s lifecycle, the following is the most common:

  1. Create container
  2. Run container
  3. Pause container
  4. Unpause container
  5. Start container
  6. Stop container
  7. Restart container
  8. Kill container
  9. Destroy container

We will next look at the intermediate-level docker interview questions and answers.

Exclusive Intermediate Interview Questions on Docker

10. Name the essential Docker commands and what they do.

The most critical Docker commands are:

  • Build. Builds a Docker image file
  • Commit. Creates a new image from container changes
  • Create. Creates a new container
  • Dockerd. Launches Docker daemon
  • Kill. Kills a container

11. What are Docker object labels?

Labels are the mechanism for applying metadata to Docker objects such as containers, images, local daemons, networks, volumes, and nodes.

12. How do you find stored Docker volumes?

Use the command: /var/lib/docker/volumes

13. How do you check the versions of Docker Client and Server?

This command gives you all the information you need: $ docker version

14. Show how you would create a container from an image.

To create a container, you pull an image from the Docker repository and run it using the following command: $ docker run -it -d <image_name>

15. How about a command to stop the container?

Use the following command: $ sudo docker stop container name

16. How would you list all of the containers currently running?

Use the command: $ docker ps

17. What’s involved in scaling a Docker container?

Docker containers have the potential to be scaled to any level needed. Thanks to the platform’s flexibility, you can have anything from a few hundred to a few thousand, to millions of containers, providing they all have continual, unconstrained access to the required memory and OS.

18. What do you know about the Docker system prune?

It’s a command used to remove all stopped containers, unused networks, build caches, and dangling images. Prune is one of the most useful commands in Docker. The syntax is:  $ docker system prune

We will next look into the advanced level docker interview questions and answers.

Advanced Docker Interview Questions for Experienced Professionals

19. List some of the more advanced Docker commands and what they do.

Some advanced commands include:

  • Docker info. Displays system-wide information regarding the Docker installation
  • Docker pull. Downloads an image
  • Docker stats. Provides you with container information
  • Docker images. Lists downloaded images

20. Can you lose data stored in a container?

Any data stored in a container remains there unless you delete the container.

21. What platforms can you run Docker on?

The Linux platforms are:

  • ArchLinux
  • CentOS 6+
  • CRUX 3.0+
  • Fedora 19/20+
  • Gentoo
  • openSUSE 12.3+
  • RHEL 6.5+
  • Ubuntu 12.04, 13.04 et al

Docker can also run on the following cloud-based platforms:

  • Amazon EC2
  • Amazon ECS
  • Google Compute Engine
  • Microsoft Azure
  • Rackspace

22. Which is the best method for removing a container: the command “stop container” followed by the command “remove the container,” the rm command by itself?

Stop the container first, then remove it. Here’s how:

  • $ docker stop <coontainer_id>
  • $ docker rm -f <container_id>

23. Can a container restart on its own?

Since the default flag -reset is set to false, a container cannot restart by itself.

24. How do Docker daemon and the Docker client communicate with each other?

You use a combination of Rest API, socket.IO, and TCP to facilitate communication.

25. Can you implement continuous development (CD) and continuous integration (CI) in Docker?

Yes, you can. You can run Jenkins on Docker and use Docker Compose to run integration tests.

26. Finally, how do you create a Docker swarm?

Use the following command: docker swarm init –advertise-addr <manager IP>


What are Docker Images?

When you mention Docker images, your very next question will be “what are Docker images”.

Docker image is the source of Docker container. In other words, Docker images are used to create containers. When a user runs a Docker image, an instance of a container is created. These docker 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 docker images live. This registry is Docker Hub. Users can pick up images from Docker Hub and use them to create customized images and containers. Currently, the Docker Hub is the world’s largest public repository of image containers.


What is Docker Swarm?


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.

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. Docker 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. Docker machine also lets you provision Docker Swarm Clusters.

How to check for Docker Client and Docker Server version?

The following command gives you information about Docker Client and Server versions:

$ docker version

How do you get the number of containers running, paused and stopped?

You can use the following command to get detailed information about the docker installed on your system.

$ docker info

If you vaguely remember the command and you’d like to confirm it, how will you get help on that particular command?

The following command is very useful as it gives you help on how to use a command, the syntax, etc.

$ docker --help

The above command lists all Docker commands. If you need help with one specific command, you can use the following syntax:

$ docker <command> --help

How to login into docker repository?

You can use the following command to login into hub.docker.com:

$ docker login

You’ll be prompted for your username and password, insert those and congratulations, you’re logged in.

How do you create a docker container from an image?

Pull an image from docker repository with the above command and run it to create a container. Use the following command:

$ docker run -it -d <image_name>

Most probably the next question would be, what does the ‘-d’ flag mean in the command?

-d means the container needs to start in the detached mode. Explain a little about the detach mode. Have a look at this blog to get a better understanding of different docker commands.

How do you list all the running containers?

The following command lists down all the running containers:

$ docker ps

Suppose you have 3 containers running and out of these, you wish to access one of them. How do you access a running container?

The following command lets us access a running container:

$ docker exec -it <container id> bash

How to start, stop and kill a container?

The following command is used to start a docker container:

$ docker start <container_id>

and the following for stopping a running container:

$ docker stop <container_id>

kill a container with the following command:

$ docker kill <container_id>


Once you’ve worked with an image, how do you push it to docker hub?

$ docker push <username/image name>

How to delete a stopped container?

Use the following command to delete a stopped container:

$ docker rm <container id>

How to delete an image from the local storage system?

The following command lets you delete an image from the local system:

$ docker rmi <image-id>

How to build a Dockerfile? 

Once you’ve written a Dockerfile, you need to build it to create an image with those specifications. Use the following command to build a Dockerfile:

$ docker build <path to docker file>

Can I use JSON instead of YAML for my compose file in Docker?

You can use JSON instead of YAML for your compose file, to use JSON file with compose, specify the JSON filename to use, for eg:

$ docker-compose -f docker-compose.json up


Is there a way to identify the status of a Docker container?

There are six possible states a container can be at any given point – Created, Running, Paused, Restarting, Exited, Dead.

Use the following command to check for docker state at any given point:

$ docker ps

The above command lists down only running containers by default. To look for all containers, use the following command:

$ docker ps -a

Can you remove a paused container from Docker?

The answer is no. You cannot remove a paused container. The container has to be in the stopped state before it can be removed.

Can a container restart by itself?

No, it’s not possible for a container to restart by itself. By default the flag -restart is set to false.

 Is it better to directly remove the container using the rm command or stop the container followed by remove container?

Its always better to stop the container and then remove it using the remove command.

$ docker stop <coontainer_id>
$ docker rm -f <container_id>

Stopping the container and then removing it will allow sending SIG_HUP signal to recipients. This will ensure that all the containers have enough time to clean up their tasks. This method is considered a good practice, avoiding unwanted errors.

Suppose you have an application that has many dependant services. Will docker compose wait for the current container to be ready to move to the running of the next service?

The answer is yes. Docker compose always runs in the dependency order. These dependencies are specifications like depends_on, links, volumes_from, etc.

Why use Docker?

Following are the reasons why one should use Docker:

  • It allows the use of system resources more efficiently
  • Software delivery cycles are faster with it
  • Application portability is possible and easy
  • It is great for the microservices architecture

How to install Docker on Linux Operating System?

  • sudo apt-get update

  • sudo apt-get install docker.io -y

How to check if Docker is installed on a system?

You can use the following command: docker –version

What is Docker Compose?

Docker Compose defines and runs multi-container Docker applications. With Compose, a YAML file is used to configure an application’s services. All the services from the configuration can be created and started with a single command.

What are the drawbacks of Docker?

Docker has a few drawbacks as listed below:

  • No storage option
  • Poor monitoring 
  • Unable to automatically reschedule inactive nodes
  • Has a complicated automatic horizontal scaling setup

 What is Docker Engine?

Docker Engine is an open-source containerization technology that facilitates the development, assembling, shipping, and running of applications with the help of the following components:

  • Docker Daemon
  • Docker Engine REST API
  • Docker CLI

What are registries?

Docker registries provide locations for storing and downloading images. There are two types of registries

  • Public registry
  • Private registry

Public registries include Docker Hub and Docker Cloud.

What are Docker namespaces?

When a container is run, Docker creates a set of isolated workspaces for the container called namespaces. 

Is it possible to use JSON instead of YAML for Docker Compose?

We can use JSON instead of YAML for a Docker Compose file. When we are using the JSON file for composing, we have to specify the filename with the following command:

docker-compose -f docker-compose.json up

What is the process for stopping and restarting a Docker container?

To stop a Docker container, we need to use the following command:

sudo docker stop CONTAINER_ID

To restart a Docker container, we need to use the following command:

docker restart CONTAINER_ID

How to create a Dockerfile?

In order to create a Dockerfile, follow these steps:

  1. Create a new file and name it “Dockerfile”
  2. Write the below script inside this Dockerfile
    FROM ubuntu
    RUN apt-get update
    RUN apt-get install -y apache2
  3. Save this file. This Dockerfile script pulls the Ubuntu base image, updates the package lists, and attempts to install the Apache2 server.

How to create a Docker image?

A Docker image can be built using the following command:

$ docker build . -t <image_name>


How to stop a Docker container?

We can use the following to stop one or more running Docker containers:

docker container stop container-id


How to remove Docker images?

Use docker images with the -a flag to get the image IDs for removal. Then, pass their IDs or tags to

docker rmi <image_name>

List:

docker images -a

Remove:

docker rmi <image_name>


Where are Docker images stored?

It depends on which system the Docker is running and the Docker storage driver is being used.
For example, on Windows, Docker images are stored by default in:
C:\ProgramData\DockerDesktop
On a Mac, Docker images are stored by default in:
~/Library/Containers/com.docker.docker/Data/vms/0/


What is host port and container port?

A host port specifies a port on the host to bind to whereas a container port specifies a port within a container.

 How to run a Docker container?

The Docker run command manages the running of containers in Docker.
Running a container under a specific name:
The command for running a container under a specific name is:
docker container run -it --name [container_name] [docker_image]

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

 

Running a container in the background in the detached mode:

The command for running a container in the background is:

docker container run -it -d [docker_image]

Running a container interactively:

The following command is run for running a container interactively:

docker container run -it [docker_image]

Running a container and publishing container ports:

We have to include -p to the docker run command, along with the following:

-p [host_ip]:[host_port]:[container_port]

Here, host_ip is optional. It is not mandatory to specify this while we run the command.

Running a container and mounting host volumes:

The docker container run command looks like this:

docker container run -v [/host/volume/location]:[/container/storage] [docker_image]

How to start a Docker container?

The following command starts a Docker container:
docker container start container_name

What command should be run to view all the running Docker containers?

To view all the running containers in Docker, we can use the following:

$ docker ps

What is Docker daemon?

Docker daemon is a service that manages Docker containers, images, storage volumes, and the network. It constantly listens to Docker API requests and processes them. A daemon can communicate with other daemons as well for the management of Docker services.

What is Docker Hub?

Docker Hub helps with linking to code repositories. This cloud-based registry enables the building, testing, and storing of images in Docker Cloud. Images can also be deployed to the host with it.

How to push images to DockerHub?

Try the following commands to push the images to your DockerHub repository:

  1. Login to your Dockerhub account using the below command:
    docker login
  2. Enter the Username and the password.
  3. Run: docker push <image_name>
  4. Login to your DockerHub account via browser and verify the repository


What are Docker object labels?

Docker object labels help us add metadata to Docker objects, including containers, images, Swarm nodes, network, volumes, and services.


What are the steps in a Docker container life cycle?

  • Build
  • Pull
  • Run


Explain the implementation method of continuous integration (CI) and continuous deployment (CD) in Docker.

  • Run Jenkins on Docker
  • Using docker-compose, run integration tests in Jenkins


Explain the differences between a Docker registry and a Docker repository.

Answer:

Docker registry is a storage and distribution system for named Docker images. The Docker Hub and Google Container Registry are examples of public registries, but you can also have private ones.

Docker repository is a collection of different Docker images with the same name, distinguished by different tags. For example, in a repository called “debian”, you might have the tags 10, 11, and 12, each corresponding to a different image of the Debian operating system.

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

Docker has several components that work together to provide a platform for packaging, deploying, and running applications in containers. These components include:

1. Docker Engine: The Docker Engine is the underlying technology that runs and manages the containers. It is responsible for creating, starting, stopping, and deleting containers, as well as managing their networking and storage.

2. Docker Daemon: The Docker Daemon is the background service that communicates with the Docker Engine. It receives commands from the Docker CLI and performs the corresponding actions on the Docker Engine.

3. Docker CLI: The Docker Command Line Interface (CLI) is a command-line tool that allows users to interact with the Docker Daemon to create, start, stop, and delete containers, as well as manage images, networks, and volumes.

4. Docker Registries: A Docker Registry is a place where images are stored and can be accessed by the Docker Daemon. Docker Hub is the default public registry, but you can also use private registries like those provided by Google or AWS.

5. Docker Images: A Docker Image is a lightweight, stand-alone, executable package that includes everything needed to run a piece of software, including the code, a runtime, libraries, environment variables, and config files.

6. Docker Containers: A Docker Container is a running instance of an image. It is a lightweight, standalone, and executable software package that includes everything needed to run the software in an isolated environment.


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

1. Docker Compose: Docker Compose is a tool for defining and running multi-container applications. It allows you to define the services that make up your application in a single docker- compose.yml file, and then start, stop, and manage those services with a single command.

2. Dockerfile: A Dockerfile is a script that contains instructions for building a Docker image. It specifies the base image to use, any additional software to install, and any configuration files or environment variables that need to be set.

3. Docker Image: A Docker image is a lightweight, stand-alone, executable package that includes everything needed to run a piece of software, including the code, a runtime, libraries, environment variables, and config files.

4. Docker Container: A Docker container is a running instance of an image. It is a lightweight, standalone, and executable software package that includes everything needed to run the software in an isolated environment. Each container runs in its own namespace and has its own set of processes and network interfaces.

How to implement CI/CD in Docker?

CI/CD (Continuous Integration/Continuous Deployment) in Docker can be implemented by following these steps:

1. Build: Use a continuous integration tool, such as Jenkins, Travis CI or CircleCI, to automatically build a Docker image from the source code whenever there are changes to the codebase. This can be done by using a Dockerfile to define the image and using the CI tool to run the docker build command.

2. Test: Run automated tests on the built image to ensure that the application is functioning correctly. This can be done by starting a container from the image and running the tests inside the container.

3. Push: Push the built image to a Docker registry, such as Docker Hub, so that it can be easily accessed by the deployment environment.

4. Deploy: Use a continuous deployment tool, such as Kubernetes or Docker Swarm, to deploy the image to the production environment. This can be done by pulling the image from the registry and starting a container from it.

5. Monitor: Monitor the deployed containers to ensure that they are running correctly and that there are no issues with the application. This can be done by using tools such as Prometheus or Elasticsearch, Logstash and others.






Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.