TechSchool
Home / Interview questions / Docker

Docker interview questions

45 questions about Docker with short answers and code. Each answer links to a full lesson on this site. Free, with no signup and no ads.

Basics

1. What is Docker?

Docker is an open source container engine. It packages an application and its dependencies into a container. The container runs on many Linux machines.

Full lesson: Docker tutorial

2. What is a Docker container?

A container is a light unit that runs one application. Containers share the host kernel. They stay isolated from each other.

Full lesson: Docker intro

3. What is a Docker image?

An image is a read-only template for a container. Docker builds a container from an image. Docker pulls the image from Docker Hub when it is not local.

Full lesson: Docker image usage

4. How do you list local Docker images?

Run the docker images command. The output shows the repository, tag, image ID, creation time, and size.

docker images

Full lesson: Docker images command

5. How do you list running containers?

Run the docker ps command. It shows only running containers by default. Add the -a option to show stopped containers too.

docker ps -a

Full lesson: Docker ps command

6. How do you run a container from an image?

Use the docker run command. It creates and starts a new container. Add -it to open an interactive shell.

docker run -it ubuntu:15.10 /bin/bash

Full lesson: Docker run command

7. What is a Dockerfile?

A Dockerfile is a text file with build instructions. Docker reads it to build a custom image. Each line adds a step to the image.

FROM nginx
RUN echo 'hello' > /usr/share/nginx/html/index.html

Full lesson: Docker dockerfile

8. How do you copy a file from a container to the host?

Use the docker cp command. Put the container name and path before the colon. Put the host path after the colon.

docker cp my_container:/path/in/container /path/on/host

Full lesson: Docker cp command

9. How do you run a command inside a running container?

Use the docker exec command. It starts a new process in the container. The container must be running.

docker exec -it my_container /bin/bash

Full lesson: Docker exec command

10. How do you show the changes in a container file system?

Run the docker diff command. The output marks added files with A. It marks deleted files with D and changed files with C.

docker diff CONTAINER

Full lesson: Docker diff command

11. How do you save a container as a new image?

Use the docker commit command. It saves the current state of the container as an image. Add -m to write a commit message.

docker commit my_container my_new_image

Full lesson: Docker commit command

12. How do you add a tag to a local image?

Use the docker tag command. It creates an alias for the image. The alias can use an easy name or a version number.

docker tag myimage:1.0 myrepo/myimage:latest

Full lesson: Docker tag command

13. How do you remove a stopped container?

Run the docker rm command with the container name or ID. The command does not delete a running container. Stop the container first.

docker rm my_container

Full lesson: Docker rm command

14. How do you remove a Docker image?

Use the docker rmi command with the image name or ID. Add -f to force the delete. The command frees local storage space.

docker rmi my_image

Full lesson: Docker rmi command

15. What common mistake happens when you delete a running container?

The docker rm command fails on a running container. This is a common mistake. Stop the container first, then run docker rm again.

docker stop my_container
docker rm my_container

Full lesson: Docker rm command

Intermediate

16. What does the docker pull command do?

The docker pull command downloads an image from a registry to your local machine. You can specify a tag to get a specific version.

docker pull ubuntu:20.04

Full lesson: Docker pull command

17. How do you view the logs of a container?

Use the docker logs command to see the output of a container. Add the -f option to follow the log in real time.

docker logs -f my_container

Full lesson: Docker logs command

18. How do you check the port mappings of a container?

The docker port command shows how container ports map to host ports. You can also specify a private port to see one mapping.

docker port my_container

Full lesson: Docker port command

19. How do you display the running processes inside a container?

Use the docker top command to list processes in a container. It works like the Linux ps command.

docker top my_container

Full lesson: Docker top command

20. How do you show detailed information about the Docker system?

The docker info command displays system-wide information. It includes the number of containers and images, and the storage driver.

docker info

Full lesson: Docker info command

21. How do you load a Docker image from a tar file?

Use the docker load command with the -i option to load an image from a tar archive. The image becomes available locally.

docker load -i myimage.tar

Full lesson: Docker load command

22. How do you push a Docker image to a registry?

The docker push command uploads a local image to a registry. You must tag the image with the registry name first.

docker push myrepo/myimage:latest

Full lesson: Docker push command

23. How do you stop a container immediately?

The docker kill command sends a SIGKILL signal to the container. The container stops without a graceful shutdown.

docker kill my_container

Full lesson: Docker kill command

24. How do you install Node.js using Docker?

