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>

 

posted @ 2020-07-25 09:43  小小七爷  阅读(3513)  评论(0编辑  收藏  举报