获取Build.Repository.LocalPath的数据并在我的DockerFile中使用它 [英] Get the data of Build.Repository.LocalPath and used it in my DockerFile

查看:58
本文介绍了获取Build.Repository.LocalPath的数据并在我的DockerFile中使用它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从变量 Build.Repository.LocalPath 中获取数据并在我的Dockerfile中使用它,但它显示了我和错误.

I want to get the data from the variable Build.Repository.LocalPath and use it in my Dockerfile, but it shows me and error.

这是我的dockerfile:

This is my dockerfile:

FROM microsoft/aspnet:latest

COPY "/${Build.Repository.LocalPath}/NH.Services.WebApi/bin/Release/Publish/" /inetpub/wwwroot

我收到此错误:

Step 2/9 : COPY "/${Build.Repository.LocalPath}/NH.Services.WebApi/bin/Release/Publish/" /inetpub/wwwroot
failed to process "\"/${Build.Repository.LocalPath}/NH.Services.WebApi/bin/Release/Publish/\"": missing ':' in substitution
##[error]C:\Program Files\Docker\docker.exe failed with return code: 1

我尝试了很多方法,将这一行放在这行:

I have try a lot of ways, putting this line:

COPY "/${Build.Repository.LocalPath}/NH.Services.WebApi/bin/Release/Publish/" /inetpub/wwwroot

推荐答案

您可以在Dockerfile中添加一个参数:

You can add in the Dockerfile an argument:

ARG path

在Azure DevOps Docker任务中添加一个参数:

In the Azure DevOps Docker task add an argument:

-task: Docker@2
 inputs:
   command: build
   arguments: --build-arg path=$(Build.Repository.LocalPath)

现在Dockerfile知道变量值,您可以使用它,例如:

Now the Dockerfile know the variable value and you can use it, for example:

FROM ubuntu:latest
ARG path
ECHO $path

结果:

Step 3/13 : RUN echo $path
---> Running in 213dsa3dacv
/home/vsts/work/1/s

但是,如果您尝试以这种方式复制应用,则:

But if you will try to copy the appliaction in this way:

FROM microsoft/aspnet:latest
ARG path
COPY $path/README.md /inetpub/wwwroot

您会收到一个错误:

复制失败:CreateFile \?\ C:\ ProgramData \ docker \ tmp \ docker-builder437597591 \ _work \ 1 \ s \ README.md:系统找不到指定的路径.

COPY faild: CreateFile \?\C:\ProgramData\docker\tmp\docker-builder437597591\_work\1\s\README.md: The system cannot find the path specified.

这是因为Docker在一个临时文件夹中构建映像,并且他将源复制到该文件夹​​中,但是他没有复制代理文件夹(_work/1/s),因此最好的方法是将相对路径放置在例如,存在Dockerfile(如果Dockerfile与README.md存在):

It's because the Docker build the image within a temporary folder and he copy the sources to there, but he doesn't copy the agent folders (_work/1/s) so the best way it's just put a relative path where the Dockerfile exist, for example (if the Dockerfile exist with the README.md):

FROM microsoft/aspnet:latest
COPY README.md /inetpub/wwwroot

这篇关于获取Build.Repository.LocalPath的数据并在我的DockerFile中使用它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