自定義構建基於 asp.net core 的基礎 docker 鏡像

首先記錄一個問題,今天在用 Jenkins 構建項目的時候忽然出現包源的錯誤:git

/usr/share/dotnet/sdk/2.2.104/NuGet.targets(114,5): error : Unable to load the service index for source https://api.nuget.org/v3/index.json. [/....csproj]
/usr/share/dotnet/sdk/2.2.104/NuGet.targets(114,5): error :   The HTTP request to 'GET https://api.nuget.org/v3/index.json' has timed out after 100000ms. [/....csproj]
複製代碼

nuget的包源沒法訪問(沒法ping通),而我在一臺服務器上訪問https://api.nuget.org/v3/index.json時則會自動重定向到https://nuget.cdn.azure.cn/v3/index.jsongithub

可是打包機器執行dotnet restore卻仍是沒法還原成功,即便指定包源後即dotnet restore -s https://nuget.cdn.azure.cn/v3/index.json 能還原一部分包,部分包依舊沒法還原docker

最後測試發現,包源只是部分地區沒法訪問,能夠嘗試切換源/使用Nuget.Config文件試試,固然最快的仍是經過科學的方式訪問~。json

若本地 VS 的包管理器也沒法正常使用,切換源(nuget.cdn.azure.cn/v3/index.js…)便可 api

再說另一個問題

而後這篇文章實際上是另外的一個問題,以前我構建了一個基礎鏡像包,基於FROM microsoft/dotnet:2.2-aspnetcore-runtime構建,而我構建時使用的sdk鏡像是FROM microsoft/dotnet:2.2-sdkbash

9.23號(.net core 3.0發佈)以前還可以正常構建,今天在解決了上面包源問題後,鏡像構建成功併發布到服務器,卻發現鏡像沒法啓動起來。服務器

報錯信息以下併發

The specified framework 'Microsoft.NETCore.App', version '2.2.2' 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:
      http://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.2.0 at [/usr/share/dotnet/shared/Microsoft.NETCore.App]
複製代碼

既然說是版本問題,那就嘗試着將基礎進行修改成FROM microsoft/dotnet:2.2.2-aspnetcore-runtime,果真,從新構建後可以好好的運行起來了。app

那麼我是如何構建的基礎鏡像的,只需下面 3 個文件就搞定了。測試

阿里雲鏡像源

阿里雲的軟件包源,可用於一些基礎鏡像中沒有的軟件安裝,寫入到 sources.list 供後面使用

  • 文件:sources.list
deb http://mirrors.aliyun.com/debian jessie main contrib non-free
deb-src http://mirrors.aliyun.com/debian jessie main contrib non-free
deb http://mirrors.aliyun.com/debian jessie-updates main contrib non-free
deb-src http://mirrors.aliyun.com/debian jessie-updates main contrib non-free
deb http://mirrors.aliyun.com/debian-security jessie/updates main contrib non-free
deb-src http://mirrors.aliyun.com/debian-security jessie/updates main contrib non-free
複製代碼

一個包含圖像庫的 Dockerfile

基於dotnet:2.2.2 aspnetcore-runtime,並在其中安裝 libgdiplus,設置時區,具體的能夠根據本身的項目須要去構建

文件:Dockerfile

FROM microsoft/dotnet:2.2.2-aspnetcore-runtime
WORKDIR /app
COPY sources.list /app/sources.list
RUN rm -f /etc/apt/sources.list && mv sources.list /etc/apt/ && apt-get update -y && apt-get install -y libgdiplus && apt-get clean && ln -s /usr/lib/libgdiplus.so /usr/lib/gdiplus.dll

# 時區設置
ENV TZ=Asia/Shanghai
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone

# ENTRYPOINT ["./xxxxx.dll"]

複製代碼

使用腳本推送鏡像到阿里雲的容器鏡像倉庫

要推送鏡像到阿里雲,須要先去阿里雲開通並建立命名空間 須要先登陸雲端鏡像倉庫 ,登陸阿里雲以下

文件名:build.sh (Linux添加執行權限 chmod +x ./build.sh)

export ALIYUN_DOCKER_CLOUD_URL=registry.cn-hangzhou.aliyuncs.com
export DOCKER_IMAGE_NAME=yimocoding/dotnet2.2.2-base
export BUILD_NUMBER=latest

docker build -t $DOCKER_IMAGE_NAME -f ./Dockerfile .
docker tag $DOCKER_IMAGE_NAME $ALIYUN_DOCKER_CLOUD_URL/$DOCKER_IMAGE_NAME:$BUILD_NUMBER
echo 推送鏡像到雲端
docker push $ALIYUN_DOCKER_CLOUD_URL/$DOCKER_IMAGE_NAME:$BUILD_NUMBER

echo '刪除本地鏡像'
docker rmi $DOCKER_IMAGE_NAME
docker rmi $ALIYUN_DOCKER_CLOUD_URL/$DOCKER_IMAGE_NAME:$BUILD_NUMBER
複製代碼

三個文件的目錄結構

文件建立完成後,執行 build.sh 便可構建鏡像並推送到阿里雲的鏡像倉庫,若想提交到其餘雲倉庫,修改腳本中的變量便可

示例文件:github.com/yimogit/MeD…

真是,人在家中坐,鍋從天落,而爲了更好的接鍋,記錄一二,省得到時候望碼興嘆。

相關文章
相關標籤/搜索