Docker操作指南

349 阅读1分钟
  • 删除none镜像:docker rmi -f $(docker images -f "dangling=true" -q)
  • 删除已停止运行的容器:docker rm $(docker ps -a | grep 'Exited' | awk '{ print $1 }')
  • 以命令行登陆容器:docker exec -it 容器ID /bin/bash
  • 删除所有容器:docker rm $(docker ps -aq)
  • 停止所有容器:docker stop $(docker ps -a -q)
  • 清空构建缓存:docker builder prune -a
  • 查看镜像启动错误:docker run image:tag,然后看错误日志
  • 进入镜像看信息:docker run -it image:tag /bin/bash
  • 查看容器运行命令
    1. 安装runlike命令:pip install runlike
    2. 查看运行原始命令:runlike -p <container_name>
  • docker-compose镜像整体导出和还原:docker compose images | awk 'FNR > 1 {print $2":"$3}' | sort -u | xargs docker save -o images.tar
    1. 列出当前docker-compose文件使用到的镜像信息:docker compose images
    2. 从1步骤中获取Repository:Tag镜像列表:awk 'FNR > 1 {print $2":"$3}'
    3. 去除重复镜像:sort -u
    4. 使用2步骤的镜像信息作为docker save命令参数合并导出多个镜像到images.tar文件中:xargs docker save -o images.tar
  • 通用Linux安装流程
    1. 下载静态包:https://download.docker.com/linux/static/stable/x86_64/
    2. docker-XX.X.X.tgz解压,并把解压后的文件移至/usr/bin文件夹下
    3. 新建文本文件docker.service,填入如下内容后移至/etc/systemd/system
      [Unit]
      Description=Docker Application Container Engine
      Documentation=https://docs.docker.com
      After=network-online.target firewalld.service
      Wants=network-online.target
      
      [Service]
      Type=notify
      # the default is not to use systemd for cgroups because the delegate issues still
      # exists and systemd currently does not support the cgroup feature set required
      # for containers run by docker
      ExecStart=/usr/bin/dockerd
      ExecReload=/bin/kill -s HUP $MAINPID
      # Having non-zero Limit\*s causes performance problems due to accounting overhead
      # in the kernel. We recommend using cgroups to do container-local accounting.
      LimitNOFILE=infinity
      LimitNPROC=infinity
      LimitCORE=infinity
      # Uncomment TasksMax if your systemd version supports it.
      # Only systemd 226 and above support this version.
      #TasksMax=infinity
      TimeoutStartSec=0
      # set delegate yes so that systemd does not reset the cgroups of docker containers
      Delegate=yes
      # kill only the docker process, not all processes in the cgroup
      KillMode=process
      # restart the docker process if it exits prematurely
      Restart=on-failure
      StartLimitBurst=3
      StartLimitInterval=60s
      
      [Install]
      WantedBy=multi-user.target
      
    4. 执行命令systemctl daemon-reload,移交管理权给系统
    5. systemctl enable docker.service设置开启自启
    6. 设置docker为rootless模式

      不设需添加sudo前缀

      sudo groupadd docker
      sudo gpasswd -a ${USER} docker
      sudo systemctl restart docker
      sudo chmod a+rw /var/run/docker.sock
      
    7. 安装compose
      1. 下载插件放在指定文件夹中
        sudo mkdir -p /usr/local/lib/docker/cli-plugins
        sudo curl -SL https://github.com/docker/compose/releases/download/v2.17.2/docker-compose-linux-x86_64 -o /usr/local/lib/docker/cli-plugins/docker-compose
        
      2. 配置权限sudo chmod +x /usr/local/lib/docker/cli-plugins/docker-compose