100 Docker Interview Questions & Answes by DevOps Shack
100 Docker Interview Questions & Answes by DevOps Shack
Docker | Interview
Questions With Answers
1. Docker Basics:
Question 2: Explain the difference between Docker image and Docker container.
Answer: The Docker architecture consists of the Docker Engine, Docker Images,
Docker Containers, Docker Registry, and Docker Compose. The Docker Engine is the
core component responsible for building and running Docker containers. Docker
Images are read-only templates used to create Docker containers. Docker Containers
are instances of Docker images that run applications. The Docker Registry is a
repository for Docker images, and Docker Compose is a tool for defining and
running multi-container Docker applications.
Answer: Docker Networking allows containers to communicate with each other and
with the outside world. Docker creates virtual networks that containers can be
attached to, enabling communication via IP addresses and ports. Docker also
supports various network drivers for different use cases, such as bridge, overlay, and
host networking.
3. Docker Commands:
Answer: Some common Docker commands include docker run (to run a
container), docker build (to build an image), docker pull (to pull an image from a
registry), docker push (to push an image to a registry), docker ps (to list running
containers), docker images (to list available images), docker stop (to stop a
container), and docker rm (to remove a container).
Question 8: Explain the difference between docker run and docker exec.
Answer: docker run is used to create and start a new container from an image,
while docker exec is used to execute commands inside a running container.
Question 9: How can you remove all Docker containers?
Answer: You can remove all Docker containers by running the command docker rm
$(docker ps -aq).
4. Dockerfile:
Answer: A Dockerfile is a text file that contains instructions for building a Docker
image. It specifies the base image to use, the commands to run during the build
process, and any configuration or metadata for the image.
Question 11: Explain the difference between CMD and ENTRYPOINT in a Dockerfile.
Answer: CMD is used to specify the default command to run when a container starts,
while ENTRYPOINT is used to specify the executable that will run when the container
starts. If both are present, ENTRYPOINT will be executed with CMD as its arguments.
Question 12: How can you pass arguments to a Dockerfile during the build process?
Answer: You can use build arguments in a Dockerfile by specifying them with
the ARG instruction and passing values to them using the --build-arg flag with
the docker build command.
5. Docker Networking:
Answer: Docker bridge network is the default network mode, which allows
containers connected to the same bridge network to communicate with each other
using IP addresses. It provides isolation and allows containers to access external
networks through NAT (Network Address Translation).
Question 14: How can you create a custom bridge network in Docker?
Answer: You can create a custom bridge network using the docker network
create command, specifying the --driver bridge option followed by the desired
network name.
Question 15: Explain Docker overlay network.
6. Docker Volumes:
Answer: A Docker volume is a persistent data storage mechanism that allows data to
persist beyond the lifetime of a container. Volumes are used to share data between
containers or to persist data even if the container is stopped or deleted.
Question 17: How can you create a Docker volume?
Answer: You can create a Docker volume using the docker volume
create command, followed by the desired volume name.
Question 18: Explain the difference between Docker bind mounts and volumes.
Answer: Docker bind mounts map a host file or directory to a container file or
directory, allowing the container to access files from the host system. Volumes, on
the other hand, are managed by Docker and provide persistent storage that can be
shared among containers and persists even if the container is removed.
7. Docker Compose:
Answer: Docker Compose is a tool for defining and running multi-container Docker
applications. It uses YAML files to configure the application's services, networks, and
volumes, making it easy to manage complex Docker setups.
Answer: Services in a Docker Compose file are defined under the services section
using YAML syntax, where each service specifies its image, ports, environment
variables, volumes, and other configuration options.
Question 21: Explain the difference between docker-compose up and docker-
compose start.
Answer: docker-compose up creates and starts containers for all services defined in
the Docker Compose file, while docker-compose start starts existing containers that
were previously created but stopped.
8. Docker Security:
Answer: You can improve Docker container security by using official images from
trusted sources, regularly updating images and containers, minimizing the attack
surface by using the principle of least privilege, implementing network segmentation,
using Docker Content Trust to verify image integrity, and monitoring container
activity.
Answer: Docker Content Trust is a security feature that allows you to verify the
authenticity and integrity of Docker images. When enabled, DCT uses cryptographic
signatures to ensure that only trusted images are pulled and run on Docker hosts.
Question 24: How can you scan Docker images for vulnerabilities?
Answer: You can scan Docker images for vulnerabilities using tools like Docker
Security Scanning, Clair, Trivy, or Anchore. These tools analyze the contents of
Docker images and report any known vulnerabilities present in the image's
dependencies.
9. Docker Swarm:
Answer: Docker Swarm is a clustering and orchestration tool provided by Docker for
managing a cluster of Docker hosts. It enables you to deploy, scale, and manage
containers across multiple hosts, providing high availability and load balancing for
containerized applications.
Answer: You can initialize a Docker Swarm by running the command docker swarm
init on a Docker host, which initializes the host as a Swarm manager and generates
a join token that other hosts can use to join the Swarm.
Question 27: Explain the difference between Docker Swarm mode and Docker
standalone mode.
Answer: Docker Swarm mode is a clustering and orchestration feature built into
Docker Engine, allowing you to create and manage a Swarm cluster for deploying
and scaling containers across multiple hosts. Docker standalone mode refers to
running Docker without clustering or orchestration features.
Answer: Docker Content Trust is a security feature that allows you to verify the
authenticity and integrity of Docker images. When enabled, DCT uses cryptographic
signatures to ensure that only trusted images are pulled and run on Docker hosts.
Answer: You can improve Docker container security by using official images from
trusted sources, regularly updating images and containers, minimizing the attack
surface by using the principle of least privilege, implementing network segmentation,
using Docker Content Trust to verify image integrity, and monitoring container
activity.
Answer: Docker security best practices include using least privilege, keeping images
and containers up to date, avoiding running containers as root, using Docker
Content Trust, implementing network segmentation, scanning images for
vulnerabilities, and monitoring container activity.
Answer: You can monitor Docker containers using tools like Docker Stats, Docker
Events, cAdvisor, Prometheus with Grafana, and third-party monitoring solutions.
These tools provide insights into container resource usage, performance metrics, and
health status.
Answer: Docker provides logging drivers that allow you to control how container
logs are handled. Some logging options include the json-file driver
(default), syslog, journald, gelf, fluentd, awslogs, and splunk.
Question 33: How can you view container logs in Docker?
Answer: You can view container logs in Docker using the docker logs command
followed by the container ID or name. Additionally, you can use logging drivers to
redirect container logs to external logging systems for centralized log management.
Answer: Docker image caching is a mechanism used during the build process to
speed up subsequent builds by reusing intermediate layers from previous builds.
When a Dockerfile is built, each instruction produces a layer, and Docker caches
these layers to avoid rebuilding them if the instruction and its context haven't
changed.
Question 35: How can you list all Docker images on a system?
Answer: You can list all Docker images on a system using the docker
images command, which displays a list of all images along with their repository, tag,
and size.
Question 36: Explain Docker image layering.
Answer: Docker Registry is a service for storing and distributing Docker images. It
can be either Docker Hub (public registry) or a private registry like Docker Trusted
Registry (DTR) or self-hosted registry using Docker Registry open-source software.
Question 41: How can you push a Docker image to a registry?
Answer: You can push a Docker image to a registry using the docker push command
followed by the image name and tag, along with the registry URL if it's not Docker
Hub.
Question 42: What is Docker Hub?
Answer: Docker Hub is a cloud-based repository provided by Docker for storing and
sharing Docker images. It hosts millions of public images and allows users to store
and distribute their own images publicly or privately.
Question 43: How can you integrate Docker into a CI/CD pipeline?
Answer: Docker can be integrated into a CI/CD pipeline by using Docker images as
build environments, running tests inside Docker containers, building Docker images
as part of the pipeline, and deploying applications using Docker containers to various
environments.
Question 45: What are some CI/CD tools that support Docker integration?
Answer: Some CI/CD tools that support Docker integration include Jenkins, GitLab
CI/CD, Travis CI, CircleCI, TeamCity, and GitHub Actions. These tools provide native
support for Docker, allowing you to build, test, and deploy applications using Docker
containers.
Answer: Docker development best practices include using Docker Compose for
defining multi-container applications, creating lightweight and single-purpose
images, optimizing Dockerfile layers, using bind mounts or volumes for local
development, and leveraging Docker layer caching.
Answer: You can debug Docker containers by attaching to a running container with
the docker exec command, inspecting container logs, using interactive terminals
(docker run -it), or connecting to the container's network namespace to
troubleshoot network issues.
Answer: Some popular Docker orchestration tools include Docker Swarm (built-in),
Kubernetes, Apache Mesos, Amazon ECS (Elastic Container Service), and Google
Kubernetes Engine (GKE). These tools provide features for automating container
management and scaling.
Question 51: Explain the difference between Docker Swarm and Kubernetes.
Question 52: How can you achieve high availability with Docker Swarm?
Answer: High availability with Docker Swarm can be achieved by running multiple
Swarm manager nodes for redundancy, enabling automatic service rescheduling,
using load balancing for distributing traffic, and implementing health checks to
detect and recover from container failures.
Question 53: What is Docker service scaling?
Answer: Docker service scaling refers to the ability to increase or decrease the
number of replicas of a service running in a Docker Swarm cluster. Scaling allows you
to distribute workload across multiple containers to handle varying levels of traffic
and ensure high availability.
Question 54: How does Docker handle node failures in a Swarm cluster?
Question 56: What are some challenges of deploying microservices with Docker?
Question 57: How can you deploy a multi-container microservices application with
Docker Compose?
Question 59: How can you create an overlay network in Docker Swarm?
Answer: You can create an overlay network in Docker Swarm using the docker
network create command with the --driver overlay option, followed by the
desired network name. This network will be available to all services and containers
within the Swarm cluster.
Question 60: Explain Docker bridge network.
Answer: Docker bridge network is the default network mode in Docker, which
creates a virtual bridge on the host system and assigns IP addresses to containers
connected to that bridge. Containers on the same bridge network can communicate
with each other using IP addresses, and the bridge network provides NAT (Network
Address Translation) for external connectivity.
Answer: Docker Content Trust (DCT) is a security feature that provides cryptographic
verification of image authenticity and integrity. It ensures that only signed and
trusted images are pulled and run on Docker hosts, preventing the execution of
potentially malicious or tampered images.
Answer: Some best practices for securing Docker containers include using official
images from trusted sources, minimizing the attack surface by removing unnecessary
components, updating images and containers regularly, implementing least privilege
principles, using network segmentation, and monitoring container activity.
Question 65: What are the key components of a Docker Swarm cluster?
Answer: The key components of a Docker Swarm cluster include Swarm managers,
which orchestrate and manage the cluster, and Swarm nodes, which execute
containerized tasks. Swarm managers maintain the cluster state, handle scheduling,
and manage worker nodes, while Swarm nodes run containers and report their status
to managers.
Question 66: How can you join a Docker Swarm as a worker node?
Answer: You can join a Docker Swarm as a worker node by running the docker
swarm join command on a host and providing the Swarm manager's address along
with the join token generated by the manager. This command joins the host to the
Swarm cluster as a worker node.
Question 67: What are some popular tools for monitoring Docker containers?
Answer: Some popular tools for monitoring Docker containers include Prometheus,
cAdvisor, Docker Stats, Datadog, New Relic, and Sysdig. These tools provide insights
into container performance, resource usage, and health metrics.
Question 68: How can you collect and centralize Docker container logs?
Answer: You can collect and centralize Docker container logs by configuring logging
drivers in Docker daemon settings or individual container configurations. Logging
drivers can redirect container logs to external logging systems like Elasticsearch,
Splunk, Fluentd, or centralized log management solutions.
Question 69: What is cAdvisor, and how does it monitor Docker containers?
Answer: Docker image layers are the read-only filesystem layers that make up a
Docker image. Each layer represents a change to the filesystem, such as adding or
modifying files. When you pull or build a Docker image, Docker uses these layers to
construct the final image.
Answer: You can optimize Docker image size by using multi-stage builds to reduce
the number of layers, minimizing the number of dependencies and unnecessary files
in the image, removing temporary files and caches, and using smaller base images
like Alpine Linux.
Question 72: What is Docker image caching, and how does it work?
Answer: Docker image caching is a mechanism used during the build process to
speed up subsequent builds by reusing intermediate layers from previous builds.
When a Dockerfile is built, each instruction produces a layer, and Docker caches
these layers to avoid rebuilding them if the instruction and its context haven't
changed.
Answer: Docker Registry is a service for storing and distributing Docker images. It
can be either Docker Hub (public registry) or a private registry like Docker Trusted
Registry (DTR) or a self-hosted registry using Docker Registry open-source software.
Question 74: How can you push a Docker image to a private registry?
Answer: You can push a Docker image to a private registry using the docker
push command, followed by the image name and tag, along with the URL of the
private registry and authentication credentials if required.
Question 75: What are some security considerations when using Docker Registry?
Answer: Some security considerations when using Docker Registry include enabling
access control and authentication, using HTTPS for secure communication, enabling
image signing and verification with Docker Content Trust, scanning images for
vulnerabilities, and monitoring registry activity.
26. Docker in CI/CD Pipeline:
Question 76: How can you integrate Docker into a CI/CD pipeline?
Answer: Docker can be integrated into a CI/CD pipeline by using Docker images as
build environments, running tests inside Docker containers, building Docker images
as part of the pipeline, and deploying applications using Docker containers to various
environments.
Question 77: What are some benefits of using Docker in a CI/CD pipeline?
Answer: Some benefits of using Docker in a CI/CD pipeline include consistent build
environments across different stages of the pipeline, reproducible builds, faster build
times with image caching, simplified dependency management, and easier
deployment and scaling of applications.
Question 78: What are some CI/CD tools that support Docker integration?
Answer: Some CI/CD tools that support Docker integration include Jenkins, GitLab
CI/CD, Travis CI, CircleCI, TeamCity, and GitHub Actions. These tools provide native
support for Docker, allowing you to build, test, and deploy applications using Docker
containers.
Question 80: What are some challenges of deploying microservices with Docker?
Question 81: How can you deploy a multi-container microservices application with
Docker Compose?
Answer: Some popular Docker orchestration tools include Docker Swarm (built-in),
Kubernetes, Apache Mesos, Amazon ECS (Elastic Container Service), and Google
Kubernetes Engine (GKE). These tools provide features for automating container
management and scaling.
Question 84: Explain the difference between Docker Swarm and Kubernetes.
Question 85: How can you achieve high availability with Docker Swarm?
Answer: High availability with Docker Swarm can be achieved by running multiple
Swarm manager nodes for redundancy, enabling automatic service rescheduling,
using load balancing for distributing traffic, and implementing health checks to
detect and recover from container failures.
Answer: Docker service scaling refers to the ability to increase or decrease the
number of replicas of a service running in a Docker Swarm cluster. Scaling allows you
to distribute workload across multiple containers to handle varying levels of traffic
and ensure high availability.
Question 87: How does Docker handle node failures in a Swarm cluster?
Question 88: What are some popular tools for monitoring Docker containers?
Answer: Some popular tools for monitoring Docker containers include Prometheus,
cAdvisor, Docker Stats, Datadog, New Relic, and Sysdig. These tools provide insights
into container performance, resource usage, and health metrics.
Question 89: How can you collect and centralize Docker container logs?
Answer: You can collect and centralize Docker container logs by configuring logging
drivers in Docker daemon settings or individual container configurations. Logging
drivers can redirect container logs to external logging systems like Elasticsearch,
Splunk, Fluentd, or centralized log management solutions.
Answer: Docker Swarm is a clustering and orchestration tool provided by Docker for
managing a cluster of Docker hosts. It enables you to deploy, scale, and manage
containers across multiple hosts, providing high availability and load balancing for
containerized applications.
Answer: You can initialize a Docker Swarm by running the command docker swarm
init on a Docker host, which initializes the host as a Swarm manager and generates
a join token that other hosts can use to join the Swarm.
Question 92: Explain the difference between Docker Swarm mode and Docker
standalone mode.
Answer: Docker Swarm mode is a built-in feature of Docker Engine that provides
clustering and orchestration capabilities, allowing you to create and manage a Swarm
cluster for deploying and scaling containers. Docker standalone mode refers to
running Docker without clustering or orchestration features.
Answer: Docker Content Trust is a security feature that allows you to verify the
authenticity and integrity of Docker images. When enabled, DCT uses cryptographic
signatures to ensure that only trusted images are pulled and run on Docker hosts.
Answer: You can improve Docker container security by using official images from
trusted sources, regularly updating images and containers, minimizing the attack
surface by using the principle of least privilege, implementing network segmentation,
using Docker Content Trust to verify image integrity, and monitoring container
activity.
Answer: Docker security best practices include using least privilege, keeping images
and containers up to date, avoiding running containers as root, using Docker
Content Trust, implementing network segmentation, scanning images for
vulnerabilities, and monitoring container activity.
Answer: You can monitor Docker containers using tools like Docker Stats, Docker
Events, cAdvisor, Prometheus with Grafana, and third-party monitoring solutions.
These tools provide insights into container resource usage, performance metrics, and
health status.
Question 97: What are some logging options available in Docker?
Answer: Docker provides logging drivers that allow you to control how container
logs are handled. Some logging options include the json-file driver
(default), syslog, journald, gelf, fluentd, awslogs, and splunk.
Question 98: How can you view container logs in Docker?
Answer: You can view container logs in Docker using the docker logs command
followed by the container ID or name. Additionally, you can use logging drivers to
redirect container logs to external logging systems for centralized log management.
Answer: Docker bridge network is the default network mode in Docker, which
creates a virtual bridge on the host system and assigns IP addresses to containers
connected to that bridge. Containers on the same bridge network can communicate
with each other using IP addresses, and the bridge network provides NAT (Network
Address Translation) for external connectivity.
Question 100: How can you create a custom bridge network in Docker?
Answer: You can create a custom bridge network using the docker network
create command, specifying the --driver bridge option followed by the desired
network name.
Answer: Docker Compose is a tool for defining and running multi-container Docker
applications. It uses YAML files to configure the application's services, networks, and
volumes, making it easy to manage complex Docker setups.
Answer: Services in a Docker Compose file are defined under the services section
using YAML syntax, where each service specifies its image, ports, environment
variables, volumes, and other configuration options.