Netcore调用R语言之 - Docker镜像集成

最近NetCore项目中需要调用R语言脚本,Docker-Hub上没有同时集成这两种环境的镜像可用。只得自己动手搞了一个,有需要的小伙伴可以用我公开的镜像renzicu/r-dotnet:4.2-3.1做为基镜像使用。

一、构建镜像

编写Dockerfile文件
FROM r-base:4.2.2

# 下载依赖包
RUN R -e "install.packages('nadiv')"
RUN R -e "install.packages('Matrix')"

# 安装wget、aspnetcore
RUN apt-get update \
    && apt-get install -y wget \
    && wget https://packages.microsoft.com/config/debian/11/packages-microsoft-prod.deb -O packages-microsoft-prod.deb \
    && dpkg -i packages-microsoft-prod.deb \
    && rm packages-microsoft-prod.deb \
    && apt-get update \
    && apt-get install -y aspnetcore-runtime-3.1

# 清理apt-get
RUN apt-get autoclean \
    && rm -rf /var/lib/apt/lists/*

# 默认监听80端口
ENV ASPNETCORE_URLS=http://+:80

# r-base:4.1.2+ 依赖docker版本20.10+
# yum -y install docker-ce-20.10.21-3.el7

二、打包推送

# 打包镜像
docker build -t rbase4.2.2-aspnetcore3.1:1.0 .
# 打标签准备推送到阿里云,需要提前创建好命名空间【示例:renzicu】
docker tag rbase4.2.2-aspnetcore3.1:1.0 registry.cn-beijing.aliyuncs.com/renzicu/rbase4.2.2-aspnetcore3.1:1.0

# 登录
docker login --username=*********** registry.cn-beijing.aliyuncs.com
docker push registry.cn-beijing.aliyuncs.com/renzicu/rbase4.2.2-aspnetcore3.1:1.0

三、注意事项

r-base4.1.2+ 版本运行报ERROR: R_HOME ('/usr/lib/R') not found,升级你的Docker版本到20.10+即可解决

附:手动构建方法

运行一个 debian 系统,安装 dotnet、r-base 及相关依赖组件
# 运行一个debian系统
docker pull debian
docker run -itd --name rbase debian
docker exec -it rbase bash

# 更换国内源(中科大镜像站)
mv /etc/apt/sources.list /etc/apt/sources.list.bak

cat > /etc/apt/sources.list<< EOF
deb http://mirrors.ustc.edu.cn/debian/ bullseye main contrib non-free
deb-src http://mirrors.ustc.edu.cn/debian/ bullseye main contrib non-free
deb http://mirrors.ustc.edu.cn/debian/ bullseye-updates main contrib non-free
deb-src http://mirrors.ustc.edu.cn/debian/ bullseye-updates main contrib non-free
deb http://mirrors.ustc.edu.cn/debian/ bullseye-backports main contrib non-free
deb-src http://mirrors.ustc.edu.cn/debian/ bullseye-backports main contrib non-free
deb http://mirrors.ustc.edu.cn/debian-security/ bullseye-security main contrib non-free
deb-src http://mirrors.ustc.edu.cn/debian-security/ bullseye-security main contrib non-free
EOF

# 更新源,安装必要软件
apt-get update && apt-get install wget vim

# 安装 dotnet和依赖
wget https://packages.microsoft.com/config/debian/11/packages-microsoft-prod.deb -O packages-microsoft-prod.deb
dpkg -i packages-microsoft-prod.deb && rm packages-microsoft-prod.deb

apt-get update && \
apt-get install -y dotnet-sdk-3.1

# dotnet debian环境下依赖组件
apt-get install libc6 libgcc-s1 libgssapi-krb5-2 libicu67 libssl1.1 libstdc++6 zlib1g libgdiplus

# Unable to load shared library 'libdl' or one of its dependencies. In order to help diagnose loading problems, consider setting the LD_DEBUG environment variable: liblibdl: cannot open shared object file: No such file or directory
ln -s /lib/x86_64-linux-gnu/libdl.so.2  /lib/x86_64-linux-gnu/libdl.so

# 添加R安装源
apt-get install dirmngr gnupg apt-transport-https ca-certificates software-properties-common
apt-key adv --keyserver keyserver.ubuntu.com --recv-key '95C0FAF38DB3CCAD0C080A7BDC78B2DDEABC47B7'
add-apt-repository 'deb http://cloud.r-project.org/bin/linux/debian bullseye-cran40/'

# 安装 r-base和组件
apt-get update && apt-get install r-base r-base-core r-base-dev

# R -e "install.packages('Matrix')"
R -e "install.packages('nadiv')"
mv /usr/local/lib/R/site-library/nadiv /usr/lib/R/library

# 配置系统环境变量
vi ~/.bashrc
export R_HOME=/usr/lib/R
export PATH=$PATH:$R_HOME/bin
source ~/.bashrc

# 清理apt-get
apt-get autoclean && rm -rf /var/lib/apt/lists/*
保存容器为镜像,并推送到hub
# 在宿主机执行
docker commit rbase renzicu/r-dotnet:4.2-3.1
docker push renzicu/r-dotnet:4.2-3.1
分享到:
本文链接:https://blog.renzicu.com/2022/rbase-netcore/index.html
版权声明:本博客所有文章除特别声明外,均采用 CC BY 4.0 许可协议。转载请注明出处!
THE END
二维码
打赏
文章目录
关闭
目 录