gpt4 book ai didi

docker - 如何在没有from标志的情况下编写多阶段Dockerfile

转载 作者:行者123 更新时间:2023-12-02 20:37:26 25 4
gpt4 key购买 nike

这实际上是我今天问的this question的延续。

我有一个使用--from标志的多阶段Dockerfile:

FROM docker.m.our-intra.net/microsoft/dotnet:2.1-sdk
WORKDIR /app
COPY . ./aspnetapp/
WORKDIR /app/aspnetapp
RUN dotnet publish -c Release -o out

FROM docker.m.our-intra.net/microsoft/dotnet:2.1.4-aspnetcore-runtime
WORKDIR /app
COPY --from=docker.m.our-intra.net/microsoft/dotnet:2.1-sdk /app/aspnetapp/MyProject.WebApi/out ./
ENTRYPOINT ["dotnet", "MyProject.WebApi.dll"]

借助此文件,我能够在本地成功构建镜像。

但是我无法在我的Jenkins管道中使用此Dockerfile,因为Jenkins Server引擎的版本低于17.05,并且不会进行更新(也许稍后但现在不进行更新)。

我是Docker和Jenkins的新手。如果有人可以帮助我修改Dockerfile,而无需使用 --from标志,我将不胜感激。

更新:

上面提到的 Dockerfile错误。借助于 Dockerfile的工作版本,我可以在本地计算机上成功构建镜像并成功运行该应用程序,该版本如下:
FROM docker.m.our-intra.net/microsoft/dotnet:2.1-sdk AS build
WORKDIR /app
COPY . ./aspnetapp/
WORKDIR /app/aspnetapp
RUN dotnet publish -c Release -o out

FROM docker.m.our-intra.net/microsoft/dotnet:2.1.4-aspnetcore-runtime AS runtime
WORKDIR /app
COPY --from=build /app/aspnetapp/MyProject.WebApi/out ./
ENTRYPOINT ["dotnet", "MyProject.WebApi.dll"]

更新2:

我正在尝试遵循Carlos的建议,现在我有两个docker文件。

这是我的 Docker-build:
FROM docker.m.our-intra.net/microsoft/dotnet:2.1-sdk
WORKDIR /app
COPY . ./aspnetapp/
WORKDIR /app/aspnetapp
RUN dotnet publish -c Release -o out

这是我的 Dockerfile:
FROM docker.m.our-intra.net/microsoft/dotnet:2.1.4-aspnetcore-runtime
COPY . .
ENTRYPOINT ["dotnet", "MyProject.WebApi.dll"]

这是我的 Jenkinsfile:
def docker_repository_url = 'docker.m.our-intra.net'
def artifact_group = 'some-artifact-group'
def artifact_name = 'my-service-api'

pipeline {
agent {
label 'build'
}
stages {
stage('Checkout') {
steps {
script {
echo 'Checkout...'
checkout scm
echo 'Checkout Completed'
}
}
}
stage('Build') {
steps {
script {
echo 'Build...'
sh 'docker version'
sh 'docker build -t fact:v${BUILD_NUMBER} -f Dockerfile-build .'
echo 'Build Completed'
}
}
}
stage('Extract artifact') {
steps {
script {
echo 'Extract...'
sh 'docker create --name build-stage-container fact:v${BUILD_NUMBER}'
sh 'docker cp build-stage-container:/app/aspnetapp/MyProject.WebApi/out .'
sh 'docker rm -f build-stage-container'
echo 'Extract Completed'
}
}
}
stage('Copy compiled artifact') {
steps {
script {
echo 'Copy artifact...'
sh "docker build -t ${docker_repository_url}/${artifact_group}/${artifact_name}:v${BUILD_NUMBER} -f Dockerfile ."
echo 'Copy artifact Completed'
}
}
}
stage('Push image') {
steps {
script {
withCredentials([[
$class: 'UsernamePasswordMultiBinding',
credentialsId: 'jenkins',
usernameVariable: 'USERNAME',
passwordVariable: 'PASSWORD'
]]) {
def username = env.USERNAME
def password = env.PASSWORD

echo 'Login...'
sh "docker login ${docker_repository_url} -u ${username} -p ${password}"
echo 'Login Successful'

echo 'Push image...'
sh "docker push ${docker_repository_url}/${artifact_group}/${artifact_name}:v${BUILD_NUMBER}"
echo 'Push image Completed'
}
}
}
}
}
}

所有步骤都成功,但是当我尝试在本地运行镜像(从Maven中提取镜像)或在OpehShift群集上运行镜像时,它失败并显示:

您是要运行dotnet SDK命令吗?请从以下位置安装dotnet SDK:
http://go.microsoft.com/fwlink/?LinkID=798306&clcid=0x409


我究竟做错了什么?

最佳答案

TL; DR:您需要自己在Docker外部复制基础功能

首先,您使用了--from选项错误。要从上一个构建阶段you must refer to its index or its name复制,例如:

FROM docker.m.our-intra.net/microsoft/dotnet:2.1-sdk
...

FROM docker.m.our-intra.net/microsoft/dotnet:2.1.4-aspnetcore-runtime
COPY --from=0 /app/aspnetapp/MyProject.WebApi/out ./

要么
FROM docker.m.our-intra.net/microsoft/dotnet:2.1-sdk AS build-stage
...
FROM docker.m.our-intra.net/microsoft/dotnet:2.1.4-aspnetcore-runtime
COPY --from=build-stage /app/aspnetapp/MyProject.WebApi/out ./

使用当前的Dockerfile,它将尝试从上游Docker镜像而不是先前的构建阶段复制文件。

其次,您无法使用17.05之前的版本进行多阶段Docker构建。您需要自己在Docker外部复制基础功能。

为此,您可以使用一个Dockerfile来构建您的 Artifact ,并基于该镜像运行一个废弃的容器,从中提取 Artifact 。您不需要运行容器,只需 create it with docker create (这将创建可写容器层):
docker create --name build-stage-container build-stage-image
docker cp build-stage-container:/app/aspnetapp/MyProject.WebApi/out .

然后,您可以使用第二个Dockerfile来构建镜像,该镜像使用从构建上下文中获取的简单 COPY复制从上一阶段提取的 Artifact 。

关于docker - 如何在没有from标志的情况下编写多阶段Dockerfile,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53707205/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com