Questions with their answers.
What is the Difference between an Image, Container and Engine?
Docker image is a light weight read-only template of the application which will be used to create docker container. docker image is build from docker file. Docker containers is a running instance of docker image. It is created from an image and includes everything needed to run the application, including the code, runtime, system tools, libraries, and settings. Where as docker engine is complete mechanism tool that uses command line interface to interact with docker daemon.
What is the Difference between the Docker command COPY vs ADD?
These both are docker file commands. Both are used to copy file from local instance into containerized instance wiht slight difference that is COPY is used when we need to copy the file only if it doesnot exist there already means it will check for the file that we want to copy if it doesnot exist inside container it will copy the file on other hand ADD will copy the file unaware of the existing file if already. Also ADD is used when we need to copy and extract the data. It is used to copy and extract data of zipped file or to run url already inside container.
What is the Difference between the Docker command CMD vs RUN?
RUN command executes one or more commands in a new layer on top of the current image and commits the results. This is typically used to install software packages or configure the environment inside the image. Where as CMD command is used only once, It specifies the default command to run when a container is started from the image. There can only be one
CMD
instruction in a Dockerfile, but it can be overridden when the container is started usingdocker run
ordocker-compose
.How Will you reduce the size of the Docker image?
There are certain practices to be followed to reduce docker image size as:
Use a smaller base image, Remove unnecessary files. use multi-stage builds, use the
--no-cache
option, useCOPY
instead ofADD
.Why and when to use Docker?
We use docker to run applications inside a container which will take minimum space and use minimum resources, to remove barrier of running application on particular os, To run microservices of application where each container runs the particular microservice for a complete application, In development purpose we use dockerization of application which let developer to use minimum resources of the machine, For cloud sharing instead of sending a complete source code we can build image of the container and share over the dockerhub repo which thus can be used to run over any other place.
Explain the Docker components and how they interact with each other.
There are several docker components such as docker-engine which is the powerhouse of the docker it is responsible for creating and managing containers as well as providing command-line interaction with containers. Next comes docker containers which are the running instances of the application. It is created from docker images , it includes all the dependencies required to run the application. Then there is a docker image which is a light weight template which contains set of instructions to run application inside a container from the source code. And then docker registry is a centralized location where Docker images can be stored and distributed. The most commonly used Docker registry is Docker Hub, which is a public registry that provides access to thousands of Docker images. Private registries can also be set up for storing and sharing images within an organization. Docker Compose is a tool for defining and running multi-container Docker applications. It allows you to define a set of services that make up an application and specify how they interact with each other. Docker Compose can be used to simplify the deployment of complex applications that consist of multiple containers.
Explain the terminology: Docker Compose, Docker File, Docker Image, Docker Container?
Docker-compose -> It is a tool that is used to define and run multi-container docker applications. consider a full stack web application that consists front end backend and database. For such applications instead of running containers individually we can use the docker-compose tool.
Dockerfile -> It is a file that contains a set of instructions that are required to build an image for the application which thus leads to running a container of that application.
Docker image-> It is the blueprint of the application made from the docker file which is used to create a docker container. When we need to run a container of application we usually consider an application that contains a set of instructions bound inside it.
Docker container -> docker containers which are the running instances of the application. It is created from docker images , it includes all the dependencies required to run the application.
In what real scenarios have you used Docker?
As a full stack web developer when i need to run and tet application on local machine i consider using docker as it will make less use of resources to start the fornt end , back end and database. Also to deploy the application instead of creating multiple instances for each service i consider docker as first choice.
Docker vs Hypervisor?
Docker and hypervisors are both technologies that are used to virtualize applications and services, but they use different approaches to achieve virtualization.
Hypervisors create virtual machines (VMs) that emulate a complete hardware environment, including a guest operating system and all the necessary drivers and software. Each VM runs on top of a hypervisor, which isolates the VM from the host operating system and other VMs. This allows multiple operating systems and applications to run on the same physical machine, providing flexibility and efficient resource allocation.
On the other hand, Docker uses containerization to virtualize applications and services. Containers are lightweight and share the host operating system, kernel, and drivers, while isolating the application and its dependencies. This allows for a more efficient use of resources than traditional virtualization, as multiple containers can run on the same host operating system without the need for a separate guest operating system.
What are the advantages and disadvantages of using docker?
Advanatges:
Portability: Docker containers are portable and can be run on any host operating system that supports Docker, which makes it easy to move applications between development, testing, and production environments.
Efficiency: Docker containers are lightweight and share the host operating system, kernel, and drivers, which allows for efficient use of resources and reduces overhead.
Scalability: Docker allows for easy scaling of applications by quickly creating or removing containers as needed, which provides flexibility and reduces costs.
Consistency: Docker provides a consistent environment for applications by packaging all the necessary dependencies and configurations into a container, which reduces errors and ensures consistency across different environments.
Security: Docker provides isolation between containers and the host operating system, which provides additional security and reduces the risk of attack.
DevOps integration: Docker integrates well with DevOps tools and practices, such as continuous integration and deployment (CI/CD), which allows for efficient deployment of applications.
Reusability: Docker images can be easily shared and reused, which reduces the need for duplicating effort and allows for faster development and deployment of applications.
Disadvantages
Complexity: Docker has a learning curve and can be complex to set up and manage, especially for those who are new to containerization.
Compatibility: Docker may not be compatible with all applications and services, especially those that require a specific operating system or hardware environment.
Overhead: Docker can incur some overhead, especially for applications that require large amounts of resources or require extensive customization.
Security: While Docker provides isolation between containers and the host operating system, there is still a risk of security vulnerabilities or attacks, especially if containers are not properly configured and managed.
Performance: While Docker generally provides better performance than traditional virtualization, there may be some overhead associated with containerization that could affect performance.
Persistence: Docker containers are typically stateless, which can make it challenging to manage persistent data across containers.
Compatibility with legacy applications: It may be difficult to containerize legacy applications that are tightly coupled with a specific operating system, hardware or software configuration.
What is a Docker namespace?
Docker namespace are a feature that provides process isolation and resource management for containers. Namespaces allow Docker to create a separate environment for each container, including its own view of the system resources such as the network interface, filesystem, process IDs, and more.
What is a Docker registry?
A Docker registry is a repository that stores Docker images. It is a server-side application that allows users to store and share Docker images with others, either publicly or privately. eg, dockerhub, self-hosted registry, third perty registries like google container registry
What is an entry point?
Entry point is the command that should be run on the start of the container . The ENTRYPOINT defines the command and parameters that should be run when the container starts. It is specified in the Dockerfile using the "ENTRYPOINT" instruction followed by the command and any arguments.
How to implement CI/CD in Docker?
To implement CI/CD we can use jenkins a tool that will let us create docker container from it's platform by fetching code along with Dockerfile from github. We just have to use jenkins t will fetch code and docker build and on instance it will take commands from jenkins and create a container and then we cann access application directly on browser
Will data on the container be lost when the docker container exits?
Yes, in case if we have not mounted volume to it. When a container is stopped or removed, any data that was stored inside the container's filesystem will be lost unless it was persisted outside of the container. This is because Docker containers are ephemeral, meaning that they are designed to be disposable and recreated easily from the original image. However, it is possible to persist data outside of a container by using Docker volumes or bind mounts. Docker volumes are managed by Docker and stored in a designated location on the host system, while bind mounts are simply a reference to a directory on the host system. By using these mechanisms, data can be shared between the host system and a container or between multiple containers.
What is a Docker swarm?
What are the docker commands for the following:
view running containers
docker container ls / docker container ps
command to run the container under a specific name
docker container run --name my-container <image name>
command to export a docker
docker export container-id > <filename>.tar
command to import an already existing docker image
docker load < <image name>.tar
commands to delete a container
docker container stop <container id>
docker container rm <container id>
command to remove all stopped containers, unused networks, build caches, and dangling images?
docker system prune -a
What are the common docker practices to reduce the size of Docker Image?
Here are some common Docker practices to reduce the size of Docker images:
Use a lightweight base image: Choose a base image that is as lightweight as possible, such as Alpine Linux, rather than a full-blown operating system like Ubuntu. This can significantly reduce the size of your Docker image.
Minimize the number of layers: Each layer in a Docker image adds to its size, so try to minimize the number of layers in your image. Combine multiple commands into a single
RUN
instruction to reduce the number of layers.Remove unnecessary files: Make sure to remove any unnecessary files from your Docker image, such as temporary files, caches, and log files. This can help reduce the size of your image and improve performance.
Use multi-stage builds: If you need to build your application from source, consider using multi-stage builds. This allows you to create a temporary build image with all the necessary build tools, and then copy only the built artifacts into a final production image. This can significantly reduce the size of your Docker image.
Compress files and directories: Use tools like
tar
orgzip
to compress files and directories before copying them into your Docker image. This can help reduce the size of your image.Use Dockerfile caching: Docker caches the results of each
RUN
instruction in your Dockerfile, so take advantage of this by structuring your Dockerfile to maximize caching. Place instructions that change frequently, such as installing dependencies or updating the code, at the end of your Dockerfile to avoid invalidating the cache.
By following these Docker practices, you can significantly reduce the size of your Docker images and improve performance.
End of Post.
Reach me on: