Docker Important Interview Questions
Docker Important Interview Questions
An image is a package that contains all the necessary components to run software. It is similar to a
snapshot of a container. A container is a running instance of an image. It acts like a virtual machine
that executes the software specified in the image. The engine is the part that handles the
containers. It is responsible for starting, stopping, and managing the containers.
The COPY command is used to copy files or directories from the build context (the directory where
the Dockerfile is located) into the container during the build process. The ADD command is also
used for copying files or directories from the build context into the container during the build
process, but it additionally allows you to specify a different name for the file or directory in the
container.
The CMD command is used to set a default command to run when a container starts. The RUN
command is used to execute a command during the container build process. It is commonly used to
configure the container environment or install software.
Use the Dockerfile's BUILD instruction to only include the necessary files and directories in the
image.
Use the Dockerfile's COPY instruction to copy only the necessary files and directories into the
image.
Use the Dockerfile's ENTRYPOINT instruction to set the default command that should be run
when the container is started.
Use the Dockerfile's EXPOSE instruction to expose a port that should be accessible from
outside the container.
Benefits of Docker:
Portability: Docker containers are portable and can be run on any Docker-supported host,
regardless of the operating system or architecture.
Flexibility: Docker facilitates the creation, modification, and deletion of containers as required.
💡 Explain the Docker components and how they interact with each other.
Docker Engine: This manages the containers. It runs, stops, and manages them.
Docker Hub: This is a cloud-based registry for storing and sharing Docker images.
Docker Compose: This tool helps define and run multi-container Docker applications.
Docker File: This is a text file with instructions for building a Docker image.
Docker Image: This is a lightweight, standalone, and executable package that has everything
needed to run software.
💡 Explain the terminology: Docker Compose, Docker File, Docker Image, Docker
Container?
Docker Compose: This is a tool that allows you to define and run multi-container Docker
applications. It provides a YAML configuration file that defines the services, networks, and volumes
for the application.
Docker Image: This is a lightweight, standalone, and executable package that includes everything
needed to run a piece of software. It's like a snapshot of a container.
Docker Container: This is a runtime instance of a Docker image. It's like a virtual machine that runs
the software defined in the image.
Deploying microservices
💡 Docker vs Hypervisor?
Docker and hypervisors are both used to run isolated environments, but they differ in their approach
and functionality:
Docker utilizes the host operating system's kernel and runs the containers in user space.
Hypervisors, on the other hand, employ a separate kernel and run the virtual machines in kernel
space.
Docker is lightweight and efficient compared to hypervisors, but it is also less secure. Hypervisors
offer better isolation and security, but they are more resource-intensive.
Advantages:
Portability: Docker containers can be moved and run on any host that supports Docker.
Flexibility: Docker allows you to easily create, modify, and delete containers as needed.
Disadvantages:
Security: Docker is less secure than hypervisors because it runs in the user space.
Performance: Docker containers may not perform as well as regular virtual machines.
Complexity: Docker can be complicated to set up and manage, especially for large-scale
applications.
A Docker namespace is a method to group related Docker objects, like containers, images, and
networks, into one unit. This helps you manage and separate the objects more easily.
A Docker registry is a central location where Docker images are stored. It allows you to store, share,
and manage Docker images.
An entry point is a command that is run when a Docker container is started. It's specified in the
Dockerfile using the ENTRYPOINT instruction.
To implement CI/CD in Docker, you can use tools like Jenkins, Travis CI, or CircleCI to automate the
build, test, and deployment process. These tools allow you to define workflows that include steps for
building Docker images, running tests, and deploying the images to a registry.
💡 Will data on the container be lost when the docker container exits?
A Docker swarm is a cluster of Docker nodes that work together to run and manage Docker
containers. It allows you to scale your applications horizontally and manage the containers more
efficiently.
Run a container under a specific name: docker run -d --name <name> <image>
Remove all stopped containers, unused networks, build caches, and dangling images: docker system prune -a
💡 What are the common docker practices to reduce the size of Docker Image?
Using the Dockerfile's BUILD instruction to only include the necessary files and directories in the
image.
Using the Dockerfile's ENTRYPOINT instruction to set the default command that should be run
when the container is started.
Using the Dockerfile's VOLUME instruction to mount a volume that contains data that should be
preserved across container restarts.
Using the Dockerfile's EXPOSE instruction to expose a port that should be accessible from
outside the container.