Photo by Ruthson Zimmerman on Unsplash
Important Docker Interview Question and Answer
Docker interview questions with answers
Table of contents
- What is the difference between image, container, and engine?
- What is the Difference between the Docker command COPY vs ADD?
- What is the Difference between the Docker command CMD vs RUN?
- How Will you reduce the size of the Docker image?
- Why and when to use Docker?
- Explain the Docker components and how they interact with each other.
- Explain the terminology: Docker Compose, Docker File, Docker Image, Docker Container.
- In what real scenarios have you used Docker?
- Docker vs Hypervisor?
- What are the advantages and disadvantages of using a docker?
- What is a Docker namespace?
- What is a Docker registry?
- What is an entry point?
- How to implement CI/CD in Docker?
- Will data on the container be lost when the docker container exits?
- What is a Docker swarm?
- What are the docker commands for the following:
- What are the common docker practices to reduce the size of a docker image?
What is the difference between image, container, and engine?
Docker Engine - It is client-server technology that builds and runs containers using docker's components and services. Docker works with the daemon process, which is dockerd, REST API and the CLI that communicates with the docker daemon through API.
Docker Image - It is a read-only immutable template that defines how a container will be realized.
Docker Container - It is a runtime instance of docker image that gets created when docker run command is implemented.
What is the Difference between the Docker command COPY vs ADD?
- The COPY command is used to copy the local files to a container whereas the ADD command is used for adding the remote files to the container.
What is the Difference between the Docker command CMD vs RUN?
RUN - the RUN instruction executes when we build the image. That means the command passed to run executes on top of the current image in a new layer. The result is committed to the image.
CMD - with the CMD instruction, we can specify a default command that executes when the container is starting.
How Will you reduce the size of the Docker image?
While creating docker image I can take measures on:
Using minimal base images
Multistage builds
Minimize the number of layers
Understanding Caching
Clean up after install
Use the Dockerignore
keeping application data elsewhere
Why and when to use Docker?
Why - Docker is used to containerize an application that can be run in any system with any configuration at any time.
When - If you have developers working with different setups, Docker provides a convenient way to have local development environments that closely match the production environment, without needing to ssh into a remote box. Also, if you want to run multiple applications on one server, keeping the components of each application in separate containers will prevent problems with dependency management.
Explain the Docker components and how they interact with each other.
There are three docker components:
1. Host
2. Client
3. daemon.
Docker runs in a client-server architecture that means docker client can connect to the docker host locally or remotely. Docker client and host can run on the same host or can run on different hosts and communicate through sockets or a RESTful API. Docker daemon constantly listen for the docker API requests and process them.
Explain the terminology: Docker Compose, Docker File, Docker Image, Docker Container.
Docker File - dockerfile is a text document that contains all the commands a user could call on the command line to assemble an image.
Docker Image - a docker image is a read-only template that contains a set of instructions for creating a container that can run on the docker platform.
Docker Compose - docker compose is a tool that was developed to help define and share multi-container applications. It should be written in docker-compose.yaml file.
Docker Container - A container is a standard unit of software that packages up code and all its dependencies, so the application runs quickly and reliably from one computing environment to another.
In what real scenarios have you used Docker?
- Docker is commonly used for deploying and running applications in production environments. I have used Docker to containerize an application by writing a Dockerfile and docker compose file including the base image and its dependencies and then using the image to build the container. I also stored the image on my Dockerhub.
Docker vs Hypervisor?
- The most significant difference between hypervisors and Dockers is the way they boot up and consume resources. Hypervisors are of two types โ the bare metal works directly on the hardware while type two hypervisors work on top of the operating system. Docker, on the other hand, works on the host kernel itself.
What are the advantages and disadvantages of using a docker?
Advantages:
Low resource consumption
Scalability
Flexibility and versatility
Quick Deployment
CI/CD support
Large community
Disadvantages:
Containers do not run at bare-metal speeds
Persistent data storage is complicated
Graphical applications do not work well
What is a Docker namespace?
- Docker is written in the Go programming language and takes advantage of several features of the Linux kernel to deliver its functionality. Docker uses a technology called namespaces to provide the isolated workspace called the container. When you run a container, Docker creates a set of namespaces for that container. These namespaces provide a layer of isolation.
What is a Docker registry?
- A registry is a storage and content delivery system, holding named Docker images, available in different tagged versions.
What is an entry point?
- ENTRYPOINT is one of the many instructions you can write in a dockerfile. The ENTRYPOINT instruction is used to configure the executables that will always run after the container is initiated. For example, you can mention a script to run as soon as the container is started.
How to implement CI/CD in Docker?
To implement CI/CD in docker, you can follow the steps:
Set up a version control system (e.g., Git) to manage your codebase.
Configure a CI/CD tool (e.g., Jenkins, GitLab CI/CD, CircleCI) to monitor the repository for changes.
Create a Dockerfile to define the image build process and environment.
Use the CI/CD tool to build the Docker image, run tests, and push the image to a Docker registry.
Configure deployment pipelines to deploy the Docker image to your target environments (e.g., staging, production) using container orchestration tools like Kubernetes or Docker Compose.
Will data on the container be lost when the docker container exits?
- The answer is no. Any data that your application writes to disk gets preserved in its container until you explicitly delete the container.
What is a Docker swarm?
- Docker Swarm is a container orchestration tool running the Docker application. It has been configured to join together in a cluster.
What are the docker commands for the following:
view running containers
- docker ps
command to run the container under a specific name
- docker run --name NAME IMAGE
command to export a docker
- docker commit CONTAINER
command to import an already existing docker image
- docker pull IMAGE
commands to delete a container
- docker rm CONTAINER
command to remove all stopped containers, unused networks, build caches, and dangling images?
- docker image prune -a
What are the common docker practices to reduce the size of a docker image?
To reduce the size of the docker image, you can follow the below steps:
Using minimal base images
Multistage builds
Minimize the number of layers
Understanding Caching
Clean up after the install
Use the dockerignore
keeping application data elsewhere
If you think there can be any other questions, do comment below ๐.
That is all for the interview questions and answers. If you like the blog, make sure you give a like, have any queries? Do comment and share the blog.
You can also subscribe to the newsletter for updates on my blogs.
Follow me:
LinkedIn: https://www.linkedin.com/in/sonineelp