# Day 21- Demystifying Docker: Essential Interview Questions and Answers  (Dec 17, 2023)

## Introduction

Docker plays a pivotal role in DevOps, making it a crucial topic for interviews, especially for those entering the field. Let's explore key Docker interview questions and equip you with insightful answers.

## 1\. Difference between Image, Container, and Engine

**Answer:**

* **Image:** A blueprint of a file system and parameters needed for a container, often immutable.
    
* **Container:** A runnable instance of an image, encapsulating the application and its dependencies.
    
* **Engine:** The core of Docker, responsible for building, running, and managing containers.
    

## 2\. Docker `COPY` vs `ADD` Command

**Answer:**

* `COPY`: Copies local files into the container. Simple and efficient for straightforward file copying.
    
* `ADD`: More powerful, as it can handle URLs and automatically unpack compressed files.
    

## 3\. Docker `CMD` vs `RUN` Command

**Answer:**

* `CMD`: Specifies default commands to execute when a container starts. It can be overridden at runtime.
    
* `RUN`: Executes commands during the image build process, altering the image itself.
    

## 4\. How to Reduce the Size of the Docker Image?

**Answer:**

* Use a minimal base image.
    
* Combine RUN commands to minimize layers.
    
* Remove unnecessary dependencies and files.
    
* Utilize multi-stage builds.
    

## 5\. Why and When to Use Docker?

**Answer:**

* **Why:** Enables consistent, reproducible environments. Enhances scalability and resource utilization.
    
* **When:** Ideal for microservices architecture, continuous integration, and deployments.
    

## 6\. Explain Docker Components and Interaction

**Answer:**

* **Docker Compose:** Orchestrates multi-container applications.
    
* **Docker File:** A script defining steps to create a Docker image.
    
* **Docker Image:** A snapshot of a file system and application.
    
* **Docker Container:** An instance of a Docker image.
    

## 7\. Real Scenarios Using Docker

**Answer:**

* Streamlining application deployment.
    
* Enabling consistent testing environments.
    
* Facilitating continuous integration and delivery.
    

## 8\. Docker vs Hypervisor

**Answer:**

* **Docker:** Containerization, lightweight, shares host OS kernel.
    
* **Hypervisor:** Virtualization, heavier, each VM has a separate OS kernel.
    

## 9\. Advantages and Disadvantages of Docker

**Answer:**

* **Advantages:** Portability, efficiency, scalability, isolation.
    
* **Disadvantages:** Security concerns, limited GUI support.
    

## 10\. What is a Docker Namespace?

**Answer:**

* Provides isolated workspaces for containers, preventing naming conflicts.
    

## 11\. What is a Docker Registry?

**Answer:**

* Repository for Docker images, often Docker Hub or private registries.
    

## 12\. What is a Docker Entry Point?

**Answer:**

* Specifies the default executable when a container starts.
    

## 13\. How to Implement CI/CD in Docker?

**Answer:**

* Utilize Docker in Jenkins pipelines.
    
* Automate image builds and deployments.
    

## 14\. Will Data on the Container be Lost When the Docker Container Exits?

**Answer:**

* Data in a container's ephemeral storage is lost. Use volumes for persistent data.
    

## 15\. What is a Docker Swarm?

**Answer:**

* Docker's native clustering and orchestration solution for managing multiple containers.
    

## Docker Commands Quick Reference:

* **View Running Containers:**
    

* ```plaintext
    docker ps
    ```
    
* **Run Container Under a Specific Name:**
    
* ```plaintext
    docker run --name my-container
    ```
    
* **Export a Docker Container:**
    
* ```plaintext
    docker export -o container.tar my-container
    ```
    
* **Import an Existing Docker Image:**
    
* ```plaintext
    docker import container.tar my-image
    ```
    
* **Delete a Container:**
    
* ```plaintext
    docker rm my-container
    ```
    
* **Remove All Stopped Containers, Unused Networks, Build Caches, and Dangling Images:**
    

* ```plaintext
    docker system prune -af
    ```
    

## Common Docker Practices to Reduce Image Size:

* **Use a minimal base image.**
    
* **Combine RUN commands.**
    
* **Remove unnecessary dependencies and files.**
    
* **Leverage multi-stage builds.**
