docker-cli

商风华
2023-12-01

删除dangling镜像

sudo docker rmi $(sudo docker images -q -f dangling=true)

删除镜像

sudo docker rmi $(sudo docker images -q)

格式化输出

不带表头

sudo docker ps -a --format "{{.ID}}\t{{.Image}}\t{{.Names}}\t{{.Status}}"

带有表头

docker ps -a --format "table {{.ID}}\t{{.Image}}\t{{.Names}}"
docker images  --format "{{.Repository}}:{{.Tag}}"

查看容器挂载信息

docker inspect nginx | jq  .[].Mounts
docker inspect   容器名字  | jq .[].Config.Env
docker  inspect    容器名字   | jq .[].Config.Cmd
docker inspect   容器名字  | jq .[].HostConfig.NetworkMode
docker inspect    容器名字  | jq .[].HostConfig.Tmpfs

查容器日志路径

docker inspect  nginx | jq .[0].LogPath

清空日志

logpath=`sudo docker nginx  | jq .[0].LogPath | sed 's/\"//g'` &&    sudo sh -c  'echo "" >   '${logpath}' '

镜像导出导入 压缩

docker save nginx | gzip >  nginx.tgz
gunzip  -c nginx.tgz | docker load

批量导入 tgz

for line in `ls *.tgz`; do  gunzip -c   $line  | docker load   ; done
for line in `docker ps -a --format "{{.Image}}"`;do echo `basename $line` ; done
for line in `docker ps -a --format "{{.Image}}"`;do docker save $line | gzip >  `basename $line`.tgz  ; done
for line in `docker ps -a --format "{{.Image}}"` ;do echo `basename $line`| tr -s ":" "-"; done
for line in $(docker ps -a --format "{{.Image}}");do  docker save $line | gzip > $(echo $(basename $line)| tr -s ":" "-").tgz; done

批量拉取表格声明的镜像

cat images 
hub.xxy.cn/yyy/	app-a	2.0.44.8_beta-df072a3	ccc-version
hub.xxy.cn/yyy/	app-b	2.0.44.8_beta-df072a3	ccc-version
for image in  `grep -v '^\s*$' images|awk '{print $1,$2,$3}'|sed -s 's@/ @/@g'|sed -s 's@ @:@g'`; do  docker pull  $image;done;
images=`grep -v '^\s*$' images|awk '{print $1,$2,$3}'|sed -s 's@/ @/@g'|sed -s 's@ @:@g'`;for image in $images;do imageName=`echo $image | rev | cut -d'/' -f 1 | rev`; name=`echo $imageName | cut -d ':' -f1`; tag=`echo $imageName | cut -d ':' -f2`;docker save   $image | gzip >  $name-$tag.tgz   ;done;
for line in `docker ps  --format "{{.Names}}"`;do   docker inspect  $line  > $line.json  ; done
for line in `docker ps  --format "{{.Names}}"`;do   echo $line ;docker inspect  $line  | jq .[].Mounts | grep '/data/record'; done

一次性运行容器

docker run --rm   -p 80:80  -ti  --name  test yimtune/nginx   echo "hello"
 类似资料: