Docker 相关 (问题 & 常用命令)

声明

本文版权归原作者所有,未经允许禁止转载。

安装

Debian/Ubuntu

apt install -y curl gnupg gnupg1 gnupg2 software-properties-common && (source /etc/os-release; if [ "$ID" = "ubuntu" ]; then curl -fsSL https://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | apt-key add - && add-apt-repository "deb [arch=amd64] https://mirrors.aliyun.com/docker-ce/linux/ubuntu $(lsb_release -cs) stable"; elif [ "$ID" = "debian" ]; then curl -fsSL https://mirrors.aliyun.com/docker-ce/linux/debian/gpg | apt-key add - && add-apt-repository "deb [arch=amd64] https://mirrors.aliyun.com/docker-ce/linux/debian $(lsb_release -cs) stable"; else echo "Unsupported OS: $ID" && exit 1; fi) && apt update && apt install -y docker-ce

Centos

yum update -y && yum install yum-utils -y && yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo && yum install -y docker-ce docker-ce-cli containerd.io

Kali

wget -O - https://archive.kali.org/archive-key.asc | apt-key add && sed -i "s@http://kali.download/kali@https://mirrors.tuna.tsinghua.edu.cn/kali@g" /etc/apt/sources.list && curl -fsSL http://mirrors.aliyun.com/docker-ce/linux/debian/gpg | gpg --dearmor -o /etc/apt/keyrings/docker.gpg && echo "deb [arch=amd64 signed-by=/etc/apt/keyrings/docker.gpg] https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/debian bookworm stable" | tee /etc/apt/sources.list.d/docker.list && apt update -y && apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin -y

换源 & pull 断点续传

可用且速度还不错的国内源列举:

https://hub.mirrorify.net
https://docker.1ms.run
https://docker.m.daocloud.io

一条命令:

tee /etc/docker/daemon.json << EOF
{
      "registry-mirrors": ["https://hub.mirrorify.net"],
      "features": {
        "containerd-snapshotter": true
      },
      "default-ulimits": {
        "nofile": {
                "Name": "nofile","Hard": 65535,"Soft": 65535}
      }
}
EOF
 
service docker restart
tee /etc/docker/daemon.json << EOF
{
      "registry-mirrors": ["https://docker.1ms.run"],
      "features": {
        "containerd-snapshotter": true
      },
      "default-ulimits": {
        "nofile": {
                "Name": "nofile","Hard": 65535,"Soft": 65535}
      }
}
EOF
 
service docker restart
tee /etc/docker/daemon.json << EOF
{
      "registry-mirrors": ["https://docker.yinsel.top"],
      "features": {
        "containerd-snapshotter": true
      },
      "default-ulimits": {
        "nofile": {
                "Name": "nofile","Hard": 65535,"Soft": 65535}
      }
}
EOF
 
service docker restart
tee /etc/docker/daemon.json << EOF
{
      "registry-mirrors": ["https://docker.m.daocloud.io"],
      "features": {
        "containerd-snapshotter": true
      },
      "default-ulimits": {
        "nofile": {
                "Name": "nofile","Hard": 65535,"Soft": 65535}
      }
}
EOF
 
service docker restart

常用命令

端口映射时绑定 IP

docker run -d -p 127.0.0.1:8000:80 nginx

进入容器终端

docker exec -it <容器ID> /bin/bash

停止全部容器

docker stop $(docker ps -q)

删除全部容器

docker rm $(docker ps -aq)

删除全部镜像

docker rmi $(docker images -q)

停止并删除全部容器

docker stop $(docker ps -aq) && docker rm $(docker ps -aq)

查看容器 PID

docker inspect -f '{{.State.Pid}}' <ID>

查看容器 IP 地址

docker inspect <ID> | grep IPAddress

导出镜像

# 包括镜像名及tag,原封不动导出
docker save -o image.zip <image>:<tag>  

导入镜像

docker load -i image.zip

# 恢复标签
docker image tag <image-ID> <name>:tag

删除无用卷

docker volume prune -f