Pull the official Node.js image from Docker Hub. Then run a container from that image.

docker pull node:latest

Full lesson: Docker install node

25. How do you install PHP using Docker?

Pull the official PHP image from Docker Hub. You can choose a specific version tag.

docker pull php:5.6-fpm

Full lesson: Docker install php

26. What happens if you try to remove an image that is used by a container?

Docker prevents the removal by default. You must stop and remove the container first, or use the -f option to force removal.

docker rmi -f my_image

Full lesson: Docker rmi command

27. What is a common mistake when you use docker run without the -d option?

The container runs in the foreground and attaches to your terminal. You cannot use the terminal until the container stops.

docker run -d my_image

Full lesson: Docker run command

28. What mistake can happen if you forget to tag an image before pushing?

The push fails because the image name does not include a registry or repository. You must tag the image with the correct name first.

docker tag myimage:1.0 myrepo/myimage:latest

Full lesson: Docker tag command

29. What is a common error when you use docker exec on a stopped container?

The command fails because docker exec requires a running container. Start the container first.

docker start my_container

Full lesson: Docker exec command

30. What mistake occurs if you use docker load with a file that is not a tar archive?

The load command fails because it expects a tar file created by docker save. Make sure the file is a valid image archive.

docker load -i myimage.tar

Full lesson: Docker load command

Common mistakes

31. What are the main parts of Docker?

Docker has three main parts. The client sends commands. The daemon builds and runs containers. Images and containers are the core objects.

Full lesson: Docker intro

32. How do you start the Docker tutorial on a new machine?

Install Docker first. Then run a test container. The tutorial shows the first steps.

docker run hello-world

Full lesson: Docker tutorial

33. How do you download an image with a specific tag?

Add the tag after a colon. The default tag is latest. This command gets the 20.04 tag of ubuntu.

docker pull ubuntu:20.04

Full lesson: Docker pull command

34. How do you run a container from a local image?

Use docker run with the image name. Docker starts a new container from that image. Add -d to run it in the background.

docker run -d nginx

Full lesson: Docker run command

35. How do you view the log output of a container?

Use the docker logs command with the container name. Add -f to follow new output. Add --tail 10 to show only the last 10 lines.

docker logs -f --tail 10 my_container

Full lesson: Docker logs command

36. How do you show the port mapping of a container?

Use docker port with the container name. Docker prints the host port for each container port. You can also ask for one private port.

docker port my_container 80

Full lesson: Docker port command

37. How do you list the processes inside a container?

Use the docker top command with the container name. Docker shows the processes that run in that container. This helps you check the main process.

docker top my_container

Full lesson: Docker top command

38. How do you show the Docker system information?

Use the docker info command. Docker prints the version, storage driver, and container counts. This helps you check the daemon state.

docker info

Full lesson: Docker info command

39. How do you load an image from a tar file?

Use docker load with the -i option and the file name. Docker reads the tar file and adds the image to the local store. The file must be a tar archive.

docker load -i myimage.tar

Full lesson: Docker load command

40. How do you push an image to a registry?

Use docker push with the full image name. The name must include the registry and the tag. Log in first if the registry needs a password.

docker push myrepo/myimage:1.0

Full lesson: Docker push command

41. What is a common mistake when you use docker logs without a container name?

The command fails because it needs a container name or ID. Docker cannot guess the target. Always give the container name.

docker logs my_container

Full lesson: Docker logs command

42. What mistake happens if you use docker port on a container with no published ports?

The command prints no mapping. The container has no host port. Start the container with -p to publish a port.

docker run -d -p 8080:80 nginx

Full lesson: Docker port command

43. What is a common error when you use docker top on a stopped container?

The command fails because the container is not running. There are no processes to list. Start the container first.

docker start my_container

Full lesson: Docker top command

44. What mistake can happen if you push an image without a registry prefix?

Docker sends the image to Docker Hub by default. The push can fail if you are not logged in. Use the full registry name to target the right registry.

docker push docker.io/myrepo/myimage:1.0

Full lesson: Docker push command

45. What is a common mistake when you use docker load with a file that is not a tar archive?

The command fails with an error. Docker needs a tar archive from docker save. Check the file type before you load it.

docker load -i myimage.tar

Full lesson: Docker load command

More on TechSchool
See the Docker cheat sheet Python JavaScript TypeScript HTML CSS React Vue 3 Node.js Java PHP C C++ C# Go SQL MongoDB Redis Linux Git Data Structures