官网:
一、安装
1. 更新
sudo apt-get update
2. 安装依赖包
sudo apt-get install apt-transport-https ca-certificates curl software-properties-common
3. 添加官方密钥
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
4. 添加仓库
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
5. 再次更新
sudo apt-get update
6. 安装docker
sudo apt-get install -y docker-ce
二、测试
1. 查看docker服务是否启动:systemctl status docker
baoli@ubuntu:~$ systemctl status docker
● docker.service - Docker Application Container Engine
Loaded: loaded (/lib/systemd/system/docker.service; enabled; vendor preset: enabled)
Active: active (running) since Sun 2019-09-08 15:19:27 CST; 44s ago
Docs: https://docs.docker.com
Main PID: 11480 (dockerd)
Tasks: 10
Memory: 55.4M
CPU: 893ms
CGroup: /system.slice/docker.service
└─11480 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
Sep 08 15:19:26 ubuntu dockerd[11480]: time="2019-09-08T15:19:26.795250717+08:00" level=wa
Sep 08 15:19:26 ubuntu dockerd[11480]: time="2019-09-08T15:19:26.795341137+08:00" level=wa
Sep 08 15:19:26 ubuntu dockerd[11480]: time="2019-09-08T15:19:26.795349613+08:00" level=wa
Sep 08 15:19:26 ubuntu dockerd[11480]: time="2019-09-08T15:19:26.796868238+08:00" level=in
Sep 08 15:19:27 ubuntu dockerd[11480]: time="2019-09-08T15:19:27.218386729+08:00" level=in
Sep 08 15:19:27 ubuntu dockerd[11480]: time="2019-09-08T15:19:27.438357184+08:00" level=in
Sep 08 15:19:27 ubuntu dockerd[11480]: time="2019-09-08T15:19:27.747766312+08:00" level=in
Sep 08 15:19:27 ubuntu dockerd[11480]: time="2019-09-08T15:19:27.749026100+08:00" level=in
Sep 08 15:19:27 ubuntu systemd[1]: Started Docker Application Container Engine.
Sep 08 15:19:27 ubuntu dockerd[11480]: time="2019-09-08T15:19:27.818603590+08:00" level=in
lines 1-21/21 (END)
若未启动,则启动docker: sudo systemctl start docker
2. 运行hello-world
执行:sudo docker run hello-world,提示:
baoli@ubuntu:~$ sudo docker run hello-world
Unable to find image 'hello-world:latest' locally
docker: Error response from daemon: Get https://registry-1.docker.io/v2/: net/http: request canceled while waiting for connection (Client.Timeout exceeded while awaiting headers).
See 'docker run --help'.
解决:
鉴于国内网络问题,后续拉取 Docker 镜像十分缓慢,我们可以需要配置加速器来解决,我使用的是网易的镜像地址:http://hub-mirror.c.163.com。
1)sudo vim /etc/docker/daemon.json
2)添加
{
"registry-mirrors": ["http://hub-mirror.c.163.com"]
}
3)sudo service docker restart
再次执行:sudo docker run hello-world
baoli@ubuntu:~$ sudo docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
1b930d010525: Pull complete
Digest: sha256:451ce787d12369c5df2a32c85e5a03d52cbcef6eb3586dd03075f3034f10adcd
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/