简体   繁体   English

创建 Azure 容器注册表任务时,如何在文件夹中引用 Dockerfile?

[英]How do I reference a Dockerfile in a folder when creating an Azure Container Registry Task?

I am trying to setup an Azure Container Registry (ACR) to automatically build Docker images when I commit changes to my Git repository.我正在尝试设置 Azure 容器注册表 (ACR) 以在我提交对 Git 存储库的更改时自动构建 Docker 映像。

The problem I'm having is that my Dockerfile is in a folder within the respository.我遇到的问题是我的 Dockerfile 位于存储库内的文件夹中。 This works, in that Azure retrieves the source code and starts to build the Docker image.这是可行的,因为 Azure 检索源代码并开始构建 Docker 映像。 However, the Dockerfile refers to files relative to the location of the Dockerfile (ie inside the folder).但是,Dockerfile 是指相对于 Dockerfile 的位置(即文件夹内)的文件。 When the ACR Task tries to build the image, it complains that the files can't be found.当 ACR 任务尝试构建映像时,它会抱怨找不到文件。 I suspect it's because it's looking for them in the root of the repository, not in the same folder as the Dockerfile.我怀疑这是因为它在存储库的根目录中寻找它们,而不是在与 Dockerfile 相同的文件夹中。

Here's how I created the Task:这是我创建任务的方式:

az acr task create -n MyName -r MyRegistry -c https://mySourceRepository -f MyFolder/Dockerfile -t myimage:{{.Run.ID}} --git-access-token <removed>

The first portion of the Dockerfile looks like this: Dockerfile 的第一部分如下所示:

FROM node:12.16.1-alpine AS build

WORKDIR /usr/src/app
COPY package.json package-lock.json ./
RUN npm install

And the ACR log says this... ACR 日志这样说...

Status: Downloaded newer image for node:12.16.1-alpine
 ---> f77abbe89ac1
Step 2/9 : WORKDIR /usr/src/app
 ---> Running in 22b362ab217c
Removing intermediate container 22b362ab217c
 ---> ee3d0930d0e9
Step 3/9 : COPY package.json package-lock.json ./
COPY failed: stat /var/lib/docker/tmp/docker-builder883638973/package.json: no such file or directory

Is there a way to specify a 'working folder' within the source repository for the ACR Task?有没有办法在 ACR 任务的源存储库中指定一个“工作文件夹”?

For this issue, you can first take a look at the context of the docker build .对于这个问题,大家可以先看一下docker build的上下文。 As it shows, I recommend you make some changes to the Dockerfile.如图所示,我建议您对 Dockerfile 进行一些更改。 The best way is to put the Dockerfile in the root folder of the repository, and then copy files in the Dockerfile with a relative path according to the root folder.最好的办法是把Dockerfile放在repository的根文件夹下,然后根据根文件夹的相对路径复制Dockerfile中的文件。

Of course, you can keep the current directory structure, then you need to change the COPY path relate to your Git repository root folder.当然,您可以保留当前目录结构,然后您需要更改与您的 Git 存储库根文件夹相关的COPY路径。

The solution to this was to specify the folder to use as part of the Context parameter:对此的解决方案是指定要用作 Context 参数的一部分的文件夹:

az acr task create -n MyName -r MyRegistry -c https://mySourceRepository#:MyFolder -f Dockerfile -t myimage:{{.Run.ID}} --git-access-token <removed>

This causes the Docker Build operation to start in the specified folder, so the Dockerfile can find the files to which it refers.这会导致 Docker 构建操作在指定文件夹中开始,因此 Dockerfile 可以找到它所引用的文件。

See here for details of Docker Build Contexts .有关Docker 构建上下文的详细信息,请参见此处。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从 Azure 容器注册表中的映像创建 Dockerfile? - How can I create a Dockerfile FROM an Image in Azure Container Registry? 如何使用资源在 Azure 上创建容器注册表? - How do I create a Container Registry on Azure with a resource? 在 Pulumi Azure Native 中创建 AKS 集群时如何使用现有的容器注册表 - How to use existing Container Registry when creating AKS cluster in Pulumi Azure Native 如何获得指向Azure容器文件夹的正确路径? - How do I get the correct path to a folder of an Azure container? 更新基本映像时,如何更新 azure 容器注册表中的所有映像 - How can I update all images in azure container registry when base image is updated Azure:如何找到容器注册表的所有者或资源 ID? - Azure: How can I find the owner or resource ID of a container registry? 如何以编程方式将容器上传到 Azure 容器注册表 - How to programmatically upload a container to Azure Container Registry Docker任务:如何通过build / release变量设置Azure容器注册表名称或ID? - Docker task : How to set the Azure container registry name or id from build/release variable? 复制 azure 容器注册表时重命名标签 - Rename tags when copy azure container registry How do I authenticate using a managed identity from gitlab-ci to push a docker container from gitlab registry to Azure web service? - How do I authenticate using a managed identity from gitlab-ci to push a docker container from gitlab registry to Azure web service?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM