Docker Interview Q&A

Docker Interview Q&A

Day 21 of 90daysofdevops

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

  • Image: In Docker, an image is a read-only template or snapshot that contains the application code, runtime environment, libraries, dependencies, and other files required to run a specific application or service. Images are built using a Dockerfile, which defines the instructions to create the image.

  • Container: A container in Docker is a runnable instance of an image. It can be thought of as a lightweight and isolated process that runs on a host machine. Containers are created from images and provide an environment where applications can run consistently across different systems.

  • Docker Engine: The Docker engine, also known as the Docker runtime or Docker daemon, is the core component of Docker that manages the building, running, and distribution of containers. It is responsible for interacting with the host operating system's kernel to create and manage containers. The Docker engine includes the Docker daemon, which runs as a background service, and the Docker CLI (command-line interface) used to interact with the engine.

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

  • COPY and ADD are both Dockerfile instructions that serve similar purposes. They let you copy files from a specific location into a Docker image.

  • COPY takes in an src and destination. It only lets you copy a local file or directory from your host (the machine building the Docker image) into the Docker image itself.

  • ADD lets you do that too, but it also supports 2 other sources. First, you can use a URL instead of a local file/directory. Secondly, you can extract a tar file from the source directly into the destination.

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

  • RUN instruction allows you to install your application and the packages required for it. It executes any commands on top of the current image and creates a new layer by committing the results. Often you will find multiple RUN instructions in a Dockerfile.

  • CMD instruction allows you to set a default command, which will be executed only when you run a container without specifying a command. If the Docker container runs with a command, the default command will be ignored. If Dockerfile has more than one CMD instruction, all but the last CMD instructions are ignored.

How Will you reduce the size of the Docker image?

  • Use Smaller Base Images: Start your Docker image with a minimal and lightweight base image.

  • Minimize Installed Packages: Only include necessary packages and dependencies in your image.

  • Optimize Dockerfile Instructions: Be mindful of the order of instructions in your Dockerfile. Place instructions that are more likely to change frequently toward the end of the file.

  • Combine Run Commands: Combine multiple RUN commands into a single command, using a single shell instance.

  • Use .dockerignore File: Utilize the .dockerignore file to exclude unnecessary files and directories from being copied into the image.

  • Multi-Stage Builds: With multi-stage builds, you can use one stage to build your application and another stage to create the final runtime image.

  • Cleanup Unnecessary Files: Remove temporary and intermediate files after they have served their purpose during the build process.

Why and when to use Docker?

  • Docker enables developers to easily pack, ship, and run any application as a lightweight, portable, self-sufficient container, which can run virtually anywhere.

  • Basic use cases: Pulling images from Docker Hub is also a good solution if your application is basic or standard enough to work with a default Docker image. If the default configuration in these images is acceptable for your needs, then pulling the image can save a lot of time that would otherwise be spent setting up your environment and installing the necessary tools.

  • App isolation: If you want to run multiple applications on one server, keeping the components of each application in separate containers will prevent problems with dependency management.

  • Developer teams: If you have developers working with different setups, Docker provides a convenient way to have local development environments that closely match the production environment, without needing to ssh into a remote box.

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

  • Docker Daemon: listens to Docker API requests and manages Docker objects such as images, containers, networks and volumes.

  • Docker Clients: With the help of Docker Clients**,** users can interact with Docker. Docker client provides a command-line interface (CLI) that allows users to run, and stop application commands to a Docker daemon.

  • Docker Host provides a complete environment to execute and run applications. It comprises the Docker daemon, Images, Containers, Networks, and Storage.

  • Docker Registry stores Docker images. Docker Hub is a public registry that anyone can use, and Docker is configured to use images on Docker Hub by default. You can run your registry on it.

  • Docker Images are read-only templates that you build from a set of instructions written in Dockerfile. Images define both what you want your packaged application and its dependencies to look like and what processes to run when it’s launched.

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

  • Docker Compose: Docker Compose is a tool that allows you to define and manage multi-container applications. It uses a YAML file to specify the services, networks, volumes, and other configurations required for your application's containers.

  • Dockerfile: A Dockerfile is a text file that contains a set of instructions to build a Docker image. It provides a declarative and reproducible way to define the environment, dependencies, and runtime settings for your application.

  • Docker Image: A Docker image is a read-only, executable package that contains everything needed to run a piece of software, including the code, runtime environment, libraries, dependencies, and configuration files. Images are built based on the instructions specified in the Dockerfile.

  • Docker Container: A Docker container is a runtime instance of a Docker image. It is a lightweight and isolated environment that encapsulates an application and its dependencies. Containers are created from Docker images using the docker run command or managed through tools like Docker Compose.

