目 录CONTENT

文章目录

docker

xlong
2024-03-25 / 0 评论 / 0 点赞 / 14 阅读 / 11352 字 / 正在检测是否收录...

docker

docker system

docker system的四个子命令

[root@master1 ~]# docker system --help 
​
Usage:  docker system COMMAND
​
Manage Docker
​
Commands:
  df          Show docker disk usage # 显示docker 磁盘使用情况
  events      Get real time events from the server  #等同于 docker events
  info        Display system-wide information # 等同于docker info
  prune       Remove unused data #清理没有使用的数据,包括镜像数据,已经停止的容器
​
Run 'docker system COMMAND --help' for more information on a command.

docker system df

提供Docker整体磁盘使用率的概况,包括镜像、容器和(本地)volume。

[root@master1 ~]# docker system df --help 
​
Usage:  docker system df [OPTIONS]
​
Show docker disk usage
​
Options:
      --format string   Pretty-print images using a Go template
  -v, --verbose         Show detailed information on space usage #显示详细的空间使用信息
[root@master1 ~]# docker system df
TYPE                TOTAL               ACTIVE              SIZE                RECLAIMABLE
Images              32                  21                  5.607GB             2.268GB (40%)
Containers          43                  39                  92.7MB              35.59MB (38%)
Local Volumes       1                   0                   198.7MB             198.7MB (100%)
Build Cache         0                   0                   0B                  0B

docker system prune

[root@master1 ~]#  docker system prune --help 
​
Usage:  docker system prune [OPTIONS]
​
Remove unused data
​
Options:
  -a, --all             Remove all unused images not just dangling ones 
                        # 删除所有未使用的图像,而不仅仅是悬空的镜像
      --filter filter   Provide filter values (e.g. 'label=<key>=<value>')
  -f, --force           Do not prompt for confirmation
      --volumes         Prune volumes

默认只删除悬空的镜像,删除所有未使用的镜像使用 -a,默认不会删除volumes,删除volumes使用 --volumes

[root@localhost ~]# 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  # 清理悬空的构建缓存
Are you sure you want to continue? [y/N] y
Total reclaimed space: 0B

过滤(--filter)

过滤标志(--filter)格式为“key=value”。如果有多个过滤器,则传递多个标志(例如,--filter "foo=bar" --filter "bif=baz"

目前支持的过滤器有:

  • until ( <timestamp>) - 仅删除在给定时间戳之前创建的容器、图像和网络

  • label ( label=<key>, label=<key>=<value>, label!=<key>, or label!=<key>=<value>) - 仅删除容器、图像、网络和卷(或不带,以防label!=...使用)指定的标签。

过滤器until可以是 Unix 时间戳、日期格式的时间戳或 Go 持续时间字符串(例如10m, 1h30m)相对于守护进程机器的时间计算。日期格式时间戳支持的格式包括 RFC3339Nano、RFC3339 2006-01-02T15:04:05、、、、 和。如果您未在时间戳末尾提供时区偏移量,则将 使用守护程序上的本地时区。当提供 Unix 时间戳时,输入 seconds[.nanoseconds],其中秒是自 1970 年 1 月 1 日(午夜 UTC/GMT)以来经过的秒数,不包括闰秒(又名 Unix 纪元或 Unix 时间),以及可选的 .纳秒字段是几分之一秒,长度不超过九位数字。2006-01-02T15:04:05.999999999``2006-01-02Z07:00``2006-01-02``Z``+-00:00

过滤器label接受两种格式。一种是label=...(label=<key>label=<key>=<value>),它删除具有指定标签的容器、图像、网络和卷。另一种格式是label!=...(label!=<key>label!=<key>=<value>),它会删除没有指定标签的容器、图像、网络和卷。

[root@master1 ~]# docker system  prune -a --filter "until=24h"
WARNING! This will remove:
  - all stopped containers
  - all networks not used by at least one container
  - all images without at least one container associated to them
  - all build cache
​
  Items to be pruned will be filtered with:
  - until=24h

镜像

镜像清理脚本

cat > /usr/local/bin/cleanup-images.sh<<'EOF'
#!/bin/bash
​
date +%F-%H:%M
# 确保 Docker 客户端已经安装
if ! command -v docker &> /dev/null; then
    echo "Docker 客户端未安装,请先安装 Docker。"
    exit 1
fi
​
​
del_images() {
    for i in ${IMAGES_NAME} ;do
      # 过滤镜像仓库
      local repo=$1
      # 获取所有版本号
      ALL_VERSIONS=$(docker images --format '{{.Repository}}:{{.Tag}}' | grep $repo|grep ${i})
      
      # 获取要保留的版本号
      KEEP_VERSIONS=$(echo "$ALL_VERSIONS" | head -n 5)
      
      # 清理未使用的镜像
      for IMAGE in $ALL_VERSIONS; do
        # 对每个版本号进行循环,判断是否在保留列表中
        FOUND=0
        for KEEP_IMAGE in $KEEP_VERSIONS; do
            if [[  "$KEEP_IMAGE" == "$IMAGE" ]]; then
                FOUND=1
                break
            fi
        done
        # 如果当前版本不在保留列表中,则删除该镜像
        if [[ $FOUND -eq 0 ]]; then
            docker rmi $IMAGE
        fi
      done
    done
    
    #docker images | awk '/jingy-dev/{print $1}'|sort |uniq -c|sort -n
} 
​
​
# 清理带jingy-dev的镜像
IMAGES_NAME=`docker images | awk '/zcy-test/{print $1}'|sort |uniq |awk -F'/' '{print $3":"}'`
del_images "zcy-test"
​
IMAGES_NAME=`docker images | awk '/zcy-release/{print $1}'|sort |uniq |awk -F'/' '{print $3":"}'`
del_images "zcy-release"
​
#30 00 * * 0 /usr/local/bin/cleanup-images.sh &>> /var/log/cleanupimages.log &
EOF
​
chmod +x /usr/local/bin/cleanup-images.sh

apline

java程序一号进程问题:

pid=1 ;\
touch /proc/${pid}/cwd/.attach_pid${pid} && \
  kill -SIGQUIT ${pid} && \
  sleep 2

apline镜像查看服务使用内存:

ps -o pid,user,vsz,rss,comm,args
# java查看最大堆内存大小
java -XX:+PrintFlagsFinal -version | grep -Ei "maxheapsize"
​

代理

mkdir -p /etc/systemd/system/docker.service.d
cat > /etc/systemd/system/docker.service.d/proxy.conf<<EOF
[Service]
Environment="HTTP_PROXY=http://172.16.1.34:8015"
Environment="HTTPS_PROXY=http://172.16.1.34:8015"
Environment="NO_PROXY=localhost,127.0.0.1"
EOF
systemctl daemon-reload
systemctl restart docker
systemctl show --property=Environment docker

查看镜像编译相关命令

docker history wes-prod-docker.ccreate.site/wes-prod/jnlp-agent-maven:latest --no-trunc --format {{.CreatedBy}}

问题

systemctl status docker 出现:

Your kernel does not support swap limit capabilities or the cgroup is not mounted. Memory limited without swap.

解决方案

这是在ubuntu或其他基于Debian的系统上才会出现的问题,原因是系统默认未开启swap限制。(开启后会使系统内存占用多1%,性能下降约10%,即使没有运行docker)

  • 修改系统的/etc/default/grub文件。使用vim在这个文件中添加一行:

GRUB_CMDLINE_LINUX="cgroup_enable=memory swapaccount=1"
  • 更新系统的GRUB:

update-grub

0

评论区