Docker Interview Questions

Docker Interview Questions

·

6 min read

It might be helpful to have a glance at these Docker interview questions I collected, hope it is helpful.

What is Docker?

Docker is an open source application container engine, developed based on Go programming language and open sourced under the Apache2.0 protocol. It lets businesses create, test, and deploy various applications and parcels software into units called containers, makes it easy to create a lightweight, portable self-sufficient container for any application on a single host.

Docker Common Use Cases?

  • Automated packaging and publishing of web applications.

  • Automated testing and continuous integration, release.

  • Deploy and tune databases or other background applications in a service-based environment.

  • Build your own PaaS environment by compiling or extending an existing OpenShift or Cloud Foundry platform from scratch.

What are the advantages of Docker?

Docker enables you to separate your application from your infrastructure so you can deliver software quickly. By taking advantage of Docker’s approach to delivering, testing, and deploying code quickly, you can greatly reduce the delay between writing code and running it in production.

  • Flexible: Even the most complex applications can potentially be containerized.

  • Lightweight: Containers leverage and share the host kernel.

  • Immutable: Container images are immutable.

  • Portable: Can be built locally, deployed to the cloud, and run anywhere.

  • Scalable: Container copies can be added and distributed on-demand.

  • Stackable: Services can be stacked vertically and instantly.

What is the difference between Docker and VM?

By adding the Hypervisor layer, the virtual machine virtualizes virtual hardware such as network card, memory, and CPU, and then builds a virtual machine on it. Each virtual machine has its own system kernel.

Docker containers, on the other hand, isolate resources such as file systems, processes, devices, and networks by means of isolation (namesapce), and then control permissions, CPU resources, etc. through (cgroup), so that containers do not affect each other.

Containers consume less resources. Under the same host, the number of containers that can be created is more than that of virtual machines.
However, the security of virtual machines is slightly better than that of containers, and docker containers share resources such as kernel and file system with the host, and are more likely to be vulnerable to other containers, influence on the host.

Explain Three Core Features of Docker?

  • Image: Docker’s image is the foundation for creating a container, similar to a snapshot of a virtual machine, and can be understood as a read-only template for the Docker container engine.

  • Container: A running instance created from an image, which can be started, stopped, and deleted. Each container created is isolated and invisible from each other to ensure the security of the platform.

  • Registry: A docker registry is a collection of different Docker images with the same name but has different tags. Tags are like a version of that Docker image, for example, v1, v2, v2.1, etc.

How to Modify the Storage Location of Docker?

By default, the storage location of Docker is: /var/lib/docker , to update the default storage location, you need to stop the Docker process:

$ systemctl stop docker

Update /etc/docker/daemon.json config file to the following:

{
  "data-root": "/new/docker/storage/location"
}

Then restart Daemon:

$ systemctl daemon-reload
$ systemctl start docker

What are Commonly Used Docker Commands?

  • docker pull

  • docker push

  • docker info

  • docker inspect

  • docker stop

  • docker start/restart

  • docker rmi

  • docker rm

How to Create a Nginx container?

Use docker run command:

$ docker run -d --name my-nginx -p 8080:80 nginx:latest
1d24755e09ffdacc017f6a1d703bc098d24e56f3dc2cabe069b2551c2074ccd7

How to Enter into Running Container?

You can use the docker exec command, for example:

$ docker exec -it my-nginx bash

What is Process Flow of Running Docker Container?

  • Check whether the specified image exists locally. When the image does not exist, it will be downloaded from the public registry;

  • Use the image to create and start a container;

  • Assign a file system to the container, mount a read-write layer outside the read-only image layer;

  • Configure the network bridge (default mode) from the host;

  • Assigns an IP address in the address pool to the container;

  • Executes the application specified by the user, and the container is terminated after execution.

What are the Docker Network Modes?

  • host: If you use the host network mode for a container, that container’s network stack is not isolated from the Docker host (the container shares the host’s networking namespace), and the container does not get its own IP-address allocated.

  • bridge: It uses a software bridge which allows containers connected to the same bridge network to communicate, while providing isolation from containers which are not connected to that bridge network.

  • container: This mode specifies that newly created containers share a Network Namespace with an existing container, rather than with the host.

  • none: With none mode, the docker container has its own network Namespace, but does not do any network configuration for the Docker container. That is to say, this Docker container has no network card, ip, routing and other information. In this network mode, the container only has the lo loopback network and no other network cards. There is no way to connect to this type of network, but a closed network can ensure the security of the container.

What is Docker’s Data Volume?

A data volume is a special directory used by a container, located within the container. The directory of the host can be mounted on the data volume, and the modification operation of the data volume can be seen immediately, and the updated data will not affect the image, thus realizing the migration of data between the host and the container. The use of data volumes is similar to the mount operation for directories under Linux.

If you need to share some data between containers, the easiest way is to use data volume containers. A data volume container is an ordinary container that provides data volumes for other containers to mount and use.

What’s the Difference Between CMD and EntryPoint?

They both specify programs that execute when the container starts running, with the following differences:

  • CMD commands are ignored by Docker Daemon when there are parameters stated within the docker run command. For example:
$ docker run my-image echo Hello

will replace the CMD line in Dockerfile

  • ENTRYPOINT instructions are not ignored but instead are appended as command line parameters by treating those as arguments of the command. For example:
# Dockerfile
From ubuntu:20.04ENTRYPOINT
["echo", "Hello From ENTRYPOINT"]$ docker build . -t my-ubuntu
$ docker run my-ubuntu
Hello From ENTRYPOINT

If you run with the following parameter:

$ docker run my-ubuntu echo hello Again
Hello From ENTRYPOINT echo hello Again

What is the Difference Between ADD and COPY?

  • ADD: Copies new file, directories or remote URLs from <src> and adds them to the filesystem of the image at <dest>. It can also do local tar extraction.

  • COPY: Copies new files or directories from <src> and adds them to the filesystem of the container at the path <dest>.

COPY is preferred in general, because it is more transparent than ADD . Because image size matters, using ADD to fetch packages from remote URL is strongly discouraged, instead you should use curl or wget .

Thank you!

Checkout my Medium Blog

medium.com/@inchararlingappa

Linkedin

linkedin.com/in/inchara-r-2b2a76213

Instagram

instagram.com/inchara_ramalingappa

YouTube

youtube.com/@DevOpswithIncharaRamalingappa