【开发技术】Docker中在.Net Framework调用MATLAB

目录

一、背景

二、解决方案

2.1 window运行环境下,使用matlab分析

2.2 编写Dockerfile文件,用于创建服务镜像

2.2.1 docker中构建.net framework4.8环境运行matlab

2.2.2 构建.netCore环境

2.3 Docker服务运行

2.3.1 创建镜像

2.3.2 运行容器

2.3.3 挂载volume

三、文章总结


一、背景

        大概几年前公司和英国白金汉大学有合作,他们那边提供了一个视觉分析的算法,使用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分析

  1.  安装matlab运行时环境
  2.  编译matlab代码为dll
  3.  将生成的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算法也进行调整。作为一个点,后面有时间可以研究下。

把之所学,以文载之~ 欢迎大家多多交流

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

勿芮介

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值