Docker Important interview Questions with answers | #Day 21 | # 90daysofdevops
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 entry point?
- 14. How to implement CI/CD in Docker?
- 15. Will data on the container be lost when the docker container exits?
- 16. What is a Docker swarm?
- 17. Docker Commands:
1. What is the difference between an Image, Container, and Engine?
Image: An image is a lightweight, standalone, executable package that includes everything needed to run a piece of software, including the code, a runtime, libraries, environment variables, and config files. It's a static snapshot of a system at a specific point in time.
Example: A Docker image for a web server contains the web server software, its dependencies, and the configuration files.
Container: A container is a running instance of an image. It encapsulates the application along with its environment and dependencies, ensuring consistent behavior across different environments. Containers are isolated from each other and share the host system's kernel.
Example: You can run multiple instances of the same web server image in separate containers, each with its own isolated environment.
Engine: The Docker Engine is the underlying software that enables the creation and management of containers. It's responsible for building, running, and distributing containers.
2. What is the difference between the Docker command COPY vs ADD?
COPY: The
COPY
command is used in a Dockerfile to copy files or directories from the host machine to the container's filesystem.Example:
bashCopy codeCOPY ./src /app/src
ADD: The
ADD
command is similar toCOPY
, but it has some extra features. It can also handle URLs and can automatically extract compressed files.Example:
bashCopy codeADD https://example.com/file.tar.gz /tmp/
3. What is the difference between the Docker command CMD vs RUN?
RUN: The
RUN
command is used in a Dockerfile to execute commands during the build process. It is used to install packages, set up configurations, etc.Example:
kotlinCopy codeRUN apt-get update && apt-get install -y package-name
CMD: The
CMD
command sets the default command to be executed when a container is run from the image. It can be overridden when running the container.Example:
cssCopy codeCMD ["python", "app.py"]
4. How Will you reduce the size of the Docker image?
To reduce the size of a Docker image, you can consider the following practices:
Use a lightweight base image.
Avoid unnecessary packages and dependencies.
Clean up after installation (e.g., remove cache and temporary files).
Use multi-stage builds for complex applications.
Minimize layers by combining commands where possible.
Utilize
.dockerignore
to exclude unnecessary files from being included.
5. Why and when to use Docker?
Docker is used for containerization, which provides a consistent environment for applications to run regardless of the underlying infrastructure. It is beneficial for:
Ensuring consistent development, testing, and production environments.
Simplifying application deployment and scaling.
Isolating applications and dependencies, improving security.
Enabling CI/CD pipelines for efficient software delivery.
Optimizing resource utilization by running multiple containers on a single host.
6. Explain the Docker components and how they interact with each other.
Docker Engine: Responsible for building, running, and distributing containers.
Docker Images: Immutable snapshots containing the application and its dependencies.
Docker Containers: Running instances of Docker images.
Docker Registry: Storage for Docker images (e.g., Docker Hub).
Dockerfile: Instructions for building Docker images.
Docker Compose: Tool for defining and running multi-container applications.
These components work together to build, deploy, and manage containerized applications.
7. Explain the terminology: Docker Compose, Docker File, Docker Image, Docker Container?
Docker Compose: A tool for defining and running multi-container Docker applications. It uses a YAML file to configure the services, networks, and volumes.
Dockerfile: A text file containing a set of instructions for building a Docker image. It specifies the base image, dependencies, and steps needed to create the final image.
Docker Image: A lightweight, standalone, executable package that includes everything needed to run a piece of software.
Docker Container: A running instance of a Docker image, containing the application and its environment.
8. In what real scenarios have you used Docker?
(You would answer based on your actual experience if you were a DevOps Engineer.)
9. Docker vs Hypervisor?
Docker: Docker uses containerization to run applications in isolated environments, sharing the host OS kernel. It's lightweight and provides efficient resource utilization.
Hypervisor: Hypervisors run virtual machines (VMs), which emulate hardware to host multiple guest OS on a single physical machine. It provides strong isolation but can be more resource-intensive compared to containers.
10. What are the advantages and disadvantages of using Docker?
Advantages:
Portability: Consistent environments across different systems.
Isolation: Applications and dependencies are isolated.
Resource Efficiency: Lightweight compared to VMs.
Scalability: Easy to scale horizontally.
CI/CD Integration: Facilitates automated software delivery pipelines.
Disadvantages:
Limited OS Support: Containers share the host OS kernel, which can be limiting in certain cases.
Security Concerns: Containers must be properly configured to prevent security breaches.
Learning Curve: Initial setup and understanding can be challenging for beginners.
11. What is a Docker namespace?
A Docker namespace is a mechanism that isolates system resources, such as processes and file systems, between containers. Namespaces provide the illusion that each container has its own isolated environment.
12. What is a Docker registry?
A Docker registry is a repository for storing and distributing Docker images. It allows users to share and manage Docker images. Docker Hub is a popular public registry, but private registries can also be set up for internal use.
13. What is an entry point?
The entry point is a command or script specified in a Dockerfile that defines what should run when a container starts. It's the default executable for the container.
Example:
cssCopy codeENTRYPOINT ["python", "app.py"]
14. How to implement CI/CD in Docker?
CI/CD in Docker involves automating the build, test, and deployment processes of containerized applications. This is typically done using CI/CD tools (e.g., Jenkins, GitLab CI) and Docker registries.
15. Will data on the container be lost when the docker container exits?
Yes, by default, any data written inside a container is not persisted when the container exits. To preserve data, you should use Docker volumes or bind mounts to store data outside the container.
16. What is a Docker swarm?
Docker Swarm is Docker's native orchestration tool used for managing a cluster of Docker hosts. It allows you to deploy, scale, and manage a group of containers across multiple machines.
17. Docker Commands:
View running containers:
docker ps
Run a container with a specific name:
docker run --name my-container image-name
Export a Docker container:
docker export container-id > exported-container.tar
Import an already existing Docker image: `docker import exported-container