我有一个
Dockerfile
和 docker-compose.yml
文件,我正在尝试构建映像,然后在推送到存储库后将其发布到 Github 容器注册表。我在 .github/workflows 中有一个publish.yaml 文件,这就是我正在做的地方。
我的问题是,我想使用在publish.yaml文件中的一个步骤中提取docker元数据而获得的标签和标签,然后通过
docker compose build
将这些标签和标签添加到图像中。所以我不能只是把它们放在 docker-compose 文件中。
我的publish.yml 文件如下所示:
on:
push:
branches:
- main
- docker
tags:
- v*
env:
REGISTRY: ghcr.io
IMAGE_NAME: ${{ github.repository }}
jobs:
build-and-publish-image:
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
attestations: write
steps:
- name: Check out the repo
uses: actions/checkout@v3
- name: Setup Docker buildx
uses: docker/setup-buildx-action@v2
- name: Log in to GitHub Container registry
uses: docker/login-action@v2
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Extract metadata (tags, labels) for Docker
id: meta
uses: docker/metadata-action@v4
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
- name: Remove and stop docker compose containers
run: docker compose -f docker-compose.yml down
###This is the problem step:###
- name: Build Docker image with docker compose
run: docker compose -f docker-compose.yml --progress=plain build --pull --no-cache --tag ${{ steps.meta.outputs.tags }} --label ${{ steps.meta.outputs.labels }}
- name: Push Docker image
run: docker push ${{ env.REGISTRY }}/{{env.IMAGE_NAME }}:latest
我在 github actions 中的“Build Docker image with docker compose”部分收到错误:
未知标志:--tag 错误:进程已完成,退出代码为 16。
因为根据 docker compose cli 和 compose build cli 参考文档,
docker compose build
似乎没有 --tag
那样的 --label
或 docker build
选项。
看完这个:Docker-compose 标记和推送,以及 docker compose cli 参考文档,我现在这样做:
###This is the problem step:###
- name: Build Docker image with docker compose
run: |
docker compose -f docker-compose.yml --progress=plain build --pull --no-cache
docker tag ${{env.IMAGE_NAME }} ${{ steps.meta.outputs.tags }}
- name: Push Docker image
run: docker push ${{ env.REGISTRY }}/{{env.IMAGE_NAME }}:latest
但我还是不知道如何把
${{ steps.meta.outputs.labels }}
的标签贴到上面。我该怎么办?有没有更好的方法来整体做到这一点?
我想使用
docker/build-push-action@v4
,就像本网站上的示例一样,但我需要它通过撰写构建。我真的不明白从这里的答案中该怎么做:How to tag docker image with docker-compose
我在我的问题下的评论中从 Benjamin W. 那里了解到了 docker buildx Baker。我最终使用
docker/metadata-action
和 docker/bake-action
来获得我想要的东西,而不是 docker Baker CLI。不过,我确实包含了我认为相当于我的 docker-action 的 CLI 内容。
{{ $outputs.labels }}
和{{ $outputs.tags }}
,这是我最初使用的。
但它ALSO有烘焙文件的输出,
{{ $outputs.bake-file-tags }}
和{{ $outputs.bake-file-labels }}
,您可以在此处阅读更多信息:元数据输出,并且您可以在此处阅读有关bake文件的更多信息。
这些文件与 bake 操作一起使用,可以应用多个标签和标签,并从我编写的
docker-compose.yml
文件中构建和推送我的图像。
这是我更新的 yml 文件:
steps:
- name: Checkout the repository
uses: actions/checkout@v4
- name: Setup Docker buildx
uses: docker/setup-buildx-action@v3
- name: Log in to GitHub Container registry
uses: docker/login-action@v3
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Extract metadata (tags, labels) for Docker
id: meta
uses: docker/metadata-action@v3
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
tags: |
# tag with version number upon "git push tag"
type=match,pattern=v(.*),group=1
# set latest tag for default branch
type=raw,value=latest,enable={{is_default_branch}}
- name: Build & Push Docker image with Github Bake Action
uses: docker/bake-action@v4
with:
source: .
push: true
pull: true
files: |
./docker-compose.yml
${{ steps.meta.outputs.bake-file-tags }}
${{ steps.meta.outputs.bake-file-labels }}
targets: myApp
我认为 CLI 相当于最后一步:
- name: Build Docker image with docker bake
run: docker buildx bake -f docker-compose.yml -f ${{ steps.meta.outputs.bake-file-tags }} -f ${{ steps.meta.outputs.bake-file-labels }} --progress=plain --pull --push myApp