.net6 docker部署,以及问题解决(附Dokerfile)

Posted cnblogsName

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了.net6 docker部署,以及问题解决(附Dokerfile)相关的知识,希望对你有一定的参考价值。

搭建仓库,发布配置

docker搭建私有仓库

参考上文,搭建好私有仓库,成功访问http://127.0.0.1:5000/v2/_catalog之后:

  1. 在VS右键 => 添加 => Docker支持 => 选择Linux,即可自动添加Dokerfile
  2. 在VS右键发布 => Docker容器注册表 => 其他Docker容器注册表 => 注册表URL:http://localhost:5000
  3. 发布配置:

部署模式选择独立或者框架依赖都可以,如果选择框架依赖,那么启动项目时,需要运行.net6的容器。

启动容器的问题以及相应解决方法

1. System.Drawing.Common is not supported on non-Windows platforms.

仅在 Windows 上支持 System.Drawing.Common
简单说就是有两种解决方法:

  1. 将引用System.Drawing.Common的代码改为其他类库
  2. 新增一个runtimeconfig.template.json 模板文件:

2. System.TypeInitializationException: The type initializer for \'Gdip\' threw an exception.

System.TypeInitializationException: The type initializer for \'Gdip\' threw an exception.
System.DllNotFoundException: Unable to load shared library \'libgdiplus\' or one of its dependencies. In order to help diagnose loading problems, consider setting the LD_DEBUG environment variable: liblibgdiplus: cannot open shared object file: No such file or directory

需要在容器中安装libgdiplus,可在dockerfile中添加如下代码:

WORKDIR /etc/apt
# 备份
RUN mkdir sources.list.backup
RUN cp sources.list ./sources.list.backup
# 以覆盖+追加的方式替换掉sources.list文件(解决下载慢问题)
RUN echo \'deb https://mirrors.aliyun.com/debian bullseye main\'>sources.list
RUN echo \'deb https://mirrors.aliyun.com/debian-security bullseye-security main\'>>sources.list
RUN echo \'deb https://mirrors.aliyun.com/debian bullseye-updates main\'>>sources.list
#RUN cat sources.list
RUN apt-get update -y
RUN apt-get install -y libgdiplus

3. Cannot find any fonts in specified font sources.

  1. 到windows系统的C://windows/Fonts目录下寻找字体,将找到的字体和Dockerfile文件放在同一级目录下
  2. 在dockerfile中添加如下代码:
COPY ["src/myProject/simkai.ttf", "/usr/share/fonts/simkai.ttf"]  
COPY ["src/myProject/SimSun.ttf", "/usr/share/fonts/SimSun.ttf"]

4. RedisConnectionException: It was not possible to connect to the redis server(s).

修改redis.windows-service.conf,增加ip地址配置(192.168.5.22为主机的ip):

bind 127.0.0.1 192.168.5.22

完整Dockerfile

#See https://aka.ms/containerfastmode to understand how Visual Studio uses this Dockerfile to build your images for faster debugging.

FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base
WORKDIR /app
EXPOSE 21234

FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
WORKDIR /src

COPY ["src/MyProject.Web.Host/MyProject.Web.Host.csproj", "src/MyProject.Web.Host/"]
COPY ["src/MyProject.Web.Core/MyProject.Web.Core.csproj", "src/MyProject.Web.Core/"]
COPY ["src/MyProject.Application/MyProject.Application.csproj", "src/MyProject.Application/"]
COPY ["src/MyProject.Core/MyProject.Core.csproj", "src/MyProject.Core/"]
COPY ["src/MyProject.EntityFrameworkCore/MyProject.EntityFrameworkCore.csproj", "src/MyProject.EntityFrameworkCore/"]
RUN dotnet restore "src/MyProject.Web.Host/MyProject.Web.Host.csproj"
COPY . .
WORKDIR "/src/src/MyProject.Web.Host"
RUN dotnet build "MyProject.Web.Host.csproj" -c Release -o /app/build

FROM build AS publish
RUN dotnet publish "MyProject.Web.Host.csproj" -c Release -o /app/publish /p:UseAppHost=false

FROM base AS final
#RUN cd /etc/apt
WORKDIR /etc/apt
# 备份
RUN mkdir sources.list.backup
RUN cp sources.list ./sources.list.backup
# 以覆盖+追加的方式替换掉sources.list文件
RUN echo \'deb https://mirrors.aliyun.com/debian bullseye main\'>sources.list
RUN echo \'deb https://mirrors.aliyun.com/debian-security bullseye-security main\'>>sources.list
RUN echo \'deb https://mirrors.aliyun.com/debian bullseye-updates main\'>>sources.list
#RUN cat sources.list
RUN apt-get update -y
RUN apt-get install -y libgdiplus

WORKDIR /app
COPY --from=publish /app/publish .

COPY ["src/MyProject.Web.Host/simkai.ttf", "/usr/share/fonts/simkai.ttf"]
COPY ["src/MyProject.Web.Host/SimSun.ttf", "/usr/share/fonts/SimSun.ttf"]
COPY ["src/MyProject.Web.Host/times.ttf", "/usr/share/fonts/times.ttf"]
COPY ["src/MyProject.Web.Host/timesbd.ttf", "/usr/share/fonts/timesbd.ttf"]
COPY ["src/MyProject.Web.Host/timesbi.ttf", "/usr/share/fonts/timesbi.ttf"]
COPY ["src/MyProject.Web.Host/timesi.ttf", "/usr/share/fonts/timesi.ttf"]

ENTRYPOINT ["dotnet", "MyProject.Web.Host.dll"]

以上是关于.net6 docker部署,以及问题解决(附Dokerfile)的主要内容,如果未能解决你的问题,请参考以下文章

一docker-compose部署elasticsearch+hanlp(7.16.2版本)----附完整镜像

如何在centos7中部署Net6.0程序?

net6 swagger与IdentityServer集成登录验证,完美解决调试部署等问题

net6 swagger与IdentityServer集成登录验证,完美解决调试部署等问题

net6 swagger与IdentityServer集成登录验证,完美解决调试部署等问题

Docker+jenkins+gitee+springboot实现自动化部署流程(详细教程)(附下载工具地址)