Docker is an open-source software development platform that runs ” containers ” software packages. A container is a standard unit of software that packages up all the dependencies of an application so that the application runs quickly and reliably from one computing environment to another. Docker interview questions and answers.
Master the top questions and answers and crack your following interview. Our interview questions on Docker cover various topics like container, Docker registry, fundamental components of a typical docker architecture, and the different major network drivers supported in the Docker ecosystem. This ultimate list of expert-curated Docker container interview questions and answers can be the gateway to your next job as a DevOps engineer, or a Java developer. Prepare well and leave the best impression on your employers.
1. What is Docker?
Docker is an open platform for developing, shipping, and running applications. Docker enables you to separate your applications from your infrastructure so you can deliver software quickly. With Docker, you can manage your infrastructure in the same ways you manage your applications. By taking advantage of Docker’s methodologies for shipping, testing, and deploying code quickly, you can significantly reduce the delay between writing code and running it in production.
2. What is a Container?
A container is a standard unit of software that packages up code and all its dependencies so the application runs quickly and reliably from one computing environment to another. A Docker container image is a lightweight, standalone, executable package of software that includes everything needed to run an application: code, runtime, system tools, system libraries, and settings.
3. What Are the Benefits of Docker Container?
The benefits of Docker in building and deploying applications are many:
- Caching a cluster of containers
- Flexible resource sharing
- Scalability – many containers can be placed in a single host
- Running your service on hardware that is much cheaper than standard servers
- Fast deployment, ease of creating new instances, and faster migrations.
- Ease of moving and maintaining your applications
- Better security, less access needed to work with the code running inside containers, and fewer software dependencies
4. 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
5. 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
6. What are Real-World Ways to Use Docker?
- Simplifying Configuration
- Code Pipeline Management
- Developer Productivity
- App Isolation
- Server Consolidation
- Debugging Capabilities
- Multi-tenancy
- Rapid Deployment
7. What is a Virtual Machine?
A virtual machine is a system that acts exactly like a computer.
In simple terms, it makes it possible to run what appears to be many separate computers on hardware, that is one computer. Each virtual machine requires its underlying operating system, and then the hardware is virtualized.
8. What is the difference between Virtual Machine and Docker?
Virtual Machine | Docker Container |
---|---|
Hypervisor Level | Docker Engine Layer |
Hardware-level process isolation | OS level process isolation |
Each VM has a separate OS | Each container can share OS |
Boots in minutes | Boots in seconds |
VMs are of few GBs | Containers are lightweight (KBs/MBs) |
Ready-made VMs are difficult to find | Pre-built docker containers are easily available |
VMs can move to new hosts easily | Containers are destroyed and re-created rather than moving |
Creating VM takes a relatively longer time | Containers can be created in seconds |
More resource usage | Less resource usage |
9. Does Docker run on Linux, macOS, and Windows?
You can run both Linux and Windows programs and executables in Docker containers. The Docker platform runs natively on Linux (on x86-64, ARM, and many other CPU architectures) and on Windows (x86-64). Docker Inc. builds products that let you build and run containers on Linux, Windows, and macOS.
10. What is DockerHub?
DockerHub is a cloud-based registry service that allows you to link to code repositories, build your images and test them, store manually pushed images, and link to the Docker cloud so you can deploy images to your hosts. It provides a centralized resource for container image discovery, distribution and change management, user and team collaboration, and workflow automation throughout the development pipeline.
11. What is a Docker image?
A Docker image is a file used to execute code in a Docker container. Docker images act as a set of instructions to build a Docker container, like a template. Docker images also act as the starting point when using Docker. An image is comparable to a snapshot in virtual machine (VM) environments.
Docker is used to creating, run and deploy applications in containers. A Docker image contains application code, libraries, tools, dependencies, and other files needed to make an application run. When a user runs an image, it can become one or many instances of a container.
12. What is Dockerfile?
Docker builds images automatically by reading the instructions from a text file called Dockerfile. It contains all commands, in order, needed to build a given image. A Dockerfile adheres to a specific format and set of instructions which you can find here.
13. What is a Docker Hub?
We can think of Docker Hub as a cloud registry that lets us link the code repositories, create the images, and test them. We can also store our pushed images, or we can link to the Docker Cloud so that the images can be deployed to the host. We have a centralized container image discovery resource that can be used for the collaboration of our teams, automating the workflow and distribution, and changing management by creating the development pipeline.
14. What is a Docker Swarm?
We can think of a Docker Swarm as the way of orchestrating the Docker containers. We will be able to implement Dockers in a cluster. We can convert our Docker pools into a single Docker Swarm for easy management and monitoring.
15. Name some important Docker commands
Below are some important Docker commands:
- build: to build an image file for Docker
- create: for the creation of a new container
- kill: to kill a container
- dockerd: for launching Docker daemon
- commit: for creating a new image from the container changes
16. 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
17. 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.
18. Name and explain the states of a Docker container.
- Created: We see this Docker container state when a container is newly created.
- Restarting: When the Docker container is restarted due to any issues, this state is observed.
- Running: It is the main state for the container after it has started.
- Paused: When a running Docker container is temporarily stopped via docker pause, this is the status that we will see.
- Exited: If a container has stopped due to some issue or stopped manually, this will be the state of the container.
- Dead: When the daemon has tried but failed to stop a container (mostly because of a busy device or resource), this state will be seen.
19. Define Virtualization.
Virtualization is the process of logically dividing mainframes to enable more than one application to run at a time.
20. What is a Hypervisor?
A hypervisor helps in the creation of a virtual environment, in which the guest virtual machines run. It manages the guest systems and checks the required resource allocations to the guests.
21. What are Docker object labels?
Docker object labels help us add metadata to Docker objects, including containers, images, Swarm nodes, network, volumes, and services.
22. What are the steps in a Docker container life cycle?
- Build
- Pull
- Run
23. Name the three components of the Docker architecture.
- Client
- Docker host
- Registry
24. How do you scale your Docker containers?
Docker containers can be scaled to any level, starting from a few hundred to even thousands or millions of containers. The only condition is that the containers need the memory and the OS all the time, and there should not be a constraint on these when the Docker is getting scaled.
25. How does communication happen between the Docker client and the Docker daemon?
The communication between the Docker client and the Docker daemon happens with the help of the combination of TCP, Rest API, and Socket.
26. 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
Advanced Interview Questions
27. How to use Docker?
The Docker command syntax looks like this:
docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
To run a container, we must incorporate the image on which it is based:
docker run [docker_image]
We can run containers if the Docker images are locally stored. If they are not, the software will take it from the online registry.
Docker can be used to run a container:
- Under a specific name
- Interactively
- And publish container ports
- In the background in the detached mode
- And mount host volumes
- And remove it once the process is complete
28. 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
29. What is the process for creating a Docker container?
We can use any specific Docker image for creating a Docker container using the below command:
docker run -t -i command name
This command not only creates the container but also starts it for us. If we want to check whether the Docker container has been created or not, then we need to have the following command that will list all the Docker containers, along with the host on which the Docker containers run:
docker ps -a
30. What is the process for stopping and restarting a Docker container?
To stop a Docker container, we need to use the following command:
docker stop CONTAINER_ID
To restart a Docker container, we need to use the following command:
docker restart CONTAINER_ID
31. How to create a Docker image?
A Docker image can be built using the following command:
$ docker build .
32. How to stop a Docker container?
We can use the following to stop one or more running Docker containers:
docker container stop [OPTIONS] CONTAINER [CONTAINER...]
33. 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:
List:
docker images -a
Remove:
docker rmi Image Image
34. 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/
35. How to run a Docker image?
The ‘docker run’ command runs an image inside the container. It will require the image name.
docker run [docker_image]
36. How to start a Docker container?
The following command starts a Docker container:
docker container start [OPTIONS] CONTAINER [CONTAINER...]
37. How to use Docker Compose?
Docker Compose typically includes a three-step process:
- Using a dockerfile to define the app’s environment to facilitate reproduction anywhere
- Defining the app services in docker-compose.yml so that they can run in an isolated environment together
- Running docker-compose up
38. 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
39. 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 --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 -d [docker_image]
Running a container interactively:
The following command is run for running a container interactively:
docker container run -it [docker_image] /bin/bash
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]
40. Explain the login procedure to Docker Repository?
To log in to the Docker repository, use the following command:
docker login [OPTIONS] [SERVER]
For example, to login to a self-hosted (local) registry, you can add the server name:
$ docker login localhost:8080