Day 21: Docker Interview Questions

This is the Day 21 of the #90DaysofDevOps challenge in which we are discussing the Docker interview questions that will be helpful for Freshers.

Q1) What is the difference between an Image, Container, and Engine?

  • Image: Docker Images are executable files that contain the software's code, dependencies, framework, etc.

  • Container: With the help of images Docker containers have been created that start the application in a running phase.

  • Engine: It is a set of instructions that are used to create Docker containers. It also manages the container's lifecycle, networking, and storage. It has several layers that are used to execute the code in the containers.

Q2) What is the difference between the Docker command COPY vs ADD?

  • COPY command is used to copy the files that are present in the Docker Host machine. So while using COPY command you have to first download the file and make it available on your Docker Host machine.

  • ADD command provides some extra features that mean while using ADD command you don't have to download the war file. You have to give the URL of the file in ADD command with the absolute path. So, this will reduce the task of the user, he is not going to download the file before.

Q3) What is the difference between the Docker command CMD vs RUN?

These two Docker commands CMD and RUN are used while creating Dockerfile.

  • CMD command is used to set default commands that are going to run when the Docker container will be started. Remember the latest CMD command will run if you included many CMD commands in Dockerfile.

  • RUN command immediately makes the new image layer that will run the commands while executing Dockerfile.

Q4) How Will you reduce the size of the Docker image?

As DevOps Engineer, we have to optimize the size of the Docker Images. While building the images and applications sometimes the size of the images comes in GBs that will take a lot of storage and it will also affect the downloading time too.

  • Using small-size Images: It means using the base images that have a size very less. alpine image is a very minimal base image and it is also very secure.

  • Using Docker Multistage Builds: Docker introduces this new feature that means creating multiple Dockerfiles with the images that we wanted to build by avoiding unused files and artifacts.

  • By using DockerSlim we can reduce the Docker image size and make it more secure. To use Dockerslim first you need to install the Dockerslim then execute the docker-slim build command to make the size of the image less.

Q5) Why and when to use Docker?

  • Docker is a platform that provides containerization technology. By using Docker we can pack all application code with its dependencies, frameworks, and its libraries.

  • Docker is a secured, isolated and portable platform. This means Docker runs on any OS or virtual machine (AWS EC2, GCP, Azure), etc. By secure means, you can give your containers access to your specified ports that you had exposed to run on that Docker container.

  • By using Docker developers don't need to worry about flexibility. Because Docker can scale the containers to multiple servers and easily can access multiple servers also.

This is the reason why we need to use Docker.

Q6) Explain the Docker components and how they interact with each other.

There are 4 Main components of Docker :

  1. Docker Client

  2. Docker Engine is also called Docker Daemon.

  3. Docker Registry i.e DockerHUB

  4. Docker Image

So, when the Docker client runs the docker run command it goes to the Docker Engine that communicates with the help of Docker API then Docker Engine or Docker daemon (dockrd) listens to the Docker API request and searches for the image in the local repository. After not getting the image into the local repository it goes to the Docker registry i.e DockerHUB which is the platform of all the images that are stored on DockerHUB. After getting the image from the DockerHUB it comes back to the Docker Engine or daemon then pull the docker image to the local repo and gives it back to the Docker client. This is how Docker's four main components work.

Q7) Explain the terminology: Docker Compose, Docker File, Docker Image, Docker Container?

  • Docker-compose: Docker-compose is a tool that contains a YAML file that is used to start the multi-containers in just single command and we can take down the created containers in single command too. Most importantly by using docker-compose we can scale the containers.

  • Docker File: DockerFile is a script file or we can say a file of instructions that are used to build the image and start the container of your created image with a set of commands that will be executed when the container will be started. Developers can share the Dockerfile on GitHub and with other developers too.

  • Docker Image: Docker Image contains application code with its dependencies, frameworks, and its libraries that are securely packed.

  • Docker Container: With the help of Docker Images containers are created that will run the image's application code so that we can access it on any web browser.

Q8) In what real scenarios have you used Docker?

I used Docker in automating the CI/CD pipeline by using Jenkins, and microservices architecture.

Q9) Docker vs Hypervisor?

Docker

Hypervisor

Docker is a software tool that works on top of the Operating System. It takes the host kernel and works on the principle of virtualization.

The hypervisor is used in the creation of making virtual machines that work on the OS or on the CPU.

In Docker, boot time is low as it works on the same OS. Users can start the containers in seconds and can start their work

As the hypervisor has many different OS so it will take a couple of minutes to start the operating systems.

Docker containers are lightweight.

The hypervisor takes GB of space as it works with many different operating systems.

Q10) What are the advantages and disadvantages of using docker?

  • Advantages of Docker:

    1. Docker containers are lightweight so it will take less time to start the containers and run the application.

    2. With the help of Docker Images, you don't need to create a new environment. You can run the Docker Image on any OS (Linux, Windows, Mac).

  • Disadvantages of Docker:

    1. While automating the CI/CD pipeline if any docker containers came down or fail during the pipeline then you cannot scale the containers efficiently.

    2. Docker is platform-independent, it works on Linux only.

Q11) What is a Docker namespace?

By creating namespaces you can isolate the Docker containers and uses different namespaces in Linux like:

  1. Pid Namespace that contains process ID's.

  2. Net Namespace that contains network interfaces.

  3. Mnt Namespaces that are used to mount the filesystem.

  4. User Namespace that runs containers with different users and different groups.

Q12) What is a Docker registry?

The Docker registry is used to store images with different versions with specific names with different tags. Docker users can easily pull the images from the central repository and push the images to the DockerHUB (central repository) where all docker images and registries are stored.

Q13) What is an entry point?

EntryPoint is similar to the CMD command which is used in Dockerfile to execute the commands while running the containers so that the container will not stop automatically. Entrypoint is used to fix the command so that when the container will start the ENTRYPOINT instruction will run the exec command.

Q14) How to implement CI/CD in Docker?

CI/CD stands for Continuous Integration and Continuous Deployment which will automate the process of creating, testing, and deploying the Docker containers. To implement CI/CD in Docker you can use

  • Version Control System i.e GitHub Workflow Actions.

  • Continuous Integration and Continuous Deployment tools like Jenkins, AWS CodeCommit, and CodeDeploy will automatically build the container and run it on the servers by just writing the script.

Q15) Will data on the container be lost when the docker container exits?

The answer is YES !! and this is the biggest disadvantage of Docker related to the persistence of data storage in containers. To store and save the data use volume while running the docker container and mount it with the container.

Q16) What is a Docker swarm?

Docker Swarm is an orchestration tool that allows us to create and manage Docker Nodes easily. By Docker swarm, you can easily scale the applications, and manage the load balancers. In a docker swarm, every docker node has its daemons so they can easily communicate with the Docker API in the proper Docker environment that offers high availability.

Q17) 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

  • command to export a docker

    docker export <containerID/Name> <Filename>

  • command to import an already existing docker image

    docker import <filename> <image:tag>

  • commands to delete a container

    docker rm -f <image-name>

  • command to remove all stopped containers, unused networks, build caches, and dangling images

    docker system prune

Thanks for reading !! That's it for today's task. I hope you all learn something new :)

Follow me on Hashnode for more Linux and DevOp Blogs !!

You can connect with me on Twitter (amitmau07) !!

HAPPY LEARNING :)

THANK YOU :)