Stephen Grider

Software engineers need to be well prepared to answer Docker interview questions when applying for their next job. Why? Docker use has grown exponentially in recent years and was named the third most commonly used platform after Linux and Windows in a 2019 survey. 

Docker is a vast ecosystem with many tools, keywords, and subjects to know within it. To help you ace your time in front of the hiring manager, here are 20+ of the most common Docker-relation questions and answers to study for your next interview. 

Question: What is the difference between the Docker CLI and the Docker daemon?

The Docker CLI (command line interface) and the Docker daemon get confused often. The daemon is the program that does the heavy lifting, functioning as the Docker engine representing the servers. The daemon builds images and manages created containers.

The CLI is the Docker client that interacts with the daemon. The CLI receives commands and feeds them into the daemon. 

Docker and Kubernetes: The Complete Guide

Last Updated May 2020

  • 312 lectures
  • All Levels
4.6 (27,240)

Build, test, and deploy Docker applications with Kubernetes while learning production-style development workflows | By Stephen Grider

Explore Course

Question: What are the most important Docker CLI commands? 

The most important commands are the ones you use all the time, so list those you use on an everyday basis.

Question: What are some considerations around running a database in a container? 

Remember, Docker containers are ephemeral in nature. When the container is removed, you lose the data inside of it forever.

If you’re going to run a database inside of a container, you need a data persistence strategy. This is usually done by mounting a volume in the container. The database can then store its data inside of this volume. If the container gets removed, the data will get persisted through the volume. We can then create a new container and mount the same volume to it to reuse that data. 

Question: What is the best way to create Docker images? 

Always create Docker images using a Dockerfile. Dockerfiles are text documents that specify a clear series of steps that the daemon should use to create an image. They are easy for other engineers to read and change.

Don’t create images by making a container, changing it by hand, then running ‘docker commit’. This may create an image, but it will be challenging for other engineers to reproduce the image.

Question: How can you debug a running container?

There are a few methods to use to debug a running container:

Question: Are Dockerfiles written with JSON or YAML syntax?

Trick question! Dockerfile’s container neither JSON nor YAML. That syntax only gets used in docker-compose files.

There is not a set name for the syntax used in a Dockerfile. You should say that Dockerfiles contain a series of Docker commands.

Question: Do containers get full access to their host operating system?

Docker containers are isolated from the host operating system by default. Containers can get access to the host operating system by setting the ‘privileged’ flag. When you create a container with the ‘privileged’ flag, it will have full access to the host operating system. 

Question: Why would you run a container with the ‘privileged’ flag?

There are few reasons to run a Docker container with the ‘privileged’ flag. The most common reason is that you want the container to be able to create containers on its own. 

Question: What is the difference between the ‘stop’ and ‘kill’ CLI commands? 

The ‘stop’ command sends the ‘SIGTERM’ signal to the primary process. Docker then waits for the process to exit. If the process does not exit, Docker will send the ‘SIGKILL’ signal ten seconds later.

The ‘kill’ command immediately sends a ‘SIGKILL’ signal to the primary process. 

Question: What is the difference between the ‘attach’ and ‘exec’ commands? 

Use the ‘exec’ command to run a command inside of a container. The command you run can start up a new process inside the container.

The ‘attach’ command interacts with streams for the primary process. In particular, it will interact with the STDOUT, STDERR, and STDIN streams. 

In summary: Use ‘exec’ when you want to run a new command in the container. Use the ‘attach’ command to interact with the primary process.

Question: What is the ‘diff’ command used for? 

The ‘diff’ command lists out all files that have changed inside of the container. This will only list changes made since you created the container.

Question: Are you required to use Docker Hub for all projects? 

No, Docker Hub is not required for all projects. We only use Docker Hub if we need to use images that are hosted there.

If we wanted to avoid using Docker Hub, we could build each image from scratch instead. 

Question: How do you constrain the resources a container can use? 

You can limit the resources a container uses by adding on some options to the ‘docker run’ command.

Question: Are Docker containers and virtual machines the same thing?

No, Docker containers are not equivalent to virtual machines. A Docker container is a single process or group of processes that are sandboxed, limiting access to the host file system. A virtual machine, however, is managed by a hypervisor.

Question: Do all images you create with the Docker CLI automatically get published to a public registry like Docker Hub?

No, images that you build do not get pushed to a public registry automatically. Built images are available only on the machine they were built on. To make that image available elsewhere, you must manually push it to a public registry.

Question: What are the pros and cons of using Docker during application development?

Pros

Cons

Question: When building a new image, how can you tell the Docker CLI to ignore all the files in a directory?

Create a ‘.dockerignore’ file. This file follows the same rules as a ‘.gitignore’ file. In the  ‘.dockerignore’ file, you’ll list each file and folder that Docker should ignore when building a new image. 

Question: Why would you want Docker to ignore a directory when building an image? 

