Docker_Interview_Questions
Docker_Interview_Questions
1. What is Docker?
2. What is a container?
You can create a Docker image using a Dockerfile, which is a text file that
contains a set of instructions for building the image.
6. What is a Dockerfile?
In Docker, you can link containers using the --link flag or by creating a
user-defined network and connecting containers to the network.
You can share Docker images with others by pushing them to a Docker
registry, such as Docker Hub or a private registry. Others can then pull the
image from the registry to use it.
You can access logs from a Docker container using the docker logs
command followed by the container ID or name.
A Docker image is a static, read-only file that contains the application and
its dependencies, while a Docker container is a running instance of an
image that has its own state and can be started, stopped, and managed.
Piyush Dwivedi 💫💫
20. How do you remove Docker images and containers?
You can remove Docker images using the docker rmi command followed
by the image ID or name. Containers can be removed using the docker
rm command followed by the container ID or name.
You can list Docker containers using the docker ps command. The -a flag
can be used to display all containers, including the ones that are not
currently running.
The COPY instruction in a Dockerfile copies files and directories from the
build context to the image. The ADD instruction can do the same but also
supports additional features like extracting tar archives and downloading
files from URLs.
You can clean up unused Docker resources using the docker system
prune command. This command removes unused images, containers,
volumes, and networks, freeing up disk space. Use it with caution, as it
permanently deletes resources.
● Answer: A Docker context is the set of files and directories that are
accessible to the Docker daemon during the build process. It is
typically specified as the directory path passed to the docker build
command.
43. How can you troubleshoot a Docker container that won't start?
● Answer: Docker health checks are used to determine the health status
of a container. They are defined in the Dockerfile using the
HEALTHCHECK instruction and can specify a command that Docker
runs to check if the container is healthy.
● Answer: Bind mounts directly mount a host directory or file into the
container, allowing for direct access to host files. Volumes are
managed by Docker and provide a more flexible and portable way to
persist data.
51. What is the purpose of Docker's --rm option in the docker run
command?
52. How can you limit the memory usage of a Docker container?
● Answer: You can limit the memory usage of a Docker container using the -m
or --memory flag followed by the memory limit when running the docker
run command, for example, docker run -m 512m <image>.
54. How can you limit the CPU usage of a Docker container?
● Answer: You can limit the CPU usage of a Docker container using the
--cpus flag followed by the number of CPUs, for example, docker run
--cpus 2 <image>.
● Answer: You can view the details of a Docker service using the docker
service inspect command followed by the service name or ID. This
command provides detailed information about the service configuration and
status.
● Answer: You can remove a Docker service using the docker service rm
command followed by the service name or ID.
● Answer: You can create a Docker network using the docker network
create command followed by the network name. You can specify the
network driver and other options as needed.
● Answer: You can disconnect a container from a Docker network using the
docker network disconnect command followed by the network name
and container ID or name.
● Answer: You can list all Docker networks using the docker network ls
command.
68. How can you start services defined in a Docker Compose file?
● Answer: You can start services defined in a Docker Compose file using the
docker-compose up command. This command starts and runs the services
as specified in the docker-compose.yml file.
69. How can you stop and remove services started by Docker Compose?
● Answer: You can stop and remove services started by Docker Compose
using the docker-compose down command. This command stops the
services and removes the containers, networks, and volumes created by
docker-compose up.
70. What is the purpose of Docker's --build-arg option in the docker build
command?
Piyush Dwivedi 💫💫
● Answer: The --build-arg option in the docker build command allows
you to pass build-time variables to the Dockerfile. These variables can be
used in the Dockerfile to customize the build process.
● Answer: You can tag a Docker image using the docker tag command
followed by the image ID or name and the new tag name, for example,
docker tag <image_id> myimage:latest.
● Answer: You can push a Docker image to a registry using the docker push
command followed by the image name and tag, for example, docker push
myimage:latest.
73. What is the purpose of Docker's --detach or -d option in the docker run
command?
● Answer: The --detach or -d option in the docker run command runs the
container in the background and prints the container ID, allowing the terminal
to be used for other commands.
● Answer: You can attach to a running Docker container using the docker
attach command followed by the container ID or name. This allows you to
interact with the container's standard input, output, and error streams.
76. How can you run a Docker container with a specific hostname?
● Answer: You can run a Docker container with a specific hostname using the
--hostname or -h option followed by the desired hostname, for example,
docker run -h myhostname <image>.
80. How can you view the resource usage of Docker containers?
● Answer: You can view the resource usage of Docker containers using the
docker stats command, which provides real-time information on CPU,
memory, network, and disk I/O usage for running containers.
● Answer: The --env-file option in the docker run command allows you
to specify a file containing environment variables to be passed to the
container, for example, docker run --env-file myenvfile.env
<image>.
● Answer: You can create a Docker volume using the docker volume
create command followed by the volume name, for example, docker
volume create myvolume.
● Answer: You can list all Docker volumes using the docker volume ls
command.
● Answer: Docker Machine is a tool that allows you to provision and manage
Docker hosts (virtual machines) on various platforms, such as local
hypervisors, cloud providers, and bare-metal servers.
● Answer: You can create a new Docker Machine using the docker-machine
create command followed by the driver and machine name, for example,
docker-machine create --driver virtualbox mymachine.
● Answer: You can list all Docker Machines using the docker-machine ls
command.
● Answer: You can search for Docker images on Docker Hub using the docker
search command followed by the search term, for example, docker
search nginx.
93. How can you log in to Docker Hub from the Docker CLI?
● Answer: You can log in to Docker Hub from the Docker CLI using the docker
login command followed by your Docker Hub username and password.
● Answer: The docker logout command logs you out from a Docker
registry, such as Docker Hub, by removing the stored credentials.
● Answer: You can inspect a Docker image using the docker inspect
command followed by the image name or ID. This command provides detailed
information about the image, including its layers and metadata.
97. How can you export a Docker container's filesystem as a tar archive?
● Answer: You can import a tar archive as a Docker image using the docker
import command followed by the tar archive file and the new image name,
for example, docker import container.tar myimage.
● Answer: The docker save command saves one or more Docker images
and their layers as a tar archive, which can be used for backup or transfer to
another Docker host.
Piyush Dwivedi 💫💫
100. How can you load a Docker image from a tar archive?
● Answer: You can load a Docker image from a tar archive using the docker
load command followed by the tar archive file, for example, docker load
< image.tar.