3

安装 Docker-CE

 1 year ago
source link: https://maxqiu.com/article/detail/154
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

注:以下命令使用 root 账户

如果之前安装过docker,请先卸载旧版本



  1. apt-get remove docker docker.io containerd runc
  2. apt-get remove docker-engine

设置存储库

更新 apt 包索引并安装包以允许 apt 通过 HTTPS 使用存储库



  1. apt-get update
  2. apt-get install ca-certificates curl gnupg

添加 Docker 的官方 GPG 密钥



  1. install -m 0755 -d /etc/apt/keyrings
  2. # 以下官方地址、阿里地址二选一,秘钥文件都是一样的
  3. curl -fsSL https://download.docker.com/linux/debian/gpg | gpg --dearmor -o /etc/apt/keyrings/docker.gpg
  4. curl -fsSL http://mirrors.aliyun.com/docker-ce/linux/debian/gpg | gpg --dearmor -o /etc/apt/keyrings/docker.gpg
  5. chmod a+r /etc/apt/keyrings/docker.gpg

设置存储库并更新缓存

设置源(二选一)



  1. # 官方源
  2. echo \
  3. "deb [arch="$(dpkg --print-architecture)" signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/debian \
  4. "$(. /etc/os-release && echo "$VERSION_CODENAME")" stable" | \
  5. tee /etc/apt/sources.list.d/docker.list > /dev/null
  6. # 阿里源
  7. echo \
  8. "deb [arch="$(dpkg --print-architecture)" signed-by=/etc/apt/keyrings/docker.gpg] http://mirrors.aliyun.com/docker-ce/linux/debian \
  9. "$(. /etc/os-release && echo "$VERSION_CODENAME")" stable" | \
  10. tee /etc/apt/sources.list.d/docker.list > /dev/null


  1. apt-get update

安装 Docker-CE

安装最新版本



  1. apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin

安装指定版本

查看版本列表



  1. apt-cache madison docker-ce | awk '{ print $3 }'

查看到如下类似内容,存储库中列出可用版本



  1. 5:24.0.1-1~debian.11~bullseye
  2. 5:24.0.0-1~debian.11~bullseye
  3. 5:23.0.6-1~debian.11~bullseye
  4. 5:23.0.5-1~debian.11~bullseye
  5. ...
  6. 5:20.10.24~3-0~debian-bullseye
  7. 5:20.10.23~3-0~debian-bullseye
  8. 5:20.10.22~3-0~debian-bullseye
  9. ...

选择所需版本并安装:



  1. VERSION_STRING=5:20.10.23~3-0~debian-bullseye
  2. apt-get install docker-ce=$VERSION_STRING docker-ce-cli=$VERSION_STRING containerd.io docker-buildx-plugin docker-compose-plugin

命令同安装命令

修改cgroupdriver & 修改日志文件记录方式 & 使用阿里容器镜像服务

docker 默认使用的文件驱动是 cgroups ,如果后期使用 k8s 时需要修改为 Systemd ,而且 Systemd 更强大

参考文档:Cgroups 与 Systemd

日志文件官方教程:JSON File logging driver

docker 默认容器下载地址在国外,国内下载很慢,需要使用阿里源。阿里镜像服务官方地址(使用阿里云账号或淘宝账号登录):容器镜像服务

这里提供我的阿里源地址:



  1. mkdir -p /etc/docker
  2. tee /etc/docker/daemon.json <<-'EOF'
  3. {
  4. "registry-mirrors": ["https://0zlpdgww.mirror.aliyuncs.com"],
  5. "exec-opts": ["native.cgroupdriver=systemd"],
  6. "log-driver": "json-file",
  7. "log-opts": {
  8. "max-size": "10m",
  9. "max-file": "3"
  10. }
  11. }
  12. EOF
  13. systemctl daemon-reload

开启 Docker 服务,开启开机自启



  1. # 默认已启动,修改配置后需要重启
  2. systemctl restart docker
  3. # 设置开机自启
  4. systemctl enable docker

运行 hello-world 验证是否正确安装

执行以下脚本



  1. docker run hello-world

查看到如下类似内容



  1. Hello from Docker!
  2. This message shows that your installation appears to be working correctly.
  3. To generate this message, Docker took the following steps:
  4. 1. The Docker client contacted the Docker daemon.
  5. 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
  6. (amd64)
  7. 3. The Docker daemon created a new container from that image which runs the
  8. executable that produces the output you are currently reading.
  9. 4. The Docker daemon streamed that output to the Docker client, which sent it
  10. to your terminal.
  11. To try something more ambitious, you can run an Ubuntu container with:
  12. $ docker run -it ubuntu bash
  13. Share images, automate workflows, and more with a free Docker ID:
  14. https://hub.docker.com/
  15. For more examples and ideas, visit:
  16. https://docs.docker.com/get-started/

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK