Docker Interview Question
Table of contents
- 1.What is the difference between an Image, Container, and Engine?
- 2.What is the difference between the Docker command COPY vs ADD?
- 3.What is the difference between the Docker command CMD vs RUN?
- 4.How will you reduce the size of the docker image?
- 5.Why and when to use Docker?
- 6.Explain the Docker components and how they interact with each other.
- 7.Explain the terminology: Docker Compose, Docker File, Docker Image, Docker Container?
- 8.In what real scenarios have you used Docker?
- 9.Docker vs Hypervisor?
- 10.What are the advantages and disadvantages of using docker?
- 11.What is a Docker namespace?
- 12.What is a Docker Registry?
- 13.What is an entrypoint?
- 14.Will data on the container be lost when the docker container exits?
- 15.What is a Docker swarm?
- 16.What are the docker commands for the following:
#day21
#90daysdevopschallenge
Before going through the interview questions, if you don't have knowledge about docker or docker-related concepts. Please go through my previous article:
https://devunnatig.hashnode.dev/docker
1.What is the difference between an Image, Container, and Engine?
Docker Image:
Docker Image is a non-editable component, that contains all the instructions to run a container.
Docker Container:
Docker Container is created by using Docker image. It is lightweight, executable software inside that applications are running. It contains everything to run an application.
Docker Engine:
The software used to host the docker container is called Docker Engine.
2.What is the difference between the Docker command COPY vs ADD?
In Docker, the COPY command provides fewer features in comparison to the Docker ADD Command. COPY command just copies your files from local to inside the container same as the ADD command but by using this you can copy tar files inside the container in executable format.
3.What is the difference between the Docker command CMD vs RUN?
CMD instruction in a Dockerfile specifies the default command to run when a container is started from the image. It is used to define what executable or script should be executed when the container starts. RUN is used to execute commands during the image-building process to set up the environment within the image.
4.How will you reduce the size of the docker image?
a) Use a smaller base image
b) Follow the multistage build process, in which you define the build environment and run the environment. After successfully building you have to push only required artifacts, not all.
c) Minimize installed package
d) Optimize image layers
e) Copy only required files
f) Mentioned package version to install particular version.
g) Removed Cached Artifacts
5.Why and when to use Docker?
Why to use docker:
Docker containers provide process and file system isolation. Each container runs in its own environment, making it possible to run applications or services on the same host without interference.
Docker container encapsulates an application and all its dependencies, such as development, testing, and production.
Containers are lightweight and share the host OS kernel, which reduces resource overhead compared to traditional virtualization.
Docker is often used in microservices architecture, It provides the facility to deploy each service separately.
Docker provides security features like container image signing, security scanning, and more.
When to use docker:
Docker is valuable in the development and testing environment to ensure that applications behave consistently across different stages of the development lifecycle.
Docker containers can be integrated into CI/CD pipelines to automate building, testing, and deploying the applications.
If you use docker, portability makes it easy to deploy applications across different cloud providers.
When you want to deploy multiple instances of an application on a single server, use docker.
6.Explain the Docker components and how they interact with each other.
Docker Engine:
The heart of Docker. It includes three main parts.
a) Docker Daemon: Docker Daemon is used to manage all the containers running on the docker host. It takes input in the form of commands or APIs from the client and performs all the operations.
b) REST API: An interface for interaction with the Docker daemon using HTTP requests.
c) CLI(Command Line Interface): A command-line tool for interacting with the Docker daemon.
d) Docker Client: Docker Client is a component, that always interacts end user to perform the operations.
e) Docker Host: Docker Host provides an environment to host our applications and perform all the operations.
f) Docker Registry: It is a collection of docker images, used to create a container.
a) Public Registry
b) Private Registry
g) Docker object:
Docker has four objects:
a) Image
b) Container
c) Networking
d) Storage
7.Explain the terminology: Docker Compose, Docker File, Docker Image, Docker Container?
Docker Compose:
Docker Compose is used to deploy multiple containers in one go. You can set dependency between containers.
Dockerfile:
Dockerfile is used to create Docker Image and Docker Image is used to create the container, inside the container application is running. So Dockerfile contains all the instructions to run an application.
Docker Image:
Docker Image is a blueprint of Docker Container.it is a non-editable file. By using the docker image created a container to deploy an application.
Docker Container:
A Docker Container is created by using a Docker image, inside the container application is running. It is lightweight executable software of packages.
If you want to study more about this, Please go through the below link:
https://devunnatig.hashnode.dev/docker-compose-for-devops-engineer
8.In what real scenarios have you used Docker?
a) Application Deployment:
It is widely used to package applications and their dependencies into containers, ensuring that they run consistently across different environments, from a developer's laptop to production servers.
b) Microservice Architecture:
In a microservices-based application, various components of the application are containerized using Docker. Each microservice can be developed, tested, and deployed independently in its own container.
c) Continuous Integration/Continuous Deployment (CI/CD):
Docker is a key technology in CI/CD pipelines. Developers create Docker images of their applications, which are then tested and deployed automatically through CI/CD tools like Jenkins, Travis CI, or GitLab CI/CD.
d) Testing and QA:
Docker containers can be used to create isolated testing environments for different stages of testing, such as unit testing, integration testing, and user acceptance testing.
9.Docker vs Hypervisor?
Docker and Hypervisor both provide virtualization environment facilities. In docker, created containers for deploying applications. They are using the same HOST OS, So they consume fewer resources. Docker Provides full isolation between running containers. Containers take less time to start and run an application. Docker provides features to deploy microservices which deploy each application in a separate container. Docker Image contains all the dependencies related to applications and docker images are easy to portable.
Hypervisor provides an environment to create virtual machines. Virtual Machine uses their own operating system, so they utilize so many resources, and takes time to start a virtual machine. Virtual Machines provided strong isolation. There is a concept of Migration to move virtual machines but it is so much complex to perform it. It is suitable for complex applications which need stronger isolation.
10.What are the advantages and disadvantages of using docker?
Advantages of Docker :
Portable:
Docker Images are easy to portable. You can move from one environment to another environment easily. Isolated: Docker Containers provide a properly isolated environment to run an application. Containers are isolated from each other.
Microservice Architecture:
Docker has a feature to deploy multi-container applications. All the services run in separate containers and connected to each other.
Rapid Deployment:
Docker containers start up quickly, allowing for rapid scaling, continuous deployment, and efficient resource utilization.
Version Control:
Docker images and containers can be versioned, making it easier to track changes and roll back to previous versions.
Disadvantage of Docker:
Security issue:
There is some security issue or challenge that comes if you don't configure it in a proper way. Docker provides an isolated environment, still, they are still using the same host operating system. so there is always a security risk.
License Cost:
Docker offers both free and paid community editions. The free community edition is also good but sometimes the company needs some advanced feature for that you have to pay.
Networking challenge:
Manage network configuration in docker is a little bit difficult. to give a proper connection between the container and the host.
OS Support:
Docker properly supports Linux but there are some challenges that come when you use it on Windows.
11.What is a Docker namespace?
Docker Namespace is a fundamental component of containerization.
PID Namespace(Process Id Namespace):
Each container has its own separate process_id so whatever process is running inside the container has its own process id.
UTS Namespace (UTSName Namespace):
The UTS namespace is responsible for isolating hostname and domain name identifiers. Each container can have its own hostname and domain name, which are independent of the host and other containers.
Mount Namespace:
Mount namespaces provide filesystem isolation for containers. Each container has its own separate filesystem namespace, allowing it to have its unique root filesystem. This ensures that containers cannot access or modify files outside of their namespace.
Network Namespace:
Network namespaces provide network isolation. Containers in separate network namespaces have their own network interfaces, IP addresses, routing tables, and firewall rules. This isolation allows containers to have their own network configurations without affecting the host or other containers.
12.What is a Docker Registry?
Docker Registry has a collection of docker images. It serves as a storage location for container images that can be easily shared and pulled by Docker users and systems.
There are two types of registries available:
Public registry: Docker Provides its own public repository. From there, anyone can pull any image and create the container. It is free of cost.
Private Registry: Some organizations want their own private registry in which they put their images separately.
13.What is an entrypoint?
Entrypoint as the name suggests, is the starting point or you can say entry point "enter to your office" or any other place.
So in docker, Entrypoint behaves like that only, whenever the container starts firstly execute your entrypoint and you can override that Entrypoint Command.
14.Will data on the container be lost when the docker container exits?
Yes, by default, data stored within a Docker container does not persist when the container exits. When a Docker container stops or exits, any changes or data created or modified inside the container during its runtime are typically lost. This behavior is because Docker containers are designed to be lightweight and ephemeral, and they follow a "containerization" approach, where the container is isolated from the host system and doesn't retain its state by default.
15.What is a Docker swarm?
Docker Swarm is an orchestration tool, that is used to manage the container service. Docker Swarm is a simpler and more lightweight solution that is well-suited for smaller-scale container deployments or scenarios where simplicity is a priority.
16.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=<container_name> <image_name>
command to export a docker
docker export -o <file_name> <container_name>
command to import an already existing docker image
docker save -o <image_name> image_name:tag
commands to delete a container
Step1: firstly check container is running or not
docker ps
Step2: if the container is running, then stop that, otherwise directly follow step3
docker stop <container_id>
Step 3: Now delete it
docker rm <container_id>
command to remove all stopped containers, unused networks, build caches, and dangling images.
docker system prune
docker system prune -f
Docker Cheatsheet:
https://devunnatig.hashnode.dev/docker-cheatsheet
In the Next Article, we will start Jenkins...
Thank you for giving your precious time to read this blog/article and if any suggestions or improvements are required on my blogs feel free to connect on LinkedIn Unnati Gupta. Happy Learning !!!
Subscribe to my newsletter
Read articles from Unnati Gupta directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Unnati Gupta
Unnati Gupta
๐จโ๐ป DevOps Engineer at 6D Technology Passionate about bridging the gap between development and operations, I'm a dedicated DevOps Engineer at 6D Technology. With a strong belief in the power of automation, continuous integration, and continuous delivery, I thrive in optimizing software development pipelines for efficiency and reliability. ๐ Exploring the DevOps Universe In my articles, I delve into the fascinating world of DevOps, where I share insights, best practices, and real-world experiences. From containerization and orchestration to CI/CD pipelines and infrastructure as code, I'm here to demystify the complex and empower fellow developers and ops enthusiasts. ๐ Blogging for Knowledge Sharing As a tech enthusiast and a lifelong learner, I'm committed to sharing knowledge. My articles aim to simplify complex concepts and provide practical tips that help teams and individuals streamline their software delivery processes. ๐ Connect with Me Let's connect and explore the ever-evolving landscape of DevOps together. Feel free to reach out, comment, or share your thoughts on my articles. Together, we can foster a culture of collaboration and innovation in the DevOps community. ๐ Social Links LinkedIn: https://www.linkedin.com/in/unnati-gupta-%F0%9F%87%AE%F0%9F%87%B3-a62563183/ GitHub: https://github.com/DevUnnati ๐ฉ Contact Have questions or looking to collaborate? You can reach me at unnatigupta527@gmail.com Happy Learning!!