Top 50 Docker Interview Question and Answers
-
What is Docker?
Docker is a containerization platform that packages applications and dependencies into lightweight, portable containers that run consistently across various environments. -
What is a Docker container?
A container is a lightweight, portable, and self-contained package that includes everything needed to run an application, ensuring consistency across different environments. -
How does Docker differ from virtual machines (VMs)?
Unlike VMs, Docker containers share the host OS kernel, making them more lightweight and faster to start than VMs, which need a separate OS instance. -
What is Docker Hub?
Docker Hub is a public repository for Docker images, allowing users to share and access pre-built images. -
What is a Dockerfile?
A Dockerfile is a text file with instructions to build a Docker image. It defines the base image, dependencies, commands, and other configurations for the image. -
Explain the concept of an image in Docker.
A Docker image is a read-only template that contains the application and its dependencies, used to create containers. -
How do you create a Docker image?
Use thedocker build
command with a Dockerfile to create an image. -
What is the difference between
docker run
anddocker start
?
docker run
creates and starts a new container, whiledocker start
restarts an existing stopped container. -
Explain the purpose of the
docker-compose
tool.
Docker Compose is used to define and manage multi-container Docker applications with a single YAML configuration file (docker-compose.yml
). -
What is the purpose of a Docker namespace?
Namespaces provide isolation to containers in terms of processes, network, and file systems. -
How do you list all running Docker containers?
Usedocker ps
to list running containers. -
How do you stop a Docker container?
Usedocker stop <container_id>
to stop a container. -
How do you remove a Docker container?
Usedocker rm <container_id>
to remove a container. -
How do you remove a Docker image?
Usedocker rmi <image_id>
to remove an image. -
Explain
docker pull
anddocker push
.
docker pull
downloads an image from Docker Hub, whiledocker push
uploads an image to a repository. -
How do you check the logs of a Docker container?
Usedocker logs <container_id>
to view container logs. -
How do you execute a command inside a running container?
Usedocker exec -it <container_id> <command>
to run a command inside a container. -
What is the
docker inspect
command used for?
docker inspect
provides detailed information about a container or image, such as configuration and state. -
How do you restart a Docker container?
Usedocker restart <container_id>
to restart a container. -
How do you tag a Docker image?
Usedocker tag <source_image> <target_image>
to add a tag to an image. -
What is Docker networking?
Docker networking allows communication between containers within a host and across multiple hosts. -
What are the different Docker network types?
Bridge, host, overlay, and macvlan. -
What is the default network in Docker?
Bridge network is the default network for Docker containers. -
How do you connect a container to a specific network?
Usedocker network connect <network_name> <container_id>
to connect a container to a network. -
What is Docker Compose networking?
Docker Compose automatically creates a network for all services in thedocker-compose.yml
file, allowing containers to communicate. -
How do you expose ports in Docker?
Use the-p
or--publish
option indocker run
to map container ports to the host. -
What is the purpose of
docker network create
?
docker network create
is used to create a custom network for Docker containers. -
How do containers in a bridge network communicate?
Containers communicate through the bridge network’s IP addressing, isolating traffic from other networks. -
Explain the overlay network in Docker Swarm.
Overlay networks enable communication between Docker services across multiple hosts in a swarm. -
What is a macvlan network?
Macvlan allows containers to appear as physical devices on the network, providing unique MAC addresses for each container. -
What is a Docker volume?
A Docker volume is a persistent storage mechanism that allows data to be shared and retained independently of container lifecycles. -
How do you create a Docker volume?
Usedocker volume create <volume_name>
to create a volume. -
How do you mount a volume in Docker?
Use-v <volume_name>:<container_path>
when running a container. -
Explain bind mounts in Docker.
Bind mounts map directories from the host system directly into the container, providing a way to share data between them. -
How do you remove a Docker volume?
Usedocker volume rm <volume_name>
to remove a volume. -
What is the difference between volumes and bind mounts?
Volumes are managed by Docker and stored separately, while bind mounts reference specific host directories. -
Can you use multiple volumes in a single container?
Yes, multiple volumes can be attached to a container using the-v
option. -
How does Docker handle data persistence?
Data can persist in Docker through volumes or by using external storage solutions. -
What is the purpose of
docker volume inspect
?
It provides details about a specific volume, such as its mount point and configuration. -
Explain the concept of tmpfs mount in Docker.
A tmpfs mount is a temporary file system in memory, useful for storing non-persistent data in containers. -
What is Docker Swarm?
Docker Swarm is Docker’s native clustering and orchestration tool, used for managing containerized applications across a cluster. -
How do you initialize a Docker Swarm?
Usedocker swarm init
on the manager node to initialize a swarm. -
What are Swarm services?
Services are tasks that run containers across swarm nodes, managed by Docker Swarm. -
What is the difference between Swarm and Kubernetes?
Swarm is Docker’s native orchestration, simpler but less feature-rich, while Kubernetes is an advanced orchestration tool with complex features and broader adoption. -
How do you add a node to a Docker Swarm?
Usedocker swarm join
with the token provided by the manager node. -
What is the purpose of
docker service
commands in Swarm?
docker service
commands create, update, and manage services in a swarm. -
How does scaling work in Docker Swarm?
Scaling increases or decreases the number of service replicas across the cluster. -
What are Docker Swarm nodes?
Nodes are machines in the swarm cluster; they can be managers or workers. -
How does Docker handle container health checks?
Docker checks the health status defined in the Dockerfile ordocker-compose.yml
to restart or manage unhealthy containers. -
What are labels in Docker Swarm?
Labels are metadata used to organize and manage resources in Swarm, like services and containers.