安装 Docker CE
卸载旧版本
较旧版本的 Docker 被称为docker
或docker-engine
。如果之前安装过,请先通过如下命令卸载。
sudo apt-get remove docker docker-engine docker.io
设置存储库
在新机器上首次安装 Docker CE,需要设置 Docker 存储库。之后,就可以从存储库安装和更新 Docker 了。
Step 1:更新 APT 包索引。
sudo apt-get update
Step 2:确保 APT 以 HTTPS 方式工作。
sudo apt-get install \
apt-transport-https \
ca-certificates \
curl \
software-properties-common
Step 3:为了确保下载有效,添加 Docker 的官方 GPG 密钥。
$ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
Step 4:将 Docker 存储库添加到 APT 源。
sudo add-apt-repository \
"deb [arch=amd64] https://download.docker.com/linux/ubuntu \
$(lsb_release -cs) \
stable"
注意,更换这个官方源后下载时可能需要 FQ,不然会出现下载中断的情况。
从存储库安装
更新 APT 包索引。
sudo apt-get update
安装最新版本的 Docker CE。
sudo apt-get install docker-ce
安装指定版本的 Docker CE,需要先列出您的仓库中可用的版本,然后选择并安装。
apt-cache madison docker-ce
然后根据完全限定版本字符串<VERSION>
安装指定版本的 Docker CE。
sudo apt-get install docker-ce=<VERSION>
安装完成后,Docker 守护进程应该会自动启动。
最后,可以通过运行hello-world
映像验证是否正确安装了 Docker CE 。
sudo docker run hello-world
正常情况下,容器运行,终端应该会有类似如下信息输出。
esofar@ubuntu:~$ docker run hello-world
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.
...
但是我的机器在运行时有如下信息输出,大概是说客户端连接不到 Docker 的守护进程。
esofar@ubuntu:~$ sudo docker run hello-world
docker: Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?.
See 'docker run --help'.
通过systemctl status docker
确认我的机器是因为 Docker 守护进程拒绝启动导致的。
esofar@ubuntu:~$ systemctl status docker
● docker.service - Docker Application Container Engine
Loaded: loaded (/lib/systemd/system/docker.service; enabled; vendor preset: enabled)
Active: failed (Result: exit-code) since Sun 2018-12-16 15:35:37 CST; 10min ago
Docs: https://docs.docker.com
Process: 21333 ExecStart=/usr/bin/dockerd -H unix:// (code=exited, status=1/FAILURE)
Main PID: 21333 (code=exited, status=1/FAILURE)
...
后面在这个 Issues 中找到了问题答案,竟然是是因为我挂了 VPN, 干扰了 Docker 守护进程默认监听的Unix
域套接字,而容器中的进程是通过它与 Docker 守护进程进行通信。关掉 VPN,systemctl restart docker
重启服务,解决。
最后再补充一点,可以通过如下命令将当前用户添加到 Docker 组,避免每次敲命令都需要在docker
前加sudo
。
sudo usermod -aG docker ${USER}
sudo gpasswd -a ${USER} docker
即时应用修改,避免重启机器。
su - ${USER}