在Ansible中使用BuildKit构建docker



我使用这个Ansible模块community.docker.docker_image来构建docker映像。我想使用--secret标志,因此我需要启用BuildKit。

我编辑了/etc/docker/daemon.json文件并添加了"features": { "buildkit": true }行,然后重新启动了docker服务。

但是Ansible运行时仍然显示错误;

fatal: [localhost]: FAILED! => {"changed": false, "msg": "Error building local:5000/test - code: None, message: the --mount option requires BuildKit. Refer to https://docs.docker.com/go/buildkit/ to learn how to build images with BuildKit enabled ...

ansible任务:

- name: Build an image and push it to a private repo
community.docker.docker_image:
build:
path: "{{ role_path }}/files/test"
args:
secret: 'id=test_app_secret_id,src={{ tempfile_1.path }}'
name: local:5000/test
tag: v1.1.2
push: yes
force_tag: yes
source: build

和在Dockerfile我使用的秘密如下:

RUN --mount=type=secret,id=test_app_secret_id

对于如何解决这个问题有什么想法吗?

使用buildkit构建不(截至2022年12月13日…)由Ansiblecommunity.docker.docker_image模块支持。引用文档中的注释

构建镜像使用Docker守护进程的API完成。不能这样使用BuildKit/buildx。

因此,目前的解决方案是通过shell设置正确的环境。比如:

- name: Build an image with BuildKit and push it to a private repo
vars:
image: local:5000/test
tag: v1.1.2
ansible.builtin.shell:
cmd: |-
docker build --secret "id=test_app_secret_id,src={{ tempfile_1.path }}" -t {{ image }}:{{ tag }} .
docker push {{ image }}:{{ tag }}
chdir: "{{ role_path }}/files/test"
environment: 
DOCKER_BUILDKIT: 1

最新更新