What is virtual machine?
- A VM is a virtualized environment of a physical computer.
- It can perform almost all of the same functions, including running applications and operating systems.
Working for virtual machine
Hypervisor
- The functioning of virtual machines involves a hypervisor, also known as a Virtual Machine Monitor (VMM), which acts as a layer of software or firmware between the physical hardware and the virtual machines.
- The hypervisor allocates and manages resources such as virtual CPUs, memory, storage, and network interfaces that allow multiple VMs to run on the same physical server concurrently.
Types of hypervisor
- Type 1 (bare metal) runs directly on the hardware
- Type 2 (hosted) runs on top of an existing operating system
Docker
- One of the most popular open-source containerized platforms is known as Docker.
- It allows developers to create containers for the applications they develop.
Docker Works
Docker Terminology
- Docker Engine
- Docker File
- Docker Image
- Docker Container
- Docker Registry
- Docker Doemon
- Docker Network & Volume
Docker Image
- It is a lightweight, executable package that consists of everything needed to run an application : code, libraries, third-party dependencies, environment variables, and configurations.
Docker Container
- It is the place where Docker runs an image.
- Images become containers when they run on Docker Engine.
Docker Registry
- It is a storage and distribution system for Docker images which are tagged and have a name.
Docker Doemon
- It is a daemon process that runs on the host operating system.
- It is responsible for running containers to manage docker services.
- It manages the Docker objects such as images, containers, networking, and storage.
- They handle the entire process, ensuring everything runs smoothly and efficiently.
Docker Architecture
- Docker Architecture is a Client Sever Architecture.
Docker File Creation Basic Structure of file creation. Example: Using java
# 1. Set the base image to platform
FROM openjdk:17-alpine
# 2. Labeling (Option)
LABEL Key:Value
# 3. Create a Working Directory
WORKDIR /app
# 4. Copy the src or java files
COPY HelloWorld.java /app
# 5. Compile the java file
RUN javac HelloWorld.java
# 6. Expose the port (Option or Web Application Only)
EXPOSE 5000
# 7. Set the entry point to run the compiled Java program
ENTRYPOINT ["java", "HelloWorld"]
Explanation:
- Base Image:
openjdk:17-alpine
is a lightweight Docker image that includes OpenJDK 17. - Working Directory: The
WORKDIR
command sets/hello
as the working directory inside the container. - Copy: The
COPY
command copiesHelloWorld.java
from your local system into the/hello
directory in the container. - Compile: The
RUN
command compiles the Java file inside the container. - Entrypoint: The
ENTRYPOINT
command specifies the command to run the compiled Java program (HelloWorld
) when the container starts.
Command | Executed At | Purpose | Can Be Overridden? |
---|---|---|---|
RUN |
Image build time | Executes commands to build the image (e.g., install software) | No, part of the image build process |
ENTRYPOINT |
Container start time | Defines the main application or process to run when the container starts | No (unless explicitly overridden using --entrypoint ) |
CMD |
Container start time | Provides default arguments or command for the container | Yes (can be overridden in docker run ) |
Best Practices:
- Use
RUN
for building and configuring the environment (e.g., installing dependencies, compiling code). - Use
ENTRYPOINT
for defining the main application or process that should always run in the container. - Use
CMD
for providing default arguments to theENTRYPOINT
or to define a default command if noENTRYPOINT
is provided.
Docker Ignore
The .dockerignore
file is used to specify which files and directories should be excluded when building a Docker image. It works similarly to a .gitignore
file in Git. By specifying patterns in .dockerignore
, you can prevent unnecessary files from being copied into the Docker image, helping reduce the image size and build time.
Why Use .dockerignore
:
- Efficiency: It helps reduce the size of the build context sent to the Docker daemon during image builds.
- Security: Sensitive files (e.g., environment files, API keys) can be excluded from being added to the image.
- Performance: Excluding unnecessary files speeds up the build process.
Example of a .dockerignore
File:
# Ignore node_modules directory
node_modules
# Ignore logs and temp files
*.log
tmp/
# Ignore any .env file (contains sensitive data)
.env
# Ignore Git-related files
.git
.gitignore