7 Frequent Interview Questions on Docker | DevOps | SRE
Docker Interview Questions and Answers
1. What is the difference between CMD and ENTRYPOINT in a Dockerfile?
Ans : CMD in Dockerfile Instruction is used to execute a command in Running container, There should be one CMD in a Dockerfile.
ENTRYPOINT in Dockerfile Instruction is used you to configure a container that you can run as an executable.
2. What if you have accidentally out of the Docker containers, will you loose the files?
Ans: No, When a Docker Container is exited, no data loss occurs as all the data is written to the disk by the application for the sole purpose of preservation. This process is consistently repeated until and unless the container is unequivocally deleted. Moreover, the file system for the Docker container persists even after the Docker container is halted.
3. Is Docker Container, Stateless or Stateful application?
Ans: Stateless applications should be preferred over a Stateful application for Docker Container. We can create one container from our application and take out the app’s configurable state parameters. Once it is one, we can run the same container with different production parameters and other env’s. Through the Stateless application, we can reuse the same image in distinct scenarios. It is also easier to scale a Stateless application than a Stateful application when it comes to Docker Containers.
4. Describe the usage of Dockerfile and Can you write Dockerfile to create Ubuntu Docker Image?
Ans : A Dockerfile is a series of specific instructions something we must send Docker to create the files. It is a text document that contains all the commands a user could call on the command line to assemble an image.
Below is workflow to create Docker Container from Dockerfile
Dockerfile –> Docker Image –> Docker Container
FROM ubuntu:18.04
LABEL version=”1.0" \
RUN apt-get update && apt-get install -y apache2 && apt-get clean
ENV APACHE_LOG_DIR /var/log/apache2
EXPOSE 80
COPY index.html /var/www/
CMD [“/usr/bin/apache”, “-D”, “FOREGROUND”]
3. What is difference between ADD and COPY in Dockerfile?
Ans: COPY : Copies a file or directory from your host to Docker image, It is used to simply copying files or directories into the build context.
Example:
COPY abc.html /var/www/
ADD: Copies a file and directory from your host to Docker image, however can also fetch remote URLs, extract TAR/ZIP files, etc. It is used downloading remote resources, extracting TAR/ZIP files.
Example:
ADD java/jdk-7hjn31-linux-x32.tar /opt/jdk/
4. How to list the specific docker image and then run the image as a container?
Ans : $ docker image ls <imagename> (command to list the docker image)
docker run -it imagename /bin/bash
Here i -> interactive, t -> terminal
5. What if you have accidentally out of the Docker containers, will you loose the files?
Ans: No, When a Docker Container is exited, no data loss occurs as all the data is written to the disk by the application for the sole purpose of preservation. This process is consistently repeated until and unless the container is unequivocally deleted. Moreover, the file system for the Docker container persists even after the Docker container is halted.
6. Is Docker Container, Stateless or Stateful application?
Ans: Stateless applications should be preferred over a Stateful application for Docker Container. We can create one container from our application and take out the app’s configurable state parameters. Once it is one, we can run the same container with different production parameters and other env’s. Through the Stateless application, we can reuse the same image in distinct scenarios. It is also easier to scale a Stateless application than a Stateful application when it comes to Docker Containers.
7. Tell us about the steps for the Docker container life cycle.
Answer: Here are the steps with relevant commands:
- Create container: docker create — name <container-name> <image-name>
• Run docker container: docker run -it -d — name <container-name> <image-name> bash
• Pause container: docker pause <container-id/name>
• Unpause container: docker unpause <container-id/name>
• Start container: docker start <container-id/name>
• Stop container: docker stop <container-id/name>
• Restart container: docker restart <container-id/name>
• Kill container: docker kill <container-id/name>
• Destroy container: docker rm <container-id/name>
Good to know about the Docker Architecture
- Client: The Docker Client component runs operations to set up communication with the Docker Host.
- Docker Host: This component holds the Docker Daemon, Images, and Containers. While the Docker Daemon establishes a link with the Registry, the Docker Images act as metadata for the applications which are held in the Docker Containers.
- Registry: This Docker Component is used to store the Docker Images. Docker Hub and Docker Cloud are public registries, which can be utilized by anyone.
At this point we reached the end of our tutorial. I hope you enjoyed reading this article, feel free to add your comments, thoughts or feedback and Please get in touch on LinkedIn: Manoj M Kumar | LinkedIn