DOCKER interview question

DOCKER interview question
  1. What is Docker?

➢ Docker is an open-source tool designed to create, deploy, and run applications by using containers.
➢ Docker uses a container on the host OS to run applications. It allows applications to use the same Linux kernel as a system on the host computer rather than creating a whole virtual OS.
➢ We can install docker on any OS but docker engine runs natively on Linux distributions.
➢ Docker is written in “GO” programming language.
➢ Docker is a tool that performs OS-level virtualization also known as Containerization.
➢ Before docker many users face the problem that a particular code is running in the developer’s system but not in the user’s system.
➢ Docker was first released in 2013. It was developed by Solomon Hykes and Sebastian Pahl.
➢ Docker is a set of Platform-as-a-Service that uses OS-level virtualization whereas VMWare uses hardware-level of virtualization.

IaaS: IaaS stands for Infrastructure as a Service. It's a type of cloud computing service that provides on-demand access to computing resources like storage, networking, and virtualization. Ex:- ec2

PAAS: PaaS, or Platform as Service, is a cloud computing model that provides a complete cloud platform for developing, running, and managing applications. eg. docker, lambda fn

PAAS offers a ready-made environment that includes all tools, services, and runtime for developers to build, deploy, and scale applications more efficiently.

SaaS: SaaS stands for Software as a Service. It's a cloud-based model for delivering software to users. eg. Zoom is a SaaS company It provides video conferencing software over the internet, allowing users to host and participate in virtual meetings. There is payment involved for end users in using a platform.

what is containerization?

Docker containerization is a process that allows developers to package their applications and their dependencies together in a single container. This container can then be easily shipped and deployed on any platform that supports them. This makes it easier to deploy and run applications in different environments.

Advantages of Docker:
➢ No pre-allocation of RAM.
➢ CI efficiency: — docker enables you to build a container image and use that same image across every step of the deployment process.
➢ Less cost.
➢ It is light in weight.
➢ It can run on physical h/w / virtual h/w or on cloud.
➢ You can reuse this image.
➢ It took very little time to create a container.

Disadvantages of Docker:
➢ Docker is not a good solution for applications that require rich GUI.
➢ Difficult to manage a large number of containers.
➢ Docker doesn’t provide cross-platform compatibility means if an application is designed to run in a docker container in Windows then it can’t run in Linux or vice-versa.
➢ Docker is suitable when the development O.S and testing O.S are the same. If the O.S are different then we should use VM.
➢ No solution for data recovery and backup.

Components of Docker:

Docker Daemon, Docker Hub/ Registry, Docker Image, Docker Containers, etc.

  1. Docker Daemon: background process. and manage container.

  2. Docker Hub/ Registry:
    ➢ Docker registry manages and stores the docker image.
    ➢ There are two types of registries in the docker:
    a. Public Registry: it is also called a docker hub.
    b. Private Registry: it is used to share images within the enterprise.

  3. Docker Image:

Single file with all the dependencies and configuration required to run a program.
➢ A Docker image is a read-only file or a template with a set of instructions that is used to create a container. Usually, we use it to create a docker image from a file that’s called a Dockerfile.
Ways to Create an Image:
a. Take an image from the docker hub.
b. Create an image from the docker file.

  1. Docker Containers:-

➢ Containers are like small packages that hold everything an application needs to run. It is an isolated environment for your code. To create these containers, use the docker run command.
➢ In other words, we can say that the image is a template and the container is a copy of that template.
➢ Container is like a virtual machine.
➢ Images become containers when they run on the docker engine.

  1. docker d(docker demon)- background process. and manage container.

  2. docker CLI - you can access everything using CLI

Dockers can run multiple applications or multiple instances of a single application. It does this with containers.

Hypervisor: A hypervisor is a piece of software or firmware that creates and runs virtual machines on the top of your OS. A hypervisor allows the users to generate multiple instances of complete operating systems.

Firmware: directly run on hardware.

Software: install on any OS.

Dockerfile:

Dockerfile is a text file. It contains some set of instructions. Automation of docker image creation.

# Getting an official Python runtime as the base image from dockerHUb
FROM python:3.9

# Set the working directory in the container where all code will be kept
WORKDIR /app

# Copy the rest of the application code (system -> your container)
COPY . .
# run like a intermediate layer
# Install app dependencies and required library or to make a container 
RUN pip install Flask   # install flask app

# Specify the command to run your application or to run a container 
#CMD ["executable","param1","param2",...]
CMD ["python", "app.py"]
# Set the entry point for the .NET container
#CMD ["dotnet", "YourApp.dll"]

Dockerfile for node js application

FROM node:alpine
WORKDIR /app
COPY package.json .
RUN npm install
CMD ["node","app.js"]

What’s the difference between COPY and ADD instructions?

Answer: Using COPY instruction, We can copy local files and folders from the docker build context to Docker Image. These files and folders will be copied while creating a Docker Image.
ADD instruction works similar to COPY instruction but the only difference is that we can download files from remote locations that are on the Internet while creating a Docker Image.

FROM ubuntu:latest

#COPY myapp.py .
#ADD myapp.py .

#COPY myapp.py example.txt
#ADD myapp.py example.txt

#COPY archive.tar.gz /tmp
#ADD archive.tar.gz /tmp  #automatically extract files

ADD http://www.schweikhardt.net/samefile-2.14.tar.gz /tmp 
# COPY http://www.schweikhardt.net/samefile-2.14.tar.gz /tmp  # can't download

When RUN and CMD instructions will be executed?

Answer: RUN instruction will be executed while building the Docker Image. CMD instruction will be executed while starting the Container.

What’s the difference between CMD and ENTRYPOINT instructions?

Answer: CMD instruction will be used to start the process or application inside the Container.
ENTRYPOINT instruction also works similarly to CMD instruction. ENTRYPOINT instruction will also be executed while creating a container. CMD instruction can be overridden while creating a Container whereas ENTRYPOINT instruction cannot be overridden while creating a Container.

When we have both CMD and ENTRYPOINT instructions in a Dockerfile?

Answer: Both CMD and ENTRYPOINT can be used to specify the command to run in a Docker container, CMD is typically used for providing default commands and arguments that can be easily overridden, and ENTRYPOINT is often used for defining the main command and making it more difficult to override.

If the Dockerfile has multiple CMD instructions, only the last one is effective.

CMD Dockerfile

FROM alpine:latest
CMD ["echo", "Line 1"]

run container

docker build . -t my-container
docker run my-container
# overrides the CMD instruction
docker run my-container echo "Line 2"  #output--> Line 2
docker run my-container "Line 2"  #give error

ENTRYPOINT Dockerfile:- It is not easily overridden by providing a command when running the container.

FROM alpine:latest
ENTRYPOINT ["echo", "Line 1"]

run container

docker build . -t my-container
docker run my-container
#provide additional arguments
docker run my-container "Line 2"  #output--> Line 1 Line 2