Docker Interview Questions: #Day21 of 90DaysofDevOps

Table of contents

Questions:

What is the Difference between an Image, Container and Engine?

Docker Image📷

A Docker image is a static snapshot/blueprint of an application with all needed dependencies.

Docker container📦

A Docker container is a running instance of an image.

Docker Engine🚂

Docker Engine is software that runs to manage containers and images on a host system.

What is the Difference between the Docker command COPY vs ADD?

COPY

Copies files from the host into the container

ADD

Copies files from the host into the container, with added support for URLs and tar extraction.

What is the Difference between the Docker command CMD vs RUN?

CMD

Specifies the default command to run when a container starts.

RUN

Executes commands during image build to set up the environment. It creates a layer in the image.

  • How Will you reduce the size of the Docker image?
    Answer:
    You can reduce the size of the Docker images by following the below steps:

    -Use a smaller base image.

    -Minimize the number of layers.

    -Clean up unnecessary files and dependencies.

  • Why and when to use Docker?

    Answer:

    Docker is used to enable consistent application deployment across different environments.
    Simplifies dependency management and isolation.
    Enhances scalability and resource utilization

    It's particularly useful in Microservices architecture, CI/CD development.

  • Explain the Docker components and how they interact with each other.

    Answer:

    Docker is a platform for developing, shipping and running applications in containers. Its components work together to provide a seamless environment for managing and deploying applications.

    -Docker Deamon: This is the background service responsible for building, running, and managing containers.
    -Docker Clients: The command-line tool or API that interacts with the Docker Daemon.
    -Docker Images: Images are the templates used to create Docker containers.
    -Docker Containers: Containers are instances created from Docker images.
    -Docker Registry: A repository for Docker images.
    -Docker Compose: A tool for defining and running multi-container applications using a YAML file.
    -Docker Networking: Docker provides various network drivers to facilitate ommunication between containers and with the outside world.
    -Docker Volumes: Volumes provide persistent data storage for containers.
    -Dockerfile: A text file containing instructions to build a Docker image.

    In the interaction between these components:
    1. A Docker image is created based on a Dockerfile.
    2. The image is then used to spin up Docker containers.

    3. The Docker Daemon manages the containers, ensuring their isolation and efficient resource utilization.

    4. The Docker Client allows users to issue commands to build, run, stop, or manage containers.

    5. Docker Compose coordinates the deployment of multiple containers using a single configuration file.

    6. Docker Networking and Volumes enable containers to communicate and store data.

  • Explain the terminology: Docker Compose, Docker File, Docker Image, Docker Container?
    Answer:

Docker Compose

Defines and runs multi-container applications.

Docker File

Script for building a Docker image.

Docker Image

Blueprint for creating containers.

Docker Container

Running instance of an image.

  • In what real scenarios have you used Docker?
    Answer:

    I have used Docker for:
    -Development environment replication.
    -Microservices deployment and scaling.

    -Continuous integration and delivery pipelines

  • Docker vs Hypervisor?
    Answer:

Docker

Hypervisor

Docker is more focused on application-level virtualization through containerization, emphasizing lightweight and consistent environments.

Hypervisors create full VMs, providing stronger isolation between different OS environments.

-The choice between Docker and hypervisors depends on the specific requirements of your applications and the level of isolation and resource utilization you need.

  • What are the advantages and disadvantages of using docker?

    Answer:

AdvantageDisadvantage
Consistency and isolation,Limited OS compatibility,
Resource efficiency,Security concerns,
Rapid deployment,No automatic backup and Complexity of orchestration.
Version control and Environment reproducibility
  • What is a Docker namespace?
    Answer: Mechanism to isolate processes, users, and resources in containers.

  • What is a Docker registry?

    Answer: Storage for Docker images, like Docker Hub or private registries.

  • What is an entry point?

    Answer: Default command to execute when a container starts.

  • How to implement CI/CD in Docker?

    Answer:
    -Use Docker images for consistent build and deployment environments.

    -Automate image creation and deployment using tools like Jenkins or GitLab CI/CD.

  • Will data on the container be lost when the docker container exits?
    Answer: Data in a container is lost if not stored in a persistent volume.

  • What is a Docker swarm?

    Answer: Native clustering and orchestration solution for Docker, managing a group of Docker nodes as a single virtual system.

  • What are the docker commands for the following:

    Answer:

    -view running containers: docker ps -a

    -command to run the container under a specific name: docker run --name <container_name> <image_name>

    -command to export a docker: docker export <container_id> > <output_file>.tar

    -command to import an already existing docker image: docker import <input_file>.tar <image_name>:

    -commands to delete a container: docker rm <containerID/name>

    -command to remove all stopped containers, unused networks, build caches, and dangling images?: docker prune -a

  • What are the common Docker practices to reduce the size of Docker Images?

    Answer:
    -Use a Minimal Base Image: Start with a minimal base image, such as Alpine Linux, to reduce the initial image size

    -Multi-Stage Builds: This helps discard unnecessary build dependencies, resulting in a smaller final image.

    -Minimize Layers: Reduce the number of layers in your Dockerfile. Combine multiple RUN commands into a single command.

    -Minimize Image Layers: Avoid installing software, copying files, and running commands in separate layers if they can be combined.

    -Remove Unnecessary Dependencies:
    Avoid installing packages or dependencies that your application doesn't need.

    -Clean Up after installation: Remove temporary files, caches, and unnecessary artifacts after each build step to minimize the size of the final image.

    -Use .dockerignore:Use a .dockerignore file to exclude unnecessary files and directories from being copied into the image during the build process.

    -Use COPY Instead of ADD: Use the COPY command instead of ADD to copy files into the image. COPY only handles local files, which is safer and more predictable.

    -Avoid running unnecessary services:
    Minimize services and processes running in the container, only including what's essential for the application to function.

I hope the above given answers help to crack the Interview easily. HappyLearninig 😊!!!