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

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

/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.json

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

最後測試發現,包源只是部分地區無法訪問,可以嘗試切換源/使用Nuget.Config文件試試,當然最快的還是通過科學的方式訪問~。

clipboard.png

若本地 VS 的包管理器也無法正常使用,切換源 (https://nuget.cdn.azure.cn/v3... 即可

clipboard.png

再說另外一個問題

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

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,果然,重新構建後能夠好好的運行起來了。

那麼我是如何構建的基礎鏡像的,只需下面 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"]

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

要推送鏡像到阿里雲,需要先去阿里雲開通並創建命名空間
需要先登錄雲端鏡像倉庫 ,登錄阿里雲如下

clipboard.png

文件名: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

三個文件的目錄結構

clipboard.png

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

示例文件:https://github.com/yimogit/Me...

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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章