构建 CentOS7 + cuda 11 + cudnn8 + openfoam2.3.x 的 Docker 镜像

由于集群环境复杂,不能联网,没有 root 权限,安装、管理和移植环境都很复杂,这里尝试在Docker中构建 CentOS7 + cuda 11 + cudnn8 + openfoam2.3.x 环境,可以大大简化集群的安装过程,下面记录配置过程。

关于Docker镜像的安装和基本操作,参考前文ubuntu docker配置cuda+anaconda+vscode+tensorflow环境的镜像

在CentOS 服务器上安装Docker

# 配置docker源,直接在阿里云镜像站下载:
wget -P /etc/yum.repos.d/ http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
# 安装
yum install docker-ce -y
# 安装 nvidia-docker
yum install -y nvidia-docker2
systemctl restart docker

下载Nvidia官方Docker镜像

git  clone  https://gitlab.com/nvidia/container-images/cuda.git

容器映像脚本存档在dist/目录中,可用于所有支持的发行版和cuda版本。
下面是一个关于如何为Ubuntu 18.04和CUDA 11.6.0构建多拱形容器映像的示例:

OPTIONS
    -h, --help            - Show this message.
    -n, --dry-run         - Show commands but don't do anything.
    -d, --debug           - Show debug output.
    --load                - Load the images on the build host. (Out of the docker cache).
    --push                - Push the images to the remote repository.
    --image-name=str      - The image name to use. Default: nvcr.io/nvidia/cuda
    --cuda-version=str    - The cuda version to use.
    --os=str              - The target operating system.
    --arch=csv            - Target architectures as a comma separated string.
    --kitpick             - Build from the kitpick directory.
    --cudagl              - Build a cudagl image set. x86_64 only.

./build.sh -d --image-name my-remote-container-registry/cuda --cuda-version 11.6.0 --os ubuntu --os-version 18.04 --arch x86_64,arm64 --push

这里使用centos7, cuda-10.1,cudnn8 的镜像
https://gitlab.com/nvidia/container-images/cuda/-/blob/master/dist/11.1.1/centos7/devel/cudnn8/Dockerfile

docker pull nvidia/cuda:10.1-devel-centos7

启动镜像

建立了一个nvidia/cuda:10.1-devel-centos7 镜像的容器, -p本地端口:容器端口 映射,容器name = zhl, 容器路径/data 挂载在 本地路径 /data/zhl_docker 上 , 之后在容器内进行操作。

docker run    --gpus all  -it  -u 0  -d --privileged=true --name=zhl_centos   -p  8122:22  -p  8180:8080   -p  8160:8060 -p 8181:8081  -v /root/zhl/data/:/data  nvidia/cuda:10.1-devel-centos7 bash

安装openMPI

安装依赖

yum groupinstall -y ‘Development Tools’
yum install -y perl

编译openmpi

wget https://download.open-mpi.org/release/open-mpi/v4.1/openmpi-4.1.2.tar.gz
tar zxvf openmpi-4.1.2.tar.gz
cd openmpi-4.1.2
./configure 
make -j 12
make all install

环境变量

# 打开.bashrc文件
vim ~/.bashrc
# 添加路径                                                        
# OPENMPI 4.1.2
export PATH=/usr/local/bin:$PATH
export LD_LIBRARY_PATH=/usr/local/lib:$LD_LIBRARY_PATH
# :wq 保存退出
# 更新环境
source ~/.bashrc

开启ssh服务 和 端口映射

在云服务器 CentOS 7 下重启服务不再通过 service 操作,而是通过 systemctl 操作。 操作说明如下

# 安装OpenSSH服务(CentOS系统默认安装了openssh)
yum install openssh-server -y
# 配置OpenSSH服务(默认的配置已可以正常工作)

vi /etc/ssh/sshd_config
	Port=22  #设置SSH的端口号是22(默认端口号为22)
	PermitRootLogin  yes  #允许root用户登陆

#启动SSH服务:
systemctl start sshd.service
#重启 sshd 服务:
systemctl restart sshd.service
#设置服务开启自启:
systemctl enable sshd.service

结合启动容器时使用的端口映射,可以通过ssh + 端口映射 连接容器,参考前文 https://blog.csdn.net/qq_43488795/article/details/126658342

ssh root@服务器ip地址 -p 8122

centos bug

这里有一个小bug,使用systemctl 命令会报错Failed to get D-Bus connection: Operation not permitted

扫描二维码关注公众号,回复: 14659401 查看本文章

解决方法

  1. 创建容器时指定命令为:/usr/sbin/init
  2. 创建容器时需要加上参数:–privileged=true -u root -d

需要打包镜像重新进入。

docker run --rm --runtime=nvidia -itd  -e NVIDIA_VISIBLE_DEVICES=all  --privileged=true  -u root  --name=zhl_centos  -v  /root/zhl/data/:/data demms:v1 /usr/sbin/init
docker exec -it zhl_centos  /bin/bash

源码编译 OpenFOAM-2.3.x

参考前文 CentOS7 源码编译 OpenFOAM-2.3.x

编译cfd和dem程序

保密

打包发布镜像

将容器变为镜像 container → image

docker commit container_name image_name

将镜像打包成tar包 image→tar

docker save -o xxx.tar test # 当前路径下会生成一个xxx.tar
docker save -o test.tar test # 举例

tar包生成镜像 tar→image

docker load < test.tar # 生成的镜像(image)跟之前打包的镜像

镜像生成容器 image→container

docker run    --gpus all  -it  -u 0  -d --privileged=true --name=zhl_centos   -p  8122:22  -p  8180:8080   -p  8160:8060 -p 8181:8081  -v /root/zhl/data/:/data  nvidia/cuda:10.1-devel-centos7 bash

猜你喜欢

转载自blog.csdn.net/qq_43488795/article/details/127866052
今日推荐