Docker should handle installing all relevant dependencies when building an image. If you give Docker a copy of the dependencies, they will be overwritten during the image build. By ignoring dependencies, it ensures that build times will be speedy. 

Question: What is the difference between the commands ‘create’, ‘start’, and ‘run’? 

Question: What happens when the primary process of a container crashes? 

It depends upon the restart policy assigned to the container when it was built.

Question: How is a Docker image created?

There are two ways to create a Docker image.

1. You can create an image by using the ‘docker build’ command with a Dockerfile

2. Images are also created by running the ‘docker commit’ command on a container 

Question: What is a ‘layer’ in an image?

Docker images get built out of many layers. Each layer represents a set of changes to the file system.

When a Docker image is created, it starts off with an empty file system. As each step of a Dockerfile runs, another layer gets added to the image. 

Question: Can two running containers share the same layers from an image?

Yes!  When you create a container, Docker adds a new layer to the underlying image.  Any changes made to the container file system get written to this layer.

The layers belonging to the underlying image never change. Docker will share them among each container that is running based on that image.

Files in these original layers are only accessed through read operations. You can not write to them through the container. 

Question: Running a single container is easy using the Docker CLI. Managing many containers is more challenging. What term refers to the process of managing many containers? Also, what tools exist for managing many containers?

Managing more than one container is called an ‘orchestration’. 

There are two popular tools for container ‘orchestration’:

  1. Docker Swarm, the official Docker solution.
  2. Kubernetes, a popular open-source solution first introduced by Google.

Each of these tools creates containers and manage their lifecycles. They’re also used to handle networking connections between different containers. 

Question: Imagine you create an image that is 1GB in size. You then create five separate containers out of this image. In each image, you create a file that is 200MB in size. How much disk space does this entire operation use up?

Each container created out of an image uses a negligible amount of disk space. Making the set of five containers does not consume a significant amount of space. However, each of the 200MB files made in each container does consume 200MB.

This entire operation uses 1GB for the original image, then 5 * 200MB for each container, for a final total of 2GB.

Creating a container out of an image does not duplicate the underlying image’s layers. 

Question: What is the easiest way to monitor a running container?

The easiest way to monitor a container is by using the ‘docker ps’ command. This will print out the status of all running containers.

It is also easy to monitor a container by using the ‘docker attach’ command. This prints logs from a container, showing the STDOUT and STDERR streams of the primary process.

All past logs emitted over STDOUT and STDERR get printed by using the ‘docker logs’ command. This is useful for seeing what a container has done in the past.

Question: List some of the different commands that appear in a Dockerfile. What is the purpose of each command?

Dockerfiles contain many different commands.

Page Last Updated: April 2020

Top courses in Docker

Docker - Hands On for Java Developers
Richard Chesterwood, Virtual Pair Programmers
4.7 (1,217)
Highest Rated
Docker Mastery: with Kubernetes +Swarm from a Docker Captain
Bret Fisher, Docker Captain Program
4.6 (36,813)
Bestseller
Docker and Kubernetes: The Complete Guide
Stephen Grider
4.6 (27,240)
Docker Crash Course for busy DevOps and Developers
Tao W., Level Up, James Lee
4.4 (9,521)
Docker for the Absolute Beginner - Hands On - DevOps
Mumshad Mannambeth, KodeKloud Training
4.6 (14,234)
Bestseller
The Docker for DevOps course: From development to production
Nick Janetakis
4.5 (1,643)
Highest Rated
Build a Backend REST API with Python & Django - Advanced
Mark Winterbottom, Brooke Rutherford
4.5 (2,249)
Bestseller
Scaling Docker on AWS
Nick Janetakis
4.4 (887)
Docker for Node.js Projects From a Docker Captain
Bret Fisher, Docker Captain Program
4.6 (705)
Docker - SWARM - Hands-on - DevOps
Mumshad Mannambeth, KodeKloud Training
4.5 (948)
Hands on With Docker & Docker Compose From a Docker Captain
Nick Janetakis, Docker Captain Program
4.7 (456)

More Docker Courses

Docker students also learn

Empower your team. Lead the industry.

Get a subscription to a library of online courses and digital learning tools for your organization with Udemy for Business.

Request a demo

Courses by Stephen Grider

Modern React with Redux [2020 Update]
Stephen Grider
4.6 (56,763)
Bestseller
Advanced React and Redux
Stephen Grider
4.5 (9,062)
Meteor and React for Realtime Apps
Stephen Grider
4.6 (1,874)
Bestseller
The Complete Elixir and Phoenix Bootcamp
Stephen Grider
4.5 (4,871)
Bestseller
The Complete React Native + Hooks Course [2020 Edition]
Stephen Grider
4.6 (31,745)
Bestseller
React Native: Advanced Concepts
Stephen Grider
4.2 (4,351)

Courses by Stephen Grider