What real scenarios have you used Docker?

  • Application Deployment: Docker allows developers to package their applications and their dependencies into Docker images, which can be deployed as containers on various platforms and environments, such as development, testing, and production servers.

  • Microservices Architecture: Microservice can be packaged as a separate Docker container, enabling independent development, deployment, and scaling of individual services.

  • Continuous Integration and Continuous Deployment (CI/CD): It allows developers to create reproducible build environments by encapsulating the required dependencies and tools within Docker images.

  • Development and Testing Environments: Developers can use Docker to set up consistent and reproducible development environments without worrying about conflicts between dependencies or differences in host machine configurations.

  • Hybrid and Multi-Cloud Deployments: By packaging applications as Docker images, they can be deployed and run consistently on various cloud platforms, on-premises infrastructure, or even locally on developers' machines.

Docker vs Hypervisor?

  • Docker is a containerization platform that allows applications to run in isolated, lightweight containers on a host operating system. Containers share the host's OS kernel, providing efficient resource utilization and fast startup times. Docker is ideal for deploying and scaling applications in a consistent and portable manner.

  • A hypervisor, on the other hand, is a virtualization technology that enables the creation and management of multiple virtual machines (VMs) on a physical server. Each VM runs its own OS, offering full isolation and flexibility but at the cost of increased resource overhead compared to containers. Hypervisors are commonly used for server virtualization and running diverse workloads.

What are the advantages and disadvantages of using docker?

  • Advantages of using Docker:

    1. Portability: Docker allows applications to be packaged as containers, providing portability across different environments, from development to production, and between different hosts and cloud platforms.

    2. Isolation: Containers provide process-level isolation, ensuring that applications and their dependencies are encapsulated and do not interfere with each other.

    3. Resource Efficiency: Docker containers share the host's operating system kernel, resulting in reduced resource overhead compared to virtual machines.

  • Disadvantages of using Docker:

    1. Learning Curve: Docker has a learning curve, especially for those new to containerization. Understanding container concepts, Dockerfile syntax, and container orchestration can require some effort.

    2. Complex Networking: Networking between containers and external systems can be challenging, especially when dealing with advanced network configurations or connecting containers across different hosts.

    3. Persistence: By default, containers are ephemeral, meaning their data is not persisted when the container is stopped. Extra configurations or the use of external storage may be required to manage persistent data.

What is a Docker namespace?

  • A Docker namespace is a feature that provides process isolation within a container. It creates separate namespaces for various resources such as process IDs, network interfaces, file systems, and more.

  • Each namespace operates independently, allowing processes within a container to have their own isolated view of these resources. This isolation prevents conflicts and ensures that processes running inside the container do not interfere with processes in other containers or the host system.

What is a Docker registry?

  • A Docker registry is a centralized repository that stores Docker images. It serves as a distribution mechanism for Docker images, allowing users to upload, download, and share container images.

  • Docker Hub is the default public registry, but private registries can also be set up for secure image storage and distribution within organizations.

What is an entry point?

  • an entry point is a command specified in the Dockerfile that defines the default executable or script to be run when a container is started from the corresponding Docker image. It is the main process that runs inside the container when it is launched.

How to implement CI/CD in Docker?

To implement CI/CD (Continuous Integration/Continuous Deployment) in Docker, you can follow these steps:

  1. Set up a version control system (e.g., Git) to manage your codebase.

  2. Configure a CI/CD tool (e.g., Jenkins, GitLab CI/CD, CircleCI) to monitor the repository for changes.

  3. Create a Dockerfile to define the image build process and environment.

  4. Use the CI/CD tool to build the Docker image, run tests, and push the image to a Docker registry.

  5. Configure deployment pipelines to deploy the Docker image to your target environments (e.g., staging, production) using container orchestration tools like Kubernetes or Docker Compose.

Will data on the container be lost when the docker container exits?

  • By default, data inside a Docker container will be lost when the container exits. Docker containers are designed to be stateless and ephemeral. However, you can use Docker volumes or bind mounts to persist data outside the container, ensuring that it is retained even when the container is stopped or removed.

What is a Docker swarm?

  • Docker Swarm is a native clustering and orchestration tool in Docker that allows you to create and manage a swarm of Docker nodes, forming a distributed system. It enables high availability, load balancing, and scaling of containers across multiple hosts, providing a unified interface to manage a cluster of Docker nodes.

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

  • To reduce the size of Docker images, common practices include using a minimal base image, optimizing Dockerfile instructions by combining commands and cleaning up unnecessary files, using multi-stage builds to discard build dependencies, and compressing and optimizing assets before adding them to the image.

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 save -o <output_file.tar> <image_name>
    
  • command to import an already existing docker image

      docker load -i <input_file.tar>
    
  • commands to delete a container

      docker rm <container_id or container_name>
    
  • command to remove all stopped containers, unused networks, build caches, and dangling images?

      docker system prune -a
    

Thank You,

I want to express my deepest gratitude to each and every one of you who has taken the time to read, engage, and support my journey as a becoming DevOps Engineer.

Feel free to reach out to me if any corrections or add-ons are required on blogs. Your feedback is always welcome & appreciated.

~ Abhisek Moharana 😊