Hey Learners! Welcome back. Till now we understood the concepts of docker, docker-compose, docker volumes, docker networking etc. It's time to get prepared for interview questions as docker is a good topic to ask in DevOps Engineer interviews. Let's look for some important interview questions.
1)What is a Docker?
Docker is a very popular and powerful open-source containerization platform that is used for building, deploying, and running applications. Docker allows us to package applications and their dependencies into a container which can be run consistently across different environments.
2) What is Container?
A container is a standard unit of software bundled with dependencies so that applications can be deployed fast and reliably between different computing platforms.
3) What is the difference between an Image, Container and Engine?
Docker Image:- It is a collection of all the files, libraries, packages and dependencies needed to run a software application that can run anywhere without glitches. They are executable packages to create containers. Docker images are immutable, so you can not change them once they are created. For any changes, you need to create a new image.
Docker Container:- Docker container is a virtual environment that bundles application code with all the dependencies required to run the application. Docker containers are just the runtime instances of docker images.
Docker Engine:- It is a core component responsible to create Docker images and to run them as services.
4) What is the difference between the Docker command COPY vs ADD?
Docker COPY:- This command is used to copy files and directories from the host(local) system to the docker container. The instructions can be used only for locally stored files, you can not use URLs to copy external files to your container.
Docker ADD:- This command is also used to copy files and directories into a docker container file system, but it has some additional features. It can automatically decompress files and supports copying files from remote URLs.
5) What is the difference between the Docker command CMD vs RUN?
CMD:- This command is used to set the default command to run when the container starts from an image. It is used to define the primary purpose of the container. This command doesn't create a new layer on the image, but we can use multiple CMD commands to set defaults for an executable.
RUN:- This command is used during the image-building process to execute commands and commit the results. Typically used for installing packages, setting u configurations, and preparing the environment.
6) How Will you reduce the size of the Docker image?
Select the base image with a small size
We can use .dockerignore files to exclude unnecessary files and directories
We can use multi-stage builds to avoid unnecessary artefacts in the final image
7) Why and when to use Docker?
Docker is used to create, deploy, and run applications in isolated environments. It provides consistency across different environments and simplifies deployment. It is valuable when you want to ensure application portability and scalability and maintain a consistent environment throughout the application lifecycle.
8) Explain the Docker components and how they interact with each other.
Docker Daemon:- It receives requests from the client and it is responsible for managing all the Docker objects like images, containers, volumes and networks.
Docker Client:- It communicates with the Docker Daemon through REST API calls to execute any docker command given by the user. It can communicate with multiple docker daemons.
Docker Registry:- It is a storage house for all the docker images. It can be either public or private.
9) Explain the terminology: Docker Compose, DockerFile, Docker Image, Docker Container?
Docker-Compose is a tool used for multi-container applications with the help of a YAML file.
Dockerfile has all the instructions to build a docker image.
A docker image is a collection of all the files, libraries, packages and dependencies needed to run an application that can run anywhere without glitches.
10) In what real scenarios have you used Docker?
To create 2 tier Node JS app with two running containers one for the application and the second for the database(MySQL).
11) Docker vs Hypervisor?
As hypervisor creates and runs virtual machines(VM)s. VMs have a guest OS inside each VM that runs on Host OS. In Docker containers host on a single host OS which shares among them. Sharing the host OS between containers makes them light and increases the boot time. Docker containers are suitable to run multiple applications over a single OS kernel; whereas, VMs are needed if the application or services are required on a different OS.
A hypervisor allows users to generate multiple instances of complete OSs. Dockers can run multiple applications or multiple instances of a single application using containers.
12) What are the advantages and disadvantages of using docker?
Advantages of Docker -
It is lightweight because it does not require any resource pre-allocation (RAM). Whenever it needs resources it acquires them from the host OS. It is of less cost.
Docker enables you to build a container image and use that same image across every step of the deployment process with very less time. You can re-use the image.
It can run on physical hardware, virtual hardware and on the cloud.
Disadvantages of Docker -
It is difficult to manage large amounts of containers.
Docker does not provide cross-platform compatibility means if an application is designed to run in a Docker container on Windows, then it cannot run on a Linux Docker container.
It does not provide any solution for data backup and recovery.
13) What is a Docker namespace?
When we create a container, Docker creates a set of namespaces for that container. These namespaces provide a layer of isolation.
14) What is a Docker registry?
It is a repository for docker images. It's a central location where you can store, share, and manage Docker Images. It can be either public or private. Docker Hub is a popular public registry.
15) What is an entry point?
The docker entry point is a command that is used to specify the executable which should run when a container is started from a docker image.
Example:- Dcokerfile might have the following ENTRYPOINT instruction:-
ENTRYPOINT ["echo", "Hello There!"]
16) How to implement CI/CD in Docker?
Integrate Docker into your CI/CD pipeline by:
Using Docker images as build environments.
Running tests and validations in Docker containers.
Building Docker images for different stages (e.g., development, testing, production).
Pushing images to a Docker registry.
Orchestrating deployments using tools like Kubernetes or Docker Swarm.
17) Will data on the container be lost when the docker container exits?
Data will persist in a container until the container is up. So when the container gets exited data will be lost.
18) What is a Docker swarm?
Docker Swarm is a container orchestration platform that allows you to create and manage a cluster of Docker nodes. It provides features for deploying, scaling, and managing containerized applications across a cluster of machines.
19) What are the docker commands for the following:
view running containers
docker ps
command to run the container under a specific name
docker run --name <specific-name> <image-name>
command to export a docker
docker export <cont-ID> > <name.tar>
command to import an already existing docker image
docker import <name.tar>
commands to delete a container
docker rm <cont-ID> (deletes stopped container)
command to remove all stopped containers, unused networks, build caches, and dangling images?
docker container prune
Hope this question helps you with preparing the interview.
Thank you so much for taking the time to read till the end! Hope you found this blog informative.
Feel free to explore more of my content, and don't hesitate to reach out if need any assistance from me or in case of you have any questions.
Find me on:- HashnodeLinkedInGithub
Happy Learning!