shel脚本快速安装

  1. $ curl -fsSL get.docker.com -o get-docker.sh
  2. $ sudo sh get-docker.sh --mirror Aliyun

自定义安装

  1. # 卸载旧版本
  2. $ sudo apt-get remove docker docker-engine docker.io containerd runc
  3. # 更新 apt
  4. $ sudo apt-get update
  5. # 安装 apt包依赖
  6. $ sudo apt-get install \
  7. apt-transport-https \
  8. ca-certificates \
  9. curl \
  10. gnupg-agent \
  11. software-properties-common
  12. # 添加 Docker 的官方 GPG 密钥
  13. $ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
  14. # 下载稳定版仓库
  15. $ sudo add-apt-repository \
  16. "deb [arch=amd64] https://download.docker.com/linux/ubuntu \
  17. bionic \
  18. stable"
  19. # 安装docker
  20. $ sudo apt-get install docker-ce docker-ce-cli containerd.io
  21. # 查看是否安装成功
  22. $ docker -v


建立 docker 用户组

默认情况下,docker 命令会使用 Unix socket(opens new window)与 Docker 引擎通讯。而只有 root 用户和 docker 组的用户才可以访问 Docker 引擎的 Unix socket。出于安全考虑,一般 Linux 系统上不会直接使用 root 用户。因此,更好地做法是将需要使用 docker 的用户加入 docker 用户组

  1. #建立 docker 组:
  2. $ sudo groupadd docker
  3. #将当前用户加入 docker 组:
  4. $ sudo usermod -aG docker $USER

退出当前终端并重新登录,进行如下测试。

测试 Docker 是否安装正确

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

配置镜像加速

  1. # 打开阿里云,登录后控制台找到 -> 容器镜像服务 -> 镜像工具 -> 镜像加速器
  2. # 1
  3. $ sudo mkdir -p /etc/docker
  4. # 2 配置镜像加速地址
  5. $ sudo tee /etc/docker/daemon.json <<-'EOF'
  6. {
  7. "registry-mirrors": ["https://docker.mirrors.ustc.edu.cn"]
  8. }
  9. EOF
  10. # 3
  11. $ sudo systemctl daemon-reload
  12. # 4
  13. $ sudo systemctl restart docker