You can check all 55 Docker interview questions here ๐ https://devinterview.io/design/docker-interview-questions
- Docker is a containerization platform which packages your application and all its dependencies together in the form of containers so as to ensure that your application works seamlessly in any environment be it development or test or production.
- Docker containers, wrap a piece of software in a complete filesystem that contains everything needed to run: code, runtime, system tools, system libraries etc. anything that can be installed on a server.
- This guarantees that the software will always run the same, regardless of its environment.
An instance of an image is called a container. You have an image, which is a set of layers. If you start this image, you have a running container of this image. You can have many running containers of the same image.
You can see all your images with docker images
whereas you can see your running containers with docker ps
(and you can see all containers with docker ps -a
).
So a running instance of an image is a container.
Although ADD
and COPY
are functionally similar, generally speaking, COPY
is preferred.
Thatโs because itโs more transparent than ADD. COPY only supports the basic copying of local files into the container, while ADD has some features (like local-only tar extraction and remote URL support) that are not immediately obvious. Consequently, the best use for ADD is local tar file auto-extraction into the image, as in ADD rootfs.tar.xz /.
Docker hub is a cloud-based registry service which allows you to link to code repositories, build your images and test them, stores manually pushed images, and links to Docker cloud so you can deploy images to your hosts. It provides a centralized resource for container image discovery, distribution and change management, user and team collaboration, and workflow automation throughout the development pipeline.
There are four states that a Docker container can be in, at any given point in time. Those states are as given as follows:
- Running
- Paused
- Restarting
- Exited
If you must stop the container really quicklyโฆ (someone pushed something to production on Friday evening?โฆ ;) )
We can identify the status of a Docker container by running the command
docker ps โa
which will in turn list down all the available docker containers with its corresponding statuses on the host. From there we can easily identify the container of interest to check its status correspondingly.
The primary difference is that using โdocker createโ creates a container in a stopped state.
Bonus point: You can use โdocker createโ and store an outputed container ID for later use. The best way to do it is to use โdocker runโ with --cidfile FILE_NAME as running it again wonโt allow to overwrite the file. A good practice is to keep well ogranised directory structure: /containers/web/server1/ws.cid containers/web/server3/ws.cid
Both CMD
and ENTRYPOINT
instructions define what command gets executed when running a container. There are few rules that describe their co-operation.
- Dockerfile should specify at least one of
CMD
orENTRYPOINT
commands. ENTRYPOINT
should be defined when using the container as an executable.CMD
should be used as a way of defining default arguments for anENTRYPOINT
command or for executing an ad-hoc command in a container.CMD
will be overridden when running the container with alternative argumen
- Docker registry is a service for hosting and distributing images (the default one is the Docker Hub).
- Docker repository is a collection of related Docker images (the same name but with different tags).
There is no loss of data when any of your Docker containers exits as any of the data that your application writes to the disk in order to preserve it. This will be done until the container is explicitly deleted. The file system for the Docker container persists even after the Docker container is halted.
When we build an Image, Docker will process each line in Dockerfile. It will execute the commands on each line in the order that is mentioned in the file. But at each line, before running any command, Docker will check if there is already an existing image in its cache that can be reused rather than creating a new image.
There are three main features helping to achieve that:
- Volumes
- Environment variable injection
- Read-only file systems
The simplest way is to use network port mapping. Thereโs also the - -link flag which is deprecated.
It is preferable to create Stateless application for Docker Container. We can create a container out of our application and take out the configurable state parameters from application. Now we can run same container in Production as well as QA environments with different parameters. This helps in reusing the same Image in different scenarios. Also a stateless application is much easier to scale with Docker Containers than a stateful application.
Docker image is the source of Docker container. In other words, Docker images are used to create containers. Images are created with the build command, and theyโll produce a container when started with run. Images are stored in a Docker registry such as registry.hub.docker.com
because they can become quite large, images are designed to be composed of layers of other images, allowing a minimal amount of data to be sent when transferring images over the network.
Docker containers include the application and all of its dependencies, but share the kernel with other containers, running as isolated processes in user space on the host operating system. Docker containers are not tied to any specific infrastructure: they run on any computer, on any infrastructure, and in any cloud.
Some of the common instructions in Dockerfile are as follows:
- FROM: We use FROM to set the base image for subsequent instructions. In every valid Dockerfile, FROM is the first instruction.
- LABEL: We use LABEL to organize our images as per project, module, licensing etc. We can also use LABEL to help in automation.
In LABEL we specify a key value pair that can be later used for programmatically handling the Dockerfile. - RUN: We use RUN command to execute any instructions in a new layer on top of the current image. With each RUN command we add something on top of the image and use it in subsequent steps in Dockerfile.
- CMD: We use CMD command to provide default values of an executing container. In a Dockerfile, if we include multiple CMD commands, then only the last instruction is used.
You will need to save the Docker image as a tar file:
docker save - o <path for generated tar file> <image name>
Then copy your image to a new system with regular file transfer tools such as cp
or scp
. After that you will have to load the image into Docker:
docker load -i <path to image tar file>
Thanks ๐ for reading and good luck on your next tech interview!
Explore 3800+ dev interview question here ๐ Devinterview.io