Skip to content

Just how can we do to miminize docker images (or container) size

License

Notifications You must be signed in to change notification settings

FistGang/MinimizeDockerImageSize

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 

Repository files navigation

Let's go through each strategy with examples:

  1. Use Minimal Base Images: Instead of using a full-fledged Linux distribution like Ubuntu, you can use Alpine Linux, which is much smaller in size. Here's an example Dockerfile using Alpine Linux:

    # Optimized Dockerfile using Alpine Linux
    FROM alpine:latest
    
    # Install nginx
    RUN apk --no-cache add nginx
    
    # Expose port 80
    EXPOSE 80
    
    # Start nginx
    CMD ["nginx", "-g", "daemon off;"]
  2. Multi-Stage Builds: Suppose you're building a Node.js application. You can use multi-stage builds to compile your application and then copy only the necessary artifacts to the final image. Here's an example Dockerfile:

    # Build Stage
    FROM node:14 AS build
    WORKDIR /app
    
    # Copy package.json and package-lock.json
    COPY package*.json ./
    
    # Install dependencies
    RUN npm install
    
    # Copy the rest of the application code
    COPY . .
    
    # Build the application
    RUN npm run build
    
    # Production Stage
    FROM nginx:alpine
    
    # Copy the build artifacts from the build stage
    COPY --from=build /app/dist /usr/share/nginx/html
    
    # Expose port 80
    EXPOSE 80
    
    # Start nginx
    CMD ["nginx", "-g", "daemon off;"]
  3. Optimize Dockerfile Instructions: Combine multiple RUN commands into a single one to reduce the number of layers. For example:

    # Optimized Dockerfile using Alpine Linux
    FROM alpine:latest
    
    # Combine multiple RUN commands to reduce layers
    RUN apk --no-cache update && apk --no-cache add nginx
    
    # Expose port 80
    EXPOSE 80
    
    # Start nginx
    CMD ["nginx", "-g", "daemon off;"]
  4. Minimize Dependencies: Only install necessary dependencies in your container. For example, if your application only needs Python and Flask, your Dockerfile might look like this:

    # Optimized Dockerfile using Python and Flask
    FROM python:3.9-alpine
    
    # Install only necessary dependencies in a single RUN command
    RUN pip install Flask
    
    # Copy application code
    COPY . /app
    
    # Set the working directory
    WORKDIR /app
    
    # Run the application
    CMD ["python", "app.py"]
  5. Cache Dependencies: Place frequently changing instructions towards the bottom of the Dockerfile to leverage caching. For example, copy the application code as late as possible:

    # Cache Layer Version
    FROM node:14 AS build
    
    # Set the working directory
    WORKDIR /app
    
    # Copy package.json and package-lock.json
    COPY package*.json ./
    
    # Install dependencies (cached)
    RUN npm install
    
    # Copy the rest of the application code
    COPY . .
    
    # Building the application
    RUN npm run build
  6. Compress Artifacts: Before copying files into the container, compress them to reduce size. For example, if you have large static files:

    FROM nginx:alpine
    COPY --from=builder /app/dist /usr/share/nginx/html
  7. Use .dockerignore: Create a .dockerignore file to exclude unnecessary files and directories. For example, exclude node_modules directory for a Node.js application:

    node_modules
    .git
    
  8. Remove Unused Layers: After building your image, remove any unnecessary layers. You can use tools like Docker Squash to squash layers together:

    docker-squash -t new-image old-image
  9. Optimize Runtime Configuration: Configure your application to use resources efficiently at runtime. For example, set appropriate memory limits in your Docker Compose file:

    services:
      web:
        image: nginx:alpine
        deploy:
          resources:
            limits:
              memory: 512M
  10. Regularly Update Base Images: Ensure you regularly update your base images and dependencies to incorporate security patches. You can use tools like Docker Security Scanning to identify vulnerabilities in your images.

We can combine these technique together to build optimized docker images. With Multi-Stage Builds and Combined RUN Commands:

# Build Stage
FROM node:14 AS build
WORKDIR /app

# Copy package.json and package-lock.json, install dependencies, and build the application in a single RUN command
COPY package*.json ./
RUN npm install && \
    npm cache clean --force

# Copy the rest of the application code
COPY . .

# Build the application in a single RUN command
RUN npm run build

# Production Stage
FROM nginx:alpine

# Copy the build artifacts from the build stage
COPY --from=build /app/dist /usr/share/nginx/html

# Expose port 80
EXPOSE 80

# Start nginx
CMD ["nginx", "-g", "daemon off;"]

About

Just how can we do to miminize docker images (or container) size

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published