Technical interviews are tough. You’re expected to know the answer to any question asked at any time. Provide a wrong answer and you’ll get the standard, “Thanks for coming in, we’ll get back to you.”
The experience can be even more trying when the interview is about a complex technology. Docker is one such technology.
Thus, to help you to move forward in the interview process and avoid being shown the door, here are some questions about Docker and containers that are typically asked of a developer in a technical interview. This is not a comprehensive list; there will surely be others asked of you. But, if you answer these correctly, you will demonstrate that you have a basic understanding of containers and Docker and how to use them.
What is the difference between a container and a container image?
A container image is a template from which a running container is created. It is defined in a text file that is named by default Dockerfile. (You can use another name and reference it with the Docker build command to create the container image.)
The following code snippet is a sample Dockerfile that defines a container image for a Node.JS web server application:
|
To build the container image, execute the following Docker command:
docker build -t mycontainerimage .Let’s break down that code snippet:
dockeris the general command executablebuildis the subcommand to create a container image-tis the option that indicates the (tag) name of the container imagemycontainerimageis the arbitrary (tag) name of the container image.(period) tells docker build to look for the container description in the default file named Dockerfile. (If the text file with the description of the container image has a different name, define it here. For example, if the textfile is named mydockerfile you would execute the command:docker build -t mycontainerimage mydockerfile)
To create a container based on the container image created above, run the following command:
docker run --name mycontainer mycontainerimageWithin that code snip:
dockeris the general command executablerunis the subcommand to create a container--nameis the option that indicates the name of the containermycontaineris the arbitrary name of the containermycontainerimageis the arbitrary (tag) name of the container image.(period) tells thedocker buildcommand to look for the container description in the default file named Dockerfile. (If the text file containing the description of the container image has a different name, define it here. For example, if the text file is named mydockerfile execute the command:docker build -t mycontainerimage mydockerfile)
What is the result if you run the following command?
sudo docker container run -it busybox /bin/shThis command creates a container using the busybox container image, and once the container is up and running exposes a command-line prompt from within the container:
sudo docker container run -it busybox /bin/shThis is similar to logging into a virtual machine and then having access to a command terminal from within the virtual machine.
How do you create a container image that denies root access to its file system?
The way to deny root access is to create a special user as part of the container image defined in the Dockerfile, and then allow that user access to only the parts of the internal file system needed to do the work at hand.
The code snippet below shows the technique to create a container image that defines a default non-root user:
|
What does the following command do?
docker run -d --name=webby --net=host nginxThis command creates a container named webby based on the container image nginx, and runs it as part of the localhost network as declared by using the option --net=host:
docker run -d --name=webby --net=host nginxIn this case, the container is an nginx web server that runs by default on port 80. Thus, once the container is up and running, to access nginx you execute the following command from the host:
curl localhost:80The container is created using the -d option. This means it will run as a background process.
What is the difference between docker and dockerd?
The executable docker is the client through which users interact with the daemon dockerd. dockerd is the software that actually interacts with the host operating system to create, run and destroy docker containers on a host system.
Thus, in the following command, the executable docker is the intermediary between you and dockerd:
docker run -d --name myserver nginxIn other words, you are telling docker to contact dockerd, which must create a container based on the container image nginx. So, dockerd will name the container myserver and run it as a background process.
How do you give a container access to a directory on the local file system?
You can give a container access to a directory on the local file system by using the -v option when executing the docker run command set. The -v option maps a directory in the local file system into a container’s internal file system.
The following code snippet illustrates the technique:
|
Is it possible to store a container image in a local repository? If so, how?
Yes you can store a container image using a local repository. Create a repository based on the container image, registry. which acts as a local Docker container image repository. The example below shows how you create the local registry and then add a container image to it:
|
Docker interview questions: How far should you go in the stack?
Containers are a game-changer. Being able to use them effectively is an important skill for anybody working in today’s software development environment.
The depth of your knowledge that will help you ace a technical interview depends on the position for which you interview. The questions asked above are general to any job that requires a working knowledge of container technology. This includes developers as well as engineers who work in CI/CD pipelines that use containers as part of the general deployment process.
The lower you go in the technology stack, the more complex things become. Interviewers will ask more difficult questions for jobs that require an in-depth understanding of containers at the operating system level. You’ll also need to answer the questions listed above just to get through the door.
No matter the position you’re interviewing for, if you can answer the questions posed above, that’s a good start to a successful career using containers in your day-to-day activities.