跳到主要内容

3.1 容器化工具

容器化 是一种软件开发和部署技术,它将应用程序及其所有依赖项(如库、配置文件等)打包到一个轻量级、独立的容器中。容器可以在任何支持容器运行时的环境中运行,从而确保应用程序在不同环境中的一致性和可移植性。 这里列出一些常用的构建容器镜像的工具。

Docker CLI

最常用的容器镜像构建工具,直接使用 Dockerfile 构建镜像。

示例:

docker build -t my-image:latest .

如果没有安装 docker,可参考以下安装指南。

官方安装指南

Ubuntu 安装

sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin

Debian 安装

sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin

Buildah

Buildah 专门用于构建 OCI 镜像。Buildah 的命令复制了 Dockerfile 中的所有命令。无需 Docker 守护进程。

安装

https://github.com/containers/buildah/blob/main/install.md

示例

buildah bud -t my-image:latest .
buildah push my-image:latest docker://my-registry/my-image:latest

Kaniko

kaniko 是一个用于在容器或 Kubernetes 集群内根据 Dockerfile 构建容器镜像的工具。无需 Docker 守护进程。 kaniko 以容器镜像发布:

gcr.io/kaniko-project/executor:v1.23.2
gcr.io/kaniko-project/executor:latest

使用指南

参考:https://github.com/GoogleContainerTools/kaniko?tab=readme-ov-file#using-kaniko

ko

快速构建和推送容器镜像的工具,专为 Go 应用设计。无需 Dockerfile, 自动将 Go 应用打包为容器镜像, 简化了容器化流程。非常适合在 CI/CD 环境使用。

安装

官方安装指南: https://ko.build/install/

  • 从 github release
    # 设置
    VERSION=TODO # choose the latest version (without v prefix)
    OS=Linux # or Darwin
    ARCH=x86_64 # or arm64, i386, s390x

    # 下载&验证包
    curl -sSfL "https://github.com/ko-build/ko/releases/download/v${VERSION}/ko_${VERSION}_${OS}_${ARCH}.tar.gz" > ko.tar.gz
    $ curl -sSfL https://github.com/ko-build/ko/releases/download/v${VERSION}/multiple.intoto.jsonl > multiple.intoto.jsonl
    $ slsa-verifier verify-artifact --provenance-path multiple.intoto.jsonl --source-uri github.com/ko-build/ko --source-tag "v${VERSION}" ko.tar.gz

    # 解压
    tar xzf ko.tar.gz ko
    chmod +x ./ko
  • Homebrew
    brew install ko
  • MacPorts
    sudo port install ko
  • Windows scoop
    scoop install ko
  • Alpine Linux
    echo https://dl-cdn.alpinelinux.org/alpine/edge/testing/ >> /etc/apk/repositories
    apk update
    apk add ko