Docker 容器构建驱动
Docker 容器驱动程序允许在专用 Docker 容器中创建受管理且可定制的 BuildKit 环境。
使用Docker容器驱动程序相比默认的Docker驱动程序有几个优势。例如:
概要
运行以下命令以创建一个名为 container 的新构建器,该构建器使用 Docker 容器驱动程序:
$ docker buildx create \
--name container \
--driver=docker-container \
--driver-opt=[key=value,...]
container
下表描述了可以传递给--driver-opt的可用驱动程序特定选项:
| Parameter | Type | Default | Description |
|---|---|---|---|
image | String | Sets the BuildKit image to use for the container. | |
memory | String | Sets the amount of memory the container can use. | |
memory-swap | String | Sets the memory swap limit for the container. | |
cpu-quota | String | Imposes a CPU CFS quota on the container. | |
cpu-period | String | Sets the CPU CFS scheduler period for the container. | |
cpu-shares | String | Configures CPU shares (relative weight) of the container. | |
cpuset-cpus | String | Limits the set of CPU cores the container can use. | |
cpuset-mems | String | Limits the set of CPU memory nodes the container can use. | |
default-load | Boolean | false | Automatically load images to the Docker Engine image store. |
network | String | Sets the network mode for the container. | |
cgroup-parent | String | /docker/buildx | Sets the cgroup parent of the container if Docker is using the "cgroupfs" driver. |
restart-policy | String | unless-stopped | Sets the container's 重启策略. |
env.<key> | String | Sets the environment variable key to the specified value in the container. |
在配置容器的资源限制之前,请阅读有关 为容器配置运行时资源约束的内容。
用法
当你运行一个构建时,Buildx 会拉取指定的 image(默认情况下,
moby/buildkit)。
当容器启动后,Buildx 会将构建提交到容器化的构建服务器。
$ docker buildx build -t <image> --builder=container .
WARNING: No output specified with docker-container driver. Build result will only remain in the build cache. To push result image into registry use --push or to load image into docker use --load
#1 [internal] booting buildkit
#1 pulling image moby/buildkit:buildx-stable-1
#1 pulling image moby/buildkit:buildx-stable-1 1.9s done
#1 creating container buildx_buildkit_container0
#1 creating container buildx_buildkit_container0 0.5s done
#1 DONE 2.4s
...
缓存持久化
docker-container 驱动程序支持缓存持久化,因为它将所有 BuildKit 状态和相关缓存存储到一个专用的 Docker 卷中。
为了持久化docker-container驱动程序的缓存,即使在通过docker buildx rm和docker buildx create重新创建驱动程序后,你也可以使用--keep-state标志来销毁构建器:
例如,要创建一个名为 container 的构建器,然后在保留状态的情况下删除它:
# setup a builder
$ docker buildx create --name=container --driver=docker-container --use --bootstrap
container
$ docker buildx ls
NAME/NODE DRIVER/ENDPOINT STATUS BUILDKIT PLATFORMS
container * docker-container
container0 desktop-linux running v0.10.5 linux/amd64
$ docker volume ls
DRIVER VOLUME NAME
local buildx_buildkit_container0_state
# remove the builder while persisting state
$ docker buildx rm --keep-state container
$ docker volume ls
DRIVER VOLUME NAME
local buildx_buildkit_container0_state
# the newly created driver with the same name will have all the state of the previous one!
$ docker buildx create --name=container --driver=docker-container --use --bootstrap
container
QEMU
docker-container 驱动程序支持使用
QEMU
(用户模式)来构建非本地平台。使用 --platform 标志来指定
你想要构建的架构。
例如,为amd64和arm64构建一个Linux镜像:
$ docker buildx build \
--builder=container \
--platform=linux/amd64,linux/arm64 \
-t <registry>/<image> \
--push .
注意
使用QEMU进行模拟可能会比本地构建慢得多,特别是对于计算密集型任务,如编译和压缩或解压缩。
自定义网络
您可以自定义构建器容器使用的网络。如果您需要为构建使用特定网络,这将非常有用。
例如,让我们
创建一个网络
命名为 foonet:
$ docker network create foonet
现在创建一个
docker-container 构建器
,它将使用这个网络:
$ docker buildx create --use \
--name mybuilder \
--driver docker-container \
--driver-opt "network=foonet"
启动并
检查 mybuilder:
$ docker buildx inspect --bootstrap
检查构建器容器 并查看正在使用的网络:
$ docker inspect buildx_buildkit_mybuilder0 --format={{.NetworkSettings.Networks}}
map[foonet:0xc00018c0c0]
进一步阅读
有关Docker容器驱动程序的更多信息,请参阅 buildx参考。