创建 Ubuntu 运行环境
首先我们需要创建一个 Ubuntu 运行环境,在云环境中,我们创建了一个版本为 “Ubuntu 18.04 Bionic Beaver LTS” ,大小为 “small” 的虚拟服务器。
具体的创建安装环境可以参考如下的文章:
Alick:Docker: 初始化安装教程第一步(在 AWS 中创建 CentOS 7 环境)zhuanlan.zhihu.com更新系统
当我们创建好了 Ubuntu 的工作环境之后,我们就需要登录到环境中来对 Ubuntu 系统来进行更新。
alick@DevNet:~$ sudo apt-get update
Hit:1 http://ap-south-1.ec2.archive.ubuntu.com/ubuntu bionic InRelease
Hit:2 http://security.ubuntu.com/ubuntu bionic-security InRelease
Get:3 http://ap-south-1.ec2.archive.ubuntu.com/ubuntu bionic-updates InRelease [88.7 kB]
Get:4 http://ap-south-1.ec2.archive.ubuntu.com/ubuntu bionic-backports InRelease [74.6 kB]
Fetched 163 kB in 1s (154 kB/s)
Reading package lists... Done
安装必要的Packages
然后我们需要做的事情就是安装必要的 Packages
我们需要安装五个必要的package,这五个package显示在下面的列表:
- apt-transport-https
- ca-certificates
- curl
- gnupg-agent
- software-properties-common
这五个 package 是用来确认和保证 Ubuntu 服务器可以和 repository 进行 https 交互
sudo apt-get -y install
apt-transport-https
ca-certificates
curl
gnupg-agent
software-properties-common
我们使用如上命令安装完成。
alick@DevNet:~$ sudo apt-get -y install
> apt-transport-https
> ca-certificates
> curl
> gnupg-agent
> software-properties-common
Reading package lists... Done
Building dependency tree
Reading state information... Done
ca-certificates is already the newest version (20190110~18.04.1).
curl is already the newest version (7.58.0-2ubuntu3.9).
software-properties-common is already the newest version (0.96.24.32.13).
apt-transport-https is already the newest version (1.6.12ubuntu0.1).
gnupg-agent is already the newest version (2.2.4-1ubuntu1.2).
The following packages were automatically installed and are no longer required:
linux-aws-5.3-headers-5.3.0-1019 linux-headers-5.3.0-1019-aws linux-image-5.3.0-1019-aws linux-modules-5.3.0-1019-aws
Use 'sudo apt autoremove' to remove them.
0 upgraded, 0 newly installed, 0 to remove and 9 not upgraded.
添加 Docker GPG key 和 repository 到 Ubuntu
下面这个命令表示下载 Docker GPG Key
然后将下载的 Key 使用 “sudo apt-key add -“ 来进行导入
alick@DevNet:~$ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
OK
下一步不是必须的
但是我们推荐来进行这一个步骤
就是校验这个 GPG Key 的 fingerprint
这个 fingerprint 是一个静态的,我们尝试搜索最后 8 为字节。
alick@DevNet:~$ sudo apt-key fingerprint 0EBFCD88
pub rsa4096 2017-02-22 [SCEA]
9DC8 5822 9FC7 DD38 854A E2D8 8D81 803C 0EBF CD88
uid [ unknown] Docker Release (CE deb) <docker@docker.com>
sub rsa4096 2017-02-22 [S]
然后我们可以使用下面的命令来添加 repository
sudo add-apt-repository
"deb [arch=amd64] https://download.docker.com/linux/ubuntu
$(lsb_release -cs)
stable"
添加 repository 成功
alick@DevNet:~$ sudo add-apt-repository
> "deb [arch=amd64] https://download.docker.com/linux/ubuntu
> $(lsb_release -cs)
> stable"
Get:1 https://download.docker.com/linux/ubuntu bionic InRelease [64.4 kB]
Get:2 https://download.docker.com/linux/ubuntu bionic/stable amd64 Packages [12.5 kB]
Hit:3 http://ap-south-1.ec2.archive.ubuntu.com/ubuntu bionic InRelease
Hit:4 http://security.ubuntu.com/ubuntu bionic-security InRelease
Hit:5 http://ap-south-1.ec2.archive.ubuntu.com/ubuntu bionic-updates InRelease
Hit:6 http://ap-south-1.ec2.archive.ubuntu.com/ubuntu bionic-backports InRelease
Fetched 76.9 kB in 1s (132 kB/s)
Reading package lists... Done
这个时候我们重新更新一次系统来保证 repository 是最新的。
alick@DevNet:~$ sudo apt-get update
Hit:1 https://download.docker.com/linux/ubuntu bionic InRelease
Hit:2 http://ap-south-1.ec2.archive.ubuntu.com/ubuntu bionic InRelease
Hit:3 http://security.ubuntu.com/ubuntu bionic-security InRelease
Hit:4 http://ap-south-1.ec2.archive.ubuntu.com/ubuntu bionic-updates InRelease
Hit:5 http://ap-south-1.ec2.archive.ubuntu.com/ubuntu bionic-backports InRelease
Reading package lists... Done
安装 Docker CE
- 安装 Docker CE Packages 特定版本,这我们选择安装特定版本 18.09.5
- 安装 Docker CE CLI 特定版本,这我们选择安装特定版本 18.09.5
- 安装
containerd.io
这里我们不限制版本,自动选择最新的。
sudo apt-get install -y docker-ce=5:18.09.5~3-0~ubuntu-bionic
docker-ce-cli=5:18.09.5~3-0~ubuntu-bionic
containerd.io
我们使用如上命令安装 Docker CE 成功
alick@DevNet:~$ sudo apt-get install -y docker-ce=5:18.09.5~3-0~ubuntu-bionic
> docker-ce-cli=5:18.09.5~3-0~ubuntu-bionic
> containerd.io
Reading package lists... Done
Building dependency tree
Reading state information... Done
containerd.io is already the newest version (1.2.13-2).
docker-ce-cli is already the newest version (5:18.09.5~3-0~ubuntu-bionic).
docker-ce is already the newest version (5:18.09.5~3-0~ubuntu-bionic).
The following packages were automatically installed and are no longer required:
linux-aws-5.3-headers-5.3.0-1019 linux-headers-5.3.0-1019-aws linux-image-5.3.0-1019-aws linux-modules-5.3.0-1019-aws
Use 'sudo apt autoremove' to remove them.
0 upgraded, 0 newly installed, 0 to remove and 9 not upgraded.
查看 docker 版本
我们使用 sudo docker version 来查看可以看到相对应的版本
alick@DevNet:~$ sudo docker version
Client:
Version: 18.09.5
API version: 1.39
Go version: go1.10.8
Git commit: e8ff056
Built: Thu Apr 11 04:43:57 2019
OS/Arch: linux/amd64
Experimental: false
Server: Docker Engine - Community
Engine:
Version: 18.09.5
API version: 1.39 (minimum version 1.12)
Go version: go1.10.8
Git commit: e8ff056
Built: Thu Apr 11 04:10:53 2019
OS/Arch: linux/amd64
Experimental: false
但是我们使用 docker version 就遇到了之前安装 centos docker 同样的错误
alick@DevNet:~$ docker version
Client:
Version: 18.09.5
API version: 1.39
Go version: go1.10.8
Git commit: e8ff056
Built: Thu Apr 11 04:43:57 2019
OS/Arch: linux/amd64
Experimental: false
Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Get http://%2Fvar%2Frun%2Fdocker.sock/v1.39/version: dial unix /var/run/docker.sock: connect: permission denied
所以这个时候我们可以了解到是因为我们使用的用户 alick 没有权限
我们需要将用户 alick 添加到 docker group 中
那么如何给予特定的用户相对应的权限来运行 docker 命令呢?
方法很简单,那就是添加这个特定的用户到 docker group
这个 docker group 在安装 docker 的时候就自动创建好了
然后我们 logout
alick@DevNet:~$ sudo usermod -a -G docker alick
alick@DevNet:~$ exit
logout
这时我们登出然后重新登陆之后 docker version 的命令就可以在特定的用户下运行了
错误已经被消除,我们可以看到 server 的相关信息了。
alick@DevNet:~$ docker version
Client:
Version: 18.09.5
API version: 1.39
Go version: go1.10.8
Git commit: e8ff056
Built: Thu Apr 11 04:43:57 2019
OS/Arch: linux/amd64
Experimental: false
Server: Docker Engine - Community
Engine:
Version: 18.09.5
API version: 1.39 (minimum version 1.12)
Go version: go1.10.8
Git commit: e8ff056
Built: Thu Apr 11 04:10:53 2019
OS/Arch: linux/amd64
Experimental: false
在环境中测试容器hello-world
我们这个时候需要做一个简单的测试
就是我们需要验证这个环境可以运行 一个简单的container
我们使用的是一个叫做 hello-world 的 container
这个命令将会从网络下载 hello-world 容器镜像
这个 container 很简单只是 print 一些简单的信息到屏幕输出中“Hello from Docker!”
alick@DevNet:~$ docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
0e03bdcc26d7: Pull complete
Digest: sha256:d58e752213a51785838f9eed2b7a498ffa1cb3aa7f946dda11af39286c3db9a9
Status: Downloaded newer image for hello-world:latest
Hello from Docker!
This message shows that your installation appears to be working correctly.
To generate this message, Docker took the following steps:
1. The Docker client contacted the Docker daemon.
2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
(amd64)
3. The Docker daemon created a new container from that image which runs the
executable that produces the output you are currently reading.
4. The Docker daemon streamed that output to the Docker client, which sent it
to your terminal.
To try something more ambitious, you can run an Ubuntu container with:
$ docker run -it ubuntu bash
Share images, automate workflows, and more with a free Docker ID:
https://hub.docker.com/
For more examples and ideas, visit:
https://docs.docker.com/get-started/
到这一步, 我们已经完成了在 Ubuntu 中简单的安装 docker 社区版本的步骤,我特定包括了一些常见的错误信息在文章中,在日常使用中会有更多的错误信息市场出现,了解排错的方式可以帮助我们更快的解决各种问题。
如果您想回顾一下在 Centos 中安装 docker 社区版本的步骤的话,可以参考如下的这篇文章:
Alick:Docker: 初始化安装教程第三步(在CentOS7安装Docker CE)zhuanlan.zhihu.com