我是Docker的初学者,并试图优化下面的bash代码。下面的代码必须不断地创建一个停靠器容器并删除它。我想要的是一次性创建对接器容器;然后根据if-else条件,在while循环中迭代执行相应的命令,最后删除对接容器。我尝试过使用对接入口点命令,但它们似乎都坚持使用docker run的单行命令。我们将非常感谢您的帮助!谢谢!
#!/bin/bash
FILE=$1
flag=1
while read line; do
#echo $line
#echo $flag
if [ $flag -gt 0 ] && [ $flag -lt 23189 ]; then
docker run --rm -v $(pwd):/eric -w /eric --entrypoint /bin/sh fedora -c "dnf info $line | tee -a manifest-fed:Core.txt; dnf repoquery --srpm $line"
((++flag))
elif [ $flag -gt 23188 ] && [ $flag -lt 46379 ]; then
docker run --rm -v $(pwd):/eric -w /eric --entrypoint /bin/sh fedora -c "dnf info $line | tee -a manifest-fed:Old.txt; dnf info $line"
((++flag))
elif [ $flag -gt 46378 ] && [ $flag -lt 69571 ]; then
docker run --rm -v $(pwd):/eric -w /eric --entrypoint /bin/sh fedora -c "dnf info $line | tee -a manifest-fed:Graphics.txt"
((++flag))
fi
done<"$1"发布于 2022-08-05 12:22:38
以分离模式创建容器:
docker run -d -t --rm --name my_container -v $(pwd):/eric -w /eric fedora在这个容器中运行命令:
docker exec my_container sh -c "echo test"发布于 2022-08-05 12:27:33
您可以将这些命令包装在一个脚本中,并将其放在映射到“/eric”卷的pwd中。然后执行如下:
docker run --rm -v $(pwd):/eric -w /eric --entrypoint /bin/sh fedora -c "/eric/dnf-info.sh 1 '$line'"脚本dnf-info.sh可能是:
#! /bin/sh
case $1 in
1)
dnf info "$2" | tee -a manifest-fed:Core.txt; dnf repoquery --srpm "$2"
;;
2)
dnf info "$2" | tee -a manifest-fed:Old.txt; dnf info "$2"
;;
3)
dnf info "$2" | tee -a manifest-fed:Graphics.txt
;;
esachttps://stackoverflow.com/questions/73249415
复制相似问题