docker - 单机多容器
2022-06-03
#容器名 gpu cpu核数区间 内存 容器内代码目录 起始端口 镜像地址
test 0,1 0-33 64 /data-hdd2 /code-online 2000 harbor.xx.com/library/xx
basepath=$(cd `dirname $0`; pwd)
list_file=${basepath}/"dev_manager.txt"
cat ${list_file} | grep -v '#' | while read line
do
line=($line)
container_name=${line[0]}
gpus=${line[1]}
cpus=${line[2]}
memory=${line[3]}
data_path=${line[4]}
workspace=${line[5]}
listen_one_port=${line[6]}
listen_two_port=$(($listen_one_port+1))
listen_three_port=$(($listen_one_port+2))
listen_four_port=$(($listen_one_port+3))
listen_five_port=$(($listen_one_port+4))
listen_six_port=$(($listen_one_port+5))
listen_seven_port=$(($listen_one_port+6))
listen_eight_port=$(($listen_one_port+7))
image_name=${line[7]}
run_image_name=$(docker ps | grep ${container_name} | awk '{print $2}')
run_container_name=$(docker ps | grep ${container_name} | awk '{print $NF}')
runx_container_name=$(docker ps -a | grep ${container_name} | awk '{print $NF}')
if [[ ${image_name} == ${run_image_name} ]] && [[ ${container_name} == ${run_container_name} ]]; then
echo ${container_name}"运行中,当前镜像:${run_image_name}"
elif [[ -z ${run_container_name} ]] || [[ ${image_name} != ${run_image_name} ]] ;then
echo 尝试更新镜像:${image_name}
docker pull ${image_name}
if [[ $? -ne 0 ]];then
echo "docker pull ${image_name} error!"
exit 1
fi
if [[ ${run_container_name} ]];then
docker kill ${container_name}
fi
if [[ ${runx_container_name} ]];then
docker rm ${container_name}
fi
docker run --restart=always -d \
--gpus '"device='${gpus}'"' --cpuset-cpus ${cpus} -m ${memory}G \
-p ${listen_one_port}:8080 -p ${listen_two_port}:6006 -p ${listen_three_port}:22 -p ${listen_four_port}:22233 -p ${listen_five_port}:7474 -p ${listen_six_port}:7475 -p ${listen_seven_port}:7476 -p ${listen_eight_port}:7477 \
-v ${data_path}/vscode/${container_name}:${workspace} --hostname ${container_name} --name ${container_name} \
--env-file=/opt/bin/docker-env-config \
-v /var/lib/lxcfs/proc/cpuinfo:/proc/cpuinfo:rw \
-v /var/lib/lxcfs/proc/diskstats:/proc/diskstats:rw \
-v /var/lib/lxcfs/proc/meminfo:/proc/meminfo:rw \
-v /var/lib/lxcfs/proc/stat:/proc/stat:rw \
-v /var/lib/lxcfs/proc/swaps:/proc/swaps:rw \
-v /var/lib/lxcfs/proc/uptime:/proc/uptime:rw \
${image_name}
fi
done
#!/bin/bash
# 限制挂载目录磁盘
# 服务器重启后会消息,需实现开机自启,因此不能在/etc/fstab下编写挂载
#!/bin/bash
basepath=$(cd `dirname $0`; pwd)
list_file=${basepath}/"dev_manager.txt"
cat -n ${list_file} | grep -v '#' | while read line
do
line=($line)
num=${line[0]}
name=${line[1]}
data_path="/data-hdd2/vscode"
img_path="/data-hdd2/img"
mkdir -p ${data_path} ${img_path}
if [[ ! -f ${img_path}/${name}.img ]];then
dd if=/dev/zero of=${img_path}/${name}.img bs=100M count=1024
fi
losetup /dev/loop${num} ${img_path}/${name}.img
if [[ ${name} == $1 ]];then
mkfs.xfs /dev/loop${num}
fi
res=$(mount | grep ${img_path}/${name}.img)
if [[ -z ${res} ]];then
mount -t xfs /dev/loop${num} ${data_path}/${name}
fi
done