【问题标题】:Gitlab CI Docker and Aspnetcore runtime version issueGitlab CI Docker 和 Aspnetcore 运行时版本问题
【发布时间】:2020-05-23 11:48:52
【问题描述】:

我在 GitLab 下有一个 aspnet core mvc 项目,它使用 DockerFile 来容器化 docker 镜像。现在我想通过构建和测试 2 个阶段来利用 GitLab CI 功能。

我的计划是构建用于 docker build 命令;测试是 docker pull 和 run 命令。我已经完成了大部分 .gitlab.ci.yml 代码,但在测试阶段遇到了问题。

看起来 gitlab shared runner docker image dotnet cli 版本低于我的预期版本。这是输出:

 It was not possible to find any compatible framework version
 The specified framework 'Microsoft.AspNetCore.App', version '2.2.0' was not found.
   - Check application dependencies and target a framework version installed at:
       /usr/share/dotnet/
   - Installing .NET Core prerequisites might help resolve this problem:
       https://go.microsoft.com/fwlink/?LinkID=798306&clcid=0x409
   - The .NET Core framework and SDK can be installed from:
       https://aka.ms/dotnet-download
   - The following versions are installed:
       2.1.15 at [/usr/share/dotnet/shared/Microsoft.AspNetCore.App]

这是我的Dockerfile

FROM microsoft/dotnet:2.2-sdk AS build-env
WORKDIR /app

# Copy csproj and restore as distinct layers
COPY *.csproj ./
RUN dotnet restore

# Copy everything else and build
COPY . ./
RUN dotnet publish -c Release -o out

# Build runtime image
FROM microsoft/dotnet:aspnetcore-runtime
WORKDIR /app
COPY --from=build-env /app/out .

# override the request to 5000
ENV ASPNETCORE_URLS=http://+:5002 
# override to 5002
EXPOSE 5002 
ENTRYPOINT ["dotnet", "ASPNetApp_2.dll"]

这是我的.gitlab.ci.yml

image: docker:latest

stages:
  - build
  - test

services:
  - docker:dind

variables:
  IMAGE_NAME: ${CI_REGISTRY}/${CI_PROJECT_PATH}

build:
  only:
    - master
    - develop
  before_script:
    - apk update && apk add bash
    - chmod +x ./function.sh
    - . function.sh
    - get_image_tag
    - echo $TAG
    - docker login registry.gitlab.com -u ${CI_REGISTRY_USER} -p ${CI_REGISTRY_PASSWORD}
  script:
    - docker build -t $IMAGE_NAME:$TAG .
    - docker push $IMAGE_NAME:$TAG
  after_script:
    - docker logout ${CI_REGISTRY}
  stage: build
  tags:
    - docker

test:
  stage: test
  before_script:
    - apk update && apk add bash
    - apt-get install aspnetcore-runtime-2.2
    - chmod +x ./function.sh
    - . function.sh
    - get_image_tag
    - echo $TAG
    - docker login registry.gitlab.com -u ${CI_REGISTRY_USER} -p ${CI_REGISTRY_PASSWORD}
  script:
    - docker pull microsoft/dotnet:2.2-aspnetcore-runtime
    - docker pull $IMAGE_NAME:$TAG
    - docker run -p 5002 $IMAGE_NAME:$TAG -it
  after_script:
    - docker logout ${CI_REGISTRY}
function.sh
#!/bin/bash
function get_image_tag(){
  if [ "$CI_COMMIT_REF_SLUG" == "master" ]; then
    export TAG="latest";
  else
    export TAG=$CI_COMMIT_REF_SLUG;
  fi
}

我一直在尝试sudo apt-get、apt-get、apk add,但没有希望,有什么建议吗?

【问题讨论】:

  • 请添加构建部分和您的 dockerfile。
  • @RiWe 我添加了 Dockerfile 和 ci 构建部分
  • 图像在您的本地计算机上是否按预期工作?我唯一想到的是 microsoft/dotnet:aspnetcore-runtime 使用的 dotnet 版本与 microsoft/dotnet:2.2-sdk 不同。
  • @RiWe 我想我找到了答案,你可以看看下面我的答案

标签: bash docker gitlab gitlab-ci alpine


【解决方案1】:

所以我设法使它与以下配置一起工作。

  1. 将图像和服务放入舞台部分。
build:
 image: docker:latest
 services:
   - docker:dind
...
test:
 image: docker:latest
 services:
   - docker:dind
   - microsoft/dotnet

通过这样做,gitlab ci runner 可以获取最新的 Microsoft dotnet 环境,并且可以让 docker 运行

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-12-12
    • 2020-06-28
    • 2020-07-29
    • 2021-10-17
    • 2021-02-27
    • 2018-01-01
    • 1970-01-01
    • 2017-05-06
    相关资源
    最近更新 更多