对docker镜像进行压缩镜像大小

166 Visits / 0 Comments / Favorite

现在项目构建出来的Docker非常大,已经到了392M了,有必要缩小Docker 镜像的大小,在传输部署的时候可以加快部署速度,这里介绍一种方法进行压缩。

Slim

Slim 在不更改 Docker 容器镜像的任何内容,可以显著缩小镜像大小。 它将通过使用各种分析技术了解您的应用程序及其需求来优化和保护您的容器。它会丢弃你不需要的东西,减少容器的攻击面。

首先在服务器安装slim:

1、拉取bash脚本,因为国内特殊情况,可能拿不到脚本。所以这里直接显示脚本:

#!/usr/bin/env bash
function get_slim() {
  local DIST=""
  local EXT=""
  local FILENAME=""
  local KERNEL=""
  local MACHINE=""
  local TMP_DIR=""
  local URL=""
  local VER=""

  if [ -n "$1" ]; then
    VER=$1
  else
    # Get the current released tag_name
    VER=$(curl -sL https://api.github.com/repos/slimtoolkit/slim/releases \
        | grep tag_name | head -n1 | cut -d'"' -f4)
  fi

  if [ -n "${VER}" ]; then
    URL="https://downloads.dockerslim.com/releases/${VER}"
  else
    echo "ERROR! Could not retrieve the current Slim version number."
    exit 1
  fi

  # Get kernel name and machine architecture.
  KERNEL=$(uname -s)
  MACHINE=$(uname -m)

  # Determine the target distrubution
  if [ "${KERNEL}" == "Linux" ]; then
    EXT="tar.gz"
    if [ "${MACHINE}" == "x86_64" ]; then
      DIST="linux"
    elif [ "${MACHINE}" == "armv7l" ]; then
      DIST="linux_arm"
    elif [ "${MACHINE}" == "aarch64" ]; then
      DIST="linux_arm64"
    fi
  elif [ "${KERNEL}" == "Darwin" ]; then
    EXT="zip"
    if [ "${MACHINE}" == "x86_64" ]; then
      DIST="mac"
    elif [ "${MACHINE}" == "arm64" ]; then
      DIST="mac_m1"
    fi
  else
    echo "ERROR! ${KERNEL} is not a supported platform."
    exit 1
  fi

  # Was a known distribution detected?
  if [ -z "${DIST}" ]; then
    echo "ERROR! ${MACHINE} is not a supported architecture."
    exit 1
  fi

  # Derive the filename
  FILENAME="dist_${DIST}.${EXT}"

  echo " - Downloading ${URL}/${FILENAME}"
  TMP_DIR=$(mktemp -d)
  curl -sLo "${TMP_DIR}/${FILENAME}" "${URL}/${FILENAME}"

  echo " - Unpacking ${FILENAME}"
  if [ "${EXT}" == "zip" ]; then
    unzip -qq -o "${TMP_DIR}/${FILENAME}" -d "${TMP_DIR}"
  elif [ "${EXT}" == "tar.gz" ]; then
    tar -xf "${TMP_DIR}/${FILENAME}" --directory "${TMP_DIR}"
  else
    echo "ERROR! Unexpected file extension."
    exit 1
  fi

  # /usr/local/bin should be present on Linux and macOS hosts. Just be sure.
  if [ -d /usr/local/bin ]; then
    echo " - Placing slim in /usr/local/bin"
    mv "${TMP_DIR}/dist_${DIST}/slim" /usr/local/bin/
    mv "${TMP_DIR}/dist_${DIST}/slim-sensor" /usr/local/bin/
    chmod +x /usr/local/bin/slim
    chmod +x /usr/local/bin/slim-sensor

    echo " - Cleaning up"
    rm -rf "${TMP_DIR}"
    echo -en " - "
    slim --version
  else
    echo "ERROR! /usr/local/bin is not present. Install aborted."
    rm -rf "${TMP_DIR}"
    exit 1
  fi
}

echo "Slim scripted install"

if [ "$(id -u)" -ne 0 ]; then
  echo "ERROR! You must run this script as root."
  exit 1
fi

get_slim $1

# You can pass a specific version to install otherwise the latest version will be installed

在本地新建一个install.sh文件,并且进行赋执行权限即可;

root@xx:~# ./install-slim.sh
Slim scripted installDownloading https://downloads.dockerslim.com/releases/1.40.11/dist_linux.tar.gzUnpacking dist_linux.tar.gzPlacing slim in /usr/local/binCleaning upslim version 

linux/amd64|Transformer|1.40.11|1b271555882eacdfb4e6598d6d0552e9b9b1449b|2024-02-02_01:36:22PM

我们先看看我们的镜像文件大小:

image.png

我们执行这个命令:

image.png

docker-slim build --http-probe=false --target nginx:latest --tag nginx:slim

默认会开启http的探测(--http-probe)。我们build的时候给他关上

输出的过程如下:

image.png

我们再来看看slim之后的镜像大小:

image.png

镜像大小从:392->244M,至此,本次教程就到这里了。后面我们会分析slim是如何进行瘦身的:)

All comments

Top