目前我真的很盲目,不明白我的问题是什么,我希望你的一双新眼睛可以帮助我。
我有以下 Dockerfile
ARG base_image=mcr.microsoft.com/dotnet/runtime
ARG base_image_version=8.0
ARG port=80
# Use a runtime image as the base image
#FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS runtime
FROM ${base_image}:${base_image_version} AS runtime
# The name of DLL which needs to be used for starting the application
ARG DLL_NAME
ENV env_dll_name=${DLL_NAME}
# Set the working directory in the container
WORKDIR /app
# Copy the published application files
COPY --chown=$APP_UID:$APP_UID . ./
# Change ownership of the files to the dedicated user
RUN chown -R $APP_UID:$APP_UID /app
# Switch to the dedicated user
USER $APP_UID
# List the contents of the /app directory to verify the files copied
RUN ls -la /app
# List the DLL to be executed
RUN echo "DLL to execute: ${env_dll_name}"
# Expose the port that the application will listen on
EXPOSE ${port}
RUN ls -la .
# Define the entry point for the container
ENTRYPOINT ["sh", "-c", "dotnet ${env_dll_name}"]
Azure DevOps Pipeline 运行以下任务
- script: |
pwd
ls -la .
ls -la $(Build.ArtifactStagingDirectory)
ls -la $(Build.ArtifactStagingDirectory)/PublishOutput
- task: Docker@2 displayName: 'Docker - Build' inputs:
command: build
#containerRegistry: $(ACR_LOGIN_SERVER)
repository: $(ACR_LOGIN_SERVER)/$(dockerImageName)
Dockerfile: '$(Build.Repository.LocalPath)/Dockerfile' # Path to your Dockerfile
buildContext: '$(Build.ArtifactStagingDirectory)/PublishOutput'
arguments: '--progress=plain --build-arg DLL_NAME=$(project_name).dll'
tags: |-
$(releaseVersion)
latest
RUN ls -la
行显示预期的文件夹内容。
但是当容器在 AKS 上启动时,它会失败,因为找不到 dll。
我已将入口点替换为
CMD ["sh", "-c", "ls -la /app && sleep 3600"]
并且已经只看到以下
drwxrwxrwx 3 root root 4096 Jul 8 17:37 .
drwxr-xr-x 1 root root 4096 Jul 9 04:38 ..
drwxr-xr-x 2 root root 4096 Jul 8 17:37 ..2024_07_08_17_37_44.4116578998
lrwxrwxrwx 1 root root 32 Jul 8 17:37 ..data -> ..2024_07_08_17_37_44.4116578998
lrwxrwxrwx 1 root root 23 Jul 8 17:37 appsettings.json -> ..data/appsettings.json
我做错了什么? 请帮忙
编辑: dotnet 构建结果由
生成- task: DotNetCoreCLI@2
inputs:
command: 'publish'
projects: |
**/*.csproj
!**/*Test.csproj
!**/*Tests.csproj
arguments: '--configuration $(buildConfiguration) --output $(Build.ArtifactStagingDirectory)/PublishOutput'
zipAfterPublish: false
modifyOutputPath: false
publishWebProjects: false
displayName: 'dotnet - Publish Project'
您的 Dockerfile 存在于
$(Build.Repository.LocalPath)
中(例如:/home/vsts/work/1/s
),但 dll 内置于 $(build.ArtifactStagingDirectory)/PublishOutput
中,这是不同的。然后您将 buildContext
指定到该目录,该目录通常应与 Dockerfile 位于同一目录。
因此,在完成
DotNetCoreCLI@2
发布任务后,您可以将dll复制到Dockerfile所在的工作目录中:
- script: |
pwd
ls -la .
cp $(build.ArtifactStagingDirectory)/PublishOutput/*.dll .
我评论了
repository
和 buildContext
Docker@2
任务:
- task: Docker@2
displayName: 'Docker - Build'
inputs:
command: build
#containerRegistry: $(ACR_LOGIN_SERVER)
#repository: $(ACR_LOGIN_SERVER)/$(dockerImageName)
Dockerfile: '$(Build.Repository.LocalPath)/Dockerfile' # Path to your Dockerfile
#buildContext: '$(Build.ArtifactStagingDirectory)/PublishOutput'
arguments: '--progress=plain --build-arg DLL_NAME=$(project_name).dll'
tags: |-
$(releaseVersion)
latest
可以在docker中获取dll文件: