docker相关操作命令集

115 阅读2分钟
  1. 清理所有未使用的镜像,filter条件过滤多久之前
docker image prune -a --filter "until=720h"
  1. 清理所有停止状态的容器
docker container prune --filter "until=720h"
  1. 清理所有没被使用的镜像,停止的容器等
docker system prune
# 控制台输出释义
WARNING! This will remove:
        - all stopped containers # 清理停止的容器
        - all networks not used by at least one container #清理没有使用的网络
        - all dangling images #清理废弃的镜像
        - all build cache #清理构建缓存

  1. Docker磁盘使用率的概况,包括镜像、容器、本地挂载和构建缓存
docker system df
# 控制台输出
[root@172e16e250e179-lc-test-node nancal]# docker system df

TYPE            TOTAL     ACTIVE    SIZE      RECLAIMABLE
Images          147       73        20.37GB   7.544GB (37%)
Containers      206       153       3.708GB   326.4MB (8%)
Local Volumes   11        4         11.32GB   4.494GB (39%)
Build Cache     0         0         0B        0B
  1. docker system info 同 docker info
  2. docker inspect 命令详解
# CPU资源使用的权重,默认为1024
    "CpuShares": 6144
# CPU周期(单位微秒),多久做一次重新分配,配合CpuQuota使用
    "CpuPeriod": 100000
# 在CPU周期内(单位微秒),可以使用的CPU时间,配合CpuPeriod使用
    "CpuQuota": 600000
# 解释上文配置数值:
    当前pod权重为默认pod的6倍,可使用的cpu时间也是默认pod的6倍;
    在0.1秒的周期内,可以使用0.6秒的cpu时间,理解为最多可完全使用CPU的6核
# 补充: stress -c 2    压测CPU的命令,占用2核的cpu时间
  1. 保存并压缩镜像
docker save iregistry.xxxxx.com/library/busybox:1.30 | gzip > busybox.tar.gz
  1. 通过进程查找容器
psid=16063   # 这里替换为要查询的进程的PID

for i in $(docker container ls --format "{{.ID}}"); 
do 
    id_count=$(docker top $i | grep ${psid} | wc -l)
     
    if [[ ${id_count} -gt 0 ]]
      then
        echo -n "$i    "
        docker inspect -f '{{.Name}}' $i | tr -d "/"
    fi
done