https://github.com/pratikshinde55/docker-image-optimization
Different approach to optimize image
https://github.com/pratikshinde55/docker-image-optimization
Last synced: 2 months ago
JSON representation
Different approach to optimize image
- Host: GitHub
- URL: https://github.com/pratikshinde55/docker-image-optimization
- Owner: Pratikshinde55
- Created: 2025-03-06T09:41:43.000Z (3 months ago)
- Default Branch: main
- Last Pushed: 2025-03-06T09:50:40.000Z (3 months ago)
- Last Synced: 2025-03-06T10:36:24.927Z (3 months ago)
- Size: 0 Bytes
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Docker-image-optimization
## Image Optimization:
- When we run a Container from an image, It gets loaded into RAM. This means that if image size is large the entire large of data is loaded into RAM.
- If image is large, it gets more time to load inti RAM, which in turn increases the time required to launch Container.
- The image size directly impacks performance.
- A large image size image size results in higher CPU & RAM usage, leading to increase costs.## Some Image Optimization methods:
1. **Build Tool**
2. **Image layering**
3. **Multi-Stage Build**
4. **Distroless-images**
5. **Alpine image**
6. **Scratch image**# 1. Docker Build tool
## Using legacy Docker build tool we can see how metadata is kept:
Use old docker build tool :export DOCKER_BUILDKIT=0

docker build -t old-flask:v1 .

docker history old-flask:v1
All metedata is kept as it is.

## Using Integrated Docker build tool-Buildkit
Now docker pre-integrated with new Build tool that is BuildKitexport DOCKER_BUILDKIT=1
Build Image:
docker build -t new-flask:v1 --no-cache .
docker build -t new-flask:v1 .
docker history new-flask:v1
All metadata is removed showing missing
# 2. Image layering:
we can reduce the layers in Dockerfile by using some keywords like **"&&"**.Number of stages/steps decreases.If we reduce some layers then intermediate images and containers are less, So building is fast, & Docker BuildKIT uses **Parallelism**.
docker build -t layers-cut:v1 .
# 3. Multi-Stage build
Now, use C programming code do multi-stage build Dockerfile. Because Multi-stage build we use depends on use case to use case, for python we don't need of compile but for Java and C/C++ we need compile.## without Multi-stage build means Single-Stage build :
This is simple C app, & Single-Build Stage Docker file compile & run the app.c
docker build -t c-app:v1 -f singleB-Dockerfile .

**See the size of image:**

Launch Docker Container:docker run -it c-app:v1
## Using Multi-Build Stage:
Here i use two Build stage, In 1st build stage i use gcc image which is neccessary to compile C-app this image size is very large & then i use ubuntu image which is small and also have C runtime to run C-app.
docker build -t multic-app:v1 -f multiB-Dockerfile --no-cache .

**See small size we found:**
docker run multic-app:v1
# 4. Distroless image:
This images do not include anything from any distribution; They provide only bare minimum, such as the C runtime that we need.
(Serch on google: distroless images --> Distroless" Container Images. - GitHub)docker build -t distroless-app:v1 -f ditroless-Dockerfile .

docker images distroless-app:v1
docker run -it distroless-app:v1

# 5. Alpine image:
This images size is very small & you can install the runtime or program that you neeed in that image.(Search on DockerHub)docker build -t alpine-app:v1 -f alpine-Dockerfile:v1 .

**Image size is very small:**
docker run -it alpine-app:v1

# 6. Scratch image:
Scratch image is smallest image ever.**an explicitly empty image, especially for building images "FROM scratch"**
**Static Linking:** By using gcc `-static`, we ensuring that the binary includes everything it needs to run, so it can execute on the **scratch image** without needing shared libraries like libc.
**Static Compilation:** The key part here is `RUN gcc -static app.c -o app`
docker build -t scratch-app:v1 -f scratch-Dockerfile .

**Image size smallest now:**
docker run --rm scratch-app:v1
