目录
2.2.1 docker中构建.net framework4.8环境运行matlab
一、背景
大概几年前公司和英国白金汉大学有合作,他们那边提供了一个视觉分析的算法,使用Matlab开发。当时公司已有一个项目是使用Framework4.8开发的Api服务,需要集成matlab分析算法,并且为了快速部署希望能够尝试接入到Docker中,当时查询了dockerhub中还没有使用.net framework集成matlab的环境,因此研究了一下。
文件结构如下:
1、algpro是.net项目文件
2、MATLAB_Runtime_R2019a_Update_9_win64是环境安装包
3、Dockerfile文件,用于构建服务镜像
二、解决方案
2.1 window运行环境下,使用matlab分析
- 安装matlab运行时环境
- 编译matlab代码为dll
- 将生成的DLL文件添加到.NET项目中,并使用Platform Invoke (P/Invoke)
因此在docker中,我们需要创建相关环境
2.2 编写Dockerfile文件,用于创建服务镜像
2.2.1 docker中构建.net framework4.8环境运行matlab
#Depending on the operating system of the host machines(s) that will build or run the containers, the image specified in the FROM statement may need to be changed.
#For more information, please see https://aka.ms/containercompat
FROM mcr.microsoft.com/dotnet/framework/aspnet:4.8-windowsservercore-ltsc2019
# Previously downloaded and unzipped installer:
COPY ["/MATLAB_Runtime_R2019a_Update_9_win64", "/MATLAB_Runtime_win64"]
# Several programs I may wish to execute:
SHELL ["powershell", "-Command", "$ErrorActionPreference = 'Stop'; $ProgressPreference = 'SilentlyContinue';"]
# Takes about 10 minutes to install:
RUN Start-Process C:\MATLAB_Runtime_win64\bin\setup.exe -ArgumentList '-mode silent', '-agreeToLicense yes' -Wait
# Line 6: Installation folder after setup is complete
RUN Remove-Item -Force -Recurse C:\\MATLAB_Runtime_win64
ARG source
WORKDIR /inetpub/wwwroot
COPY ${source:-obj/Docker/publish} .
ENTRYPOINT AlgDocker.exe
2.2.2 构建.netCore环境
#Depending on the operating system of the host machines(s) that will build or run the containers, the image specified in the FROM statement may need to be changed.
#For more information, please see https://aka.ms/containercompat
FROM microsoft/windowsservercore
EXPOSE 80
# Previously downloaded and unzipped installer:
COPY ["/MATLAB_Runtime_R2019a_Update_9_win64", "/MATLAB_Runtime_win64"]
# Several programs I may wish to execute:
SHELL ["powershell", "-Command", "$ErrorActionPreference = 'Stop'; $ProgressPreference = 'SilentlyContinue';"]
# Takes about 10 minutes to install:
RUN Start-Process C:\MATLAB_Runtime_win64\bin\setup.exe -ArgumentList '-mode silent', '-agreeToLicense yes' -Wait
# Line 6: Installation folder after setup is complete
RUN Remove-Item -Force -Recurse C:\\MATLAB_Runtime_win64
ARG source
WORKDIR /inetpub/wwwroot
COPY ${source:-obj/Docker/publish} .
ENTRYPOINT AlgCoreDocker.exe
2.3 Docker服务运行
2.3.1 创建镜像
docker build . -t algpro(容器名称)
2.3.2 运行容器
docker run -it -d -p 8040:80 algpro
2.3.3 挂载volume
//创建一个自定义的容器卷
docker volume create my-volume-2
//挂载到容器内部地址下(方便更新内部服务)
docker run -it -d -v my-volume:C:/inetpub/wwwroot -p 8040:80 algpro
ps:该卷会被存储在宿主机的docker/volumes
目录下
三、文章总结
.net Framework环境需要配置在Docker的window containers。后续有尝试使用.net Core来调用Matlab,但是当前提供的matlab算法文件是基于window环境下使用的,所以后没有深入研究,但是结合网上的例子是可实现的,不过前提是需要matlab算法也进行调整。作为一个点,后面有时间可以研究下。
把之所学,以文载之~ 欢迎大家多多交流