Question:
A container is running from an image. If we try to delete the image while the container is running, why can't we delete the image?
Answer:
If we attempt to delete the image, Docker will display an error message stating that the image is being used by a container.
The reason is that each line in a Dockerfile creates a layer.
Each layer is a read-only layer. Once a layer is created, it cannot be modified. If we want to make changes to the image, we need to create a new Dockerfile and build a new image.
When we create a container, only a reference to the image layers is passed to the container. The container has a writable layer, meaning we can create files, modify files, or generate output inside the container. These changes reside only in the container and do not affect the image.
However, the container always depends on the image. That is why we cannot delete the image while the container is using it.
This methodology is called Copy-on-Write (CoW).
Question:
If an image size is 10 GB, what will be the container size?
Answer:
A container contains only references to the image layers. Therefore, the container size consists of the files and changes stored in the writable layer running inside the container.
Question:
What command is used to find the size of a container?
docker ps --size
The Dive application is used to analyze each layer in a Docker image.
Client-Server Architecture
Docker works based on the Client-Server Architecture.
The Docker client sends requests to the Docker daemon, and the Docker daemon responds to the client.
If a requested image is not found on the local system, Docker retrieves it from a public repository.
docker run hello-world
This command pulls the image from the repository and runs it on the local system.
To pull an image from an image repository
docker pull busybox:1.36
To create a container
docker run busybox:1.36
To create a container, execute the ls command inside it, and exit immediately
docker run busybox:1.36 ls
To create a container and enter interactive mode
docker run -it busybox:1.36
Interview Questions
Question 1:
In a Client-Server architecture, who sends requests to the server?
Answer: The client.
Question 2:
What happens when a container is created from an image?
Answer: A writable layer is created.
Question 3:
What is the purpose of the writable layer in a container?
Answer: To handle file modifications and store changes made inside the container.
Question 4:
The read-only layers in Docker come from what?
Answer: The base image and its image layers.
Question 5:
What happens to the writable layer when the container is deleted?
Answer: It is discarded.
Question 6:
In a Docker environment, the client interacts with what?
Answer: The Docker daemon.
Question 7:
How do you start a container in interactive mode?
Answer:
docker run -it <image-name>
Top comments (0)