Day 21: Docker Important interview Questions.
Table of contents
- Difference between an Image, Container, and Engine:
- Difference between Docker command COPY vs ADD:
- Difference between Docker command CMD vs RUN:
- How to reduce the size of the Docker image:
- Why and when to use Docker:
- Explanation of Docker components and terminology:
- Real scenarios of using Docker:
- Docker vs Hypervisor:
- Advantages and disadvantages of Docker:
- Docker namespace:
- Docker registry:
- Entry point:
- Implementing CI/CD in Docker:
- Data persistence in Docker containers:
- Docker swarm:
- Common Docker commands:
- Common Docker practices to reduce image size:
In a DevOps interview focused on Docker, concise and precise answers are essential. Let's address each question succinctly:
Difference between an Image, Container, and Engine:
Image: A blueprint for creating containers. It contains all the dependencies needed to run an application.
Container: A runtime instance of an image. It encapsulates the application and its dependencies, isolated from the host system.
Engine: The software that enables building, managing, and running containers. Docker Engine is a popular containerization platform.
Difference between Docker command COPY vs ADD:
COPY: Copies files or directories from the host into the container filesystem.
ADD: Similar to COPY but with additional features like remote URL support and automatic tar extraction, which makes it more versatile.
Difference between Docker command CMD vs RUN:
CMD: Specifies the default command to execute when a container starts. It's often used to define the primary application process.
RUN: Executes commands during image build time. It's typically used for installing dependencies or setting up the environment.
How to reduce the size of the Docker image:
Use multi-stage builds.
Minimize the number of layers.
Remove unnecessary files and dependencies.
Utilize Alpine Linux as a base image for smaller footprint.
Optimize Dockerfile instructions.
Why and when to use Docker:
Docker enables consistent development, shipping, and deployment of applications across different environments.
It's useful in microservices architecture, continuous integration/continuous deployment (CI/CD), and scaling applications.
Explanation of Docker components and terminology:
Docker Compose: Tool for defining and running multi-container Docker applications.
Docker File: Text file containing instructions for building Docker images.
Docker Image: Blueprint for containers, including application code and dependencies.
Docker Container: Runnable instance of a Docker image.
Real scenarios of using Docker:
Development environments.
Continuous integration and deployment pipelines.
Microservices architecture.
Scaling applications.
Docker vs Hypervisor:
- Docker containers share the host OS kernel, making them lightweight and faster to start compared to virtual machines managed by hypervisors.
Advantages and disadvantages of Docker:
Advantages: Portability, scalability, resource efficiency.
Disadvantages: Limited support for Windows environments, security concerns with shared kernel.
Docker namespace:
- A namespace provides isolation for resources within a container, such as network interfaces, process IDs, and filesystem mounts.
Docker registry:
- A centralized repository for storing and distributing Docker images.
Entry point:
- The initial command or script executed when a container starts.
Implementing CI/CD in Docker:
Use Docker images for building, testing, and deploying applications.
Automate Docker image builds and deployments using CI/CD tools like Jenkins or GitLab CI.
Data persistence in Docker containers:
- Data within a container is typically lost when the container exits unless specifically stored in persistent volumes or external storage.
Docker swarm:
- Docker swarm is a clustering and orchestration tool used to manage a cluster of Docker hosts.
Common Docker commands:
View running containers:
docker ps
Run container under a specific name:
docker run --name <name>
Export a Docker image:
docker save
Import an existing Docker image:
docker load
Delete a container:
docker rm
Remove all stopped containers, unused networks, build caches, and dangling images:
docker system prune
Common Docker practices to reduce image size:
Use lightweight base images.
Minimize the number of layers.
Remove unnecessary files and dependencies.
Optimize Dockerfile instructions.