Debian 12 安装 Docker命令简单记录

前言

每次去安装docker时。都是去到处找,然后觉得命令多懒。于是在这做一下简单的记录。跟我一样懒的可以直接一行行复制执行,部分内容都有解释

代码部分

安装必要的环境:

apt update
apt upgrade -y
apt install curl vim wget gnupg dpkg apt-transport-https lsb-release ca-certificates

插入docker的gpg公钥和apt源:

curl -sSL https://download.docker.com/linux/debian/gpg | gpg --dearmor > /usr/share/keyrings/docker-ce.gpg
echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-ce.gpg] https://download.docker.com/linux/debian $(lsb_release -sc) stable" > /etc/apt/sources.list.d/docker.list
curl -sSL https://download.docker.com/linux/debian/gpg | gpg --dearmor > /usr/share/keyrings/docker-ce.gpg
echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-ce.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -sc) stable" > /etc/apt/sources.list.d/docker.list

国内机器源可以用中科大源这个:

curl -sS https://mirrors.ustc.edu.cn/docker-ce/linux/debian/gpg | gpg --dearmor > /usr/share/keyrings/docker-ce.gpg
echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-ce.gpg] https://mirrors.ustc.edu.cn/docker-ce/linux/debian/ $(lsb_release -sc) stable" > /etc/apt/sources.list.d/docker.list
curl -sS https://mirrors.ustc.edu.cn/docker-ce/linux/ubuntu/gpg | gpg --dearmor > /usr/share/keyrings/docker-ce.gpg
echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-ce.gpg] https://mirrors.ustc.edu.cn/docker-ce/linux/ubuntu/ $(lsb_release -sc) stable" > /etc/apt/sources.list.d/docker.list

然后更新一下apt库缓存,即可安装Docker CE 和 Docker Compose了。(现在的docker compose已经是插件版的了,相对于老版本,好像是命令从docker-compose 变化到 docker compose

apt update
apt install docker-ce docker-ce-cli containerd.io docker-compose-plugin

此时可以用 docker versiondocker compose version 来检查docker是否安装成功:

root@VM-Debian ~ $ docker version
Client: Docker Engine - Community
 Version:           27.3.1
 API version:       1.47
 Go version:        go1.22.7
 Git commit:        ce12230
 Built:             Fri Sep 20 11:41:11 2024
 OS/Arch:           linux/amd64
 Context:           default

Server: Docker Engine - Community
 Engine:
  Version:          27.3.1
  API version:      1.47 (minimum version 1.24)
  Go version:       go1.22.7
  Git commit:       41ca978
  Built:            Fri Sep 20 11:41:11 2024
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.7.22
  GitCommit:        7f7fdf5fed64eb6a7caf99b3e12efcf9d60e311c
 runc:
  Version:          1.1.14
  GitCommit:        v1.1.14-0-g2c9f560
 docker-init:
  Version:          0.19.0
  GitCommit:        de40ad0

root@VM-Debian ~ $ docker compose version
Docker Compose version v2.29.7

如果需要某个特定用户可以用 Docker rootless 模式运行 Docker,那么可以把这个用户也加入 docker 组,比如我们把 www-data 用户加进去:

apt install docker-ce-rootless-extras
sudo usermod -aG docker www-data

Docker 配置优化

普通的常规使用可以使用以下配置文件:

主要是开启了自定义内网IPv6地址开启容器IPv6限制日志文件大小 防止docker日志塞满硬盘

(感谢 烧饼博客 提供的docker教学配置文件部分)

cat > /etc/docker/daemon.json << EOF
{
    "log-driver": "json-file",
    "log-opts": {
        "max-size": "20m",
        "max-file": "3"
    },
    "ipv6": true,
    "fixed-cidr-v6": "fd00:dead:beef:c0::/80",
    "experimental":true,
    "ip6tables":true
}
EOF

如国内机器链接docker hub不太方便的情况下,可以使用添加了镜像源的配置文件

cat > /etc/docker/daemon.json << EOF
{
    "log-driver": "json-file",
    "log-opts": {
        "max-size": "20m",
        "max-file": "3"
    },
    "ipv6": true,
    "fixed-cidr-v6": "fd00:dead:beef:c0::/80",
    "experimental":true,
    "ip6tables":true
    "registry-mirrors": [
                "https://hub.nat.tf",
                "https://hub1.nat.tf",
                "https://hub2.nat.tf"
    ]
}
EOF

最后,重载docker,Enjoy!

systemctl daemon-reload
systemctl restart docker
© 版权声明
THE END
喜欢就支持一下吧
点赞14 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容