手动添加软件源

添加软件源

首先需要安装 apt-transport-https 等软件包支持https协议的源:

  1. $ sudo apt update
  2. $ sudo apt install apt-transport-https ca-certificates curl software-properties-common

添加源的gpg密钥:

  1. $ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add - OK

确认导入指纹为 “9DC8 5822 9FC7 DD38 854A E2D8 8D81 803C 0EBF CD88”的 GPG 公钥:

  1. $ sudo apt-key fingerprint 0EBFCD88

获取当前操作系统的代号:

  1. $ lsb_release -cs
  2. // Ubuntu 16.04 LTS: Xenial
  3. // Ubuntu 18.04 LTS: bionic

添加Docker稳定版的官方软件源:

  1. $ sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu Xenial stable"

添加成功后,再次更新apt软件包缓存:

  1. $ sudo apt-get update

安装Docker

  1. $ sudo apt install -y docker-ce

自动化脚本安装

官网脚本

  1. $ curl -sSL https://get.docker.com | sh

国内镜像

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

安装后日志分析

  1. // docker客户端
  2. Client:
  3. Version: 18.09.2
  4. API version: 1.39
  5. Go version: go1.10.6
  6. Git commit: 6247962
  7. Built: Sun Feb 10 04:13:50 2019
  8. OS/Arch: linux/amd64
  9. Experimental: false
  10. // docker服务端 - 社区版
  11. Server: Docker Engine - Community
  12. Engine:
  13. Version: 18.09.2
  14. API version: 1.39 (minimum version 1.12)
  15. Go version: go1.10.6
  16. Git commit: 6247962
  17. Built: Sun Feb 10 03:42:13 2019
  18. OS/Arch: linux/amd64
  19. Experimental: false
  20. // 如果你不想用root账号使用 Docker,你应该把你的用户加入到 “docker” 分组中(安装过程中已经新建了docker分组)
  21. If you would like to use Docker as a non-root user, you should now consider
  22. adding your user to the "docker" group with something like:
  23. sudo usermod -aG docker your-user
  24. // 记住你必须退出并重新登录才能生效
  25. Remember that you will have to log out and back in for this to take effect!
  26. // 警告:将用户添加到“docker”组将授予运行容器的能力,这些容器可用于获取docker主机上的root权限。
  27. WARNING: Adding a user to the "docker" group will grant the ability to run
  28. containers which can be used to obtain root privileges on the
  29. docker host.
  30. Refer to https://docs.docker.com/engine/security/security/#docker-daemon-attack-surface
  31. for more information.
  32. ** DOCKER ENGINE - ENTERPRISE **
  33. Test drive additional security features by activating Docker Engine - Enterprise.
  34. * Leverage FIPS 140-2 validated encryption
  35. * Run only trusted images with digital signature enforcement
  36. // 参考资源
  37. ** Learn more at https://dockr.ly/engine1 **
  38. ACTIVATE your own engine to Docker Engine - Enterprise using:
  39. sudo docker engine activate

添加用户到docker分组【可选】

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

  1. $ sudo usermod -aG docker $USER
  2. # 如果没有该分组就新建 sudo groupadd docker

退出当前终端并重新登录

启动 Docker CE

  1. $ sudo systemctl enable docker
  2. $ sudo systemctl start docker
  3. //
  4. $ sudo service docker start

测试 Docker 是否安装正确

  1. $ docker run hello-world

执行以上命令,若能正常输出以下信息,则说明安装成功。

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

国内镜像

请在 /etc/docker/daemon.json 中写入如下内容(如果文件不存在请新建该文件) :

  1. {
  2. "registry-mirrors": [
  3. "https://registry.docker-cn.com"
  4. ]
  5. }

注意,一定要保证该文件符合 json 规范,否则 Docker 将不能启动。

之后重新启动服务:

  1. $ sudo systemctl daemon-reload
  2. $ sudo systemctl restart docker

配置加速器之后,如果拉取镜像仍然十分缓慢,请手动检查加速器配置是否生效,在命令行执行 docker info ,如果从结果中看到了如下内容,说明配置成功。

  1. Registry Mirrors:
  2. https://registry.docker-cn.com/

参考