aspnetcore3.1通過dockerfile發布到docker遇到的坑


內在影響

  環境:window 10,docker2.3.0.2,vs 2019 

  外部資源:mssql(2016),reides

  發布內容:web api (http:若創建時點擊了支持https也沒有關系,不影響發布http)

正常步驟:

1.右鍵點擊項目->添加->支持docker

2.修改Dockerfile

3.發布docker成功

非正常步驟:

1.鏡像無法下載,修改dockerfile的鏡像(使用阿里鏡像)

mcr.microsoft.com/dotnet/core/aspnet:3.1-buster-slim  -> registry.cn-chengdu.aliyuncs.com/jimlicatpub/aspnet:3.1-buster-slim

mcr.microsoft.com/dotnet/core/sdk:3.1-buster -> registry.cn-chengdu.aliyuncs.com/jimlicatpub/dotnet-sdk:3.1-buste

 

2.無法連接數據庫

  錯誤信息:SSL Handshake failed with OpenSSL error

  解決方案:修改dockerfile文件 -> 添加 RUN sed -i 's/TLSv1.2/TLSv1.0/g' /etc/ssl/openssl.cnf (ps:也有說添加 RUN sed -i "s|DEFAULT@SECLEVEL=2|DEFAULT@SECLEVEL=1|g" /etc/ssl/openssl.cnf ,未測試)

   參考方案:

    1.https://blog.csdn.net/qq_21265915/article/details/103274624

    2.https://www.cnblogs.com/jidanfan/p/12158972.html

  ps:由於找不到原答案,所以找了兩個相近的鏈接補上

3.數據庫連接超時
  錯誤信息:Connection Timeout Expired

       解決方案:查看數據庫版本->sql server 2008 sp3(ps:只測試了sql server 2016、2014)(ps:也有說數據庫連接超時是因為 mcr.microsoft.com/dotnet/core/aspnet:3.1-buster-slim   的原因,但是在未更改數據庫 為 sql server 2016時,也是無效的;后續改為sql server 2016后可以,如果高版本數據庫不行,則可以考慮更改 為  mcr.microsoft.com/dotnet/core/aspnet:3.1 試試)

  參考方案:

    1.未找到原文地址,所以給一個相近的地址:https://my.oschina.net/lichaoqiang/blog/1793006

    2.測試無效,但是感覺有道理的方案:https://q.cnblogs.com/q/DetailPage/127069/

 

4.aspnetcore 3.1 在Linux下,使用圖片等操作導致系統錯誤

  錯誤信息:The type initializer for 'Gdip' threw an exception

  解決方案:

RUN echo "deb http://mirrors.aliyun.com/debian stretch main contrib non-free \
deb-src http://mirrors.aliyun.com/debian stretch main contrib non-free \
deb http://mirrors.aliyun.com/debian stretch-updates main contrib non-free \
deb-src http://mirrors.aliyun.com/debian stretch-updates main contrib non-free \
deb http://mirrors.aliyun.com/debian-security stretch/updates main contrib non-free \
deb-src http://mirrors.aliyun.com/debian-security stretch/updates main contrib non-free \
deb http://mirrors.aliyun.com/debian/ stretch-backports main non-free contrib \
deb-src http://mirrors.aliyun.com/debian/ stretch-backports main non-free contrib" > /etc/apt/sources.list
 
RUN apt-get update && apt-get install libgdiplus -y && ln -s libgdiplus.so gdiplus.dll

 

ps:

    1.鏡像可能會遇到生成出錯,注意命令添加位置,我是放到dockerfile末尾;

    2.打包成功但是無法運行,還是報同樣的錯誤,將命令放到文件最后,我開始放到中間出現此問題,放到文件末尾則無此問題;

    3.鏡像可能在window的打包環境可以運行,但是服務器會報"來自守護程序的錯誤響應:未指定命令",我的解決方案是刪除本地docker庫中打包程序的歷史鏡像,重新打包就好了

 4.個人認為有效的是“RUN apt-get update && apt-get install libgdiplus -y ”,奈何只加這一句打包會出問題,未找到原因,添加鏡像是為了快速拉取

 5.未找到原博客的引用地址,后續若有找到則添加

 

5.Aspnetcore 3.1 在生成docker鏡像時,項目引用多個nuget來源(私有nuget倉庫)時,編譯鏡像不通過

  錯誤信息:No packages exist with this id in source(s): nuget.org 

  解決方案:

    1、通過 -s|--source <SOURCE> 管理,通過多次指定此選項來提供多個源。

     示例:RUN dotnet restore "xxx"  -s xxx1 -s xxx2

    2、通過nuget.config文件管理,示例如下:

            a.在項目下創建nuget.config文件,如下

                <?xml version="1.0" encoding="utf-8"?>
                <configuration>
                  <packageSources>
                    <add key="NuGet" value="https://api.nuget.org/v3/index.json" />
                    <add key="MyNuGet" value="xxxx" />
                  </packageSources>
                </configuration>

            b. 將文件copy到工作站 COPY ["xxx/nuget.config", "xxxx"]

            c.通過–configfile加載配置文件 :RUN dotnet restore "xxx.csproj"  --configfile "xxx/nuget.config"

       參考方案:

            1、dockerfile:https://docs.microsoft.com/en-us/dotnet/core/tools/dotnet-restore

            2、nuget.config:https://docs.microsoft.com/en-us/nuget/reference/nuget-config-file

ps:注意nuget包的來源

6.在docker項目中,遇到本地項目中文件存在,但是在docker運行時找不到文件

  錯誤信息:The layout view 'xxx' could not be located. The following locations were searched:/xxx

  解決方案:

            1、先在解決方案中排除此文件,再包含的方式解決;

            2、刪除工程文件內容 ItemGroup節點下內容

            <ItemGroup>
                  <Content Remove="xxx" />
            </ItemGroup>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM