安装 Docker
平台支持
Docker CE 支持多种平台,如下表所示
桌面
平台 | 架构 |
---|---|
Docker Desktop for Mac (macOS) | X64 |
Docker Desktop for Windows (Microsoft Windows 10) | X64 |
服务器
平台 | x86_64 / amd64 | ARM | ARM64 / AARCH64 | IBM Power (ppc64le) | IBM Z (s390x) |
---|---|---|---|---|---|
CentOS | ✔ | ✔ | |||
Debian | ✔ | ✔ | ✔ | ||
Fedora | ✔ | ✔ | |||
Ubuntu | ✔ | ✔ | ✔ | ✔ | ✔ |
准备安装
卸载旧版本
就版本的Docker称为docker或者docker-engine,使用以下命令卸载旧版本
sudo apt-get remove docker \
docker-engine \
docker.io
Ubuntu 16.04 +
Ubuntu 16.04 + 上的 Docker CE 默认使用 overlay2 存储层驱动,无需手动配置。
使用 APT 安装
安装必要的一些系统工具
sudo apt-get update
sudo apt-get -y install apt-transport-https ca-certificates curl software-properties-common
安装 GPG 证书
curl -fsSL http://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | sudo apt-key add -
写入软件源信息
sudo add-apt-repository "deb [arch=amd64] http://mirrors.aliyun.com/docker-ce/linux/ubuntu $(lsb_release -cs) stable"
更新并安装 Docker CE
sudo apt-get -y update
sudo apt-get -y install docker-ce
启动 Docker CE
sudo systemctl enable docker
sudo systemctl start docker
验证安装是否成功
docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
ca4f61b1923c: Pull complete
Digest: sha256:be0cd392e45be79ffeffa6b05338b98ebb16c87b255f48e297ec7f98e123905c
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://cloud.docker.com/
For more examples and ideas, visit:
https://docs.docker.com/engine/userguide/
配置 Docker 镜像加速器
阿里云加速器(推荐)
官方提供中国区镜像
https://registry.docker-cn.com
配置加速器
以配置阿里云加速器为例,首先 登录阿里云(没有账号请先注册),搜索 容器镜像服务
找到你的专属加速器
通过修改 daemon 配置文件 /etc/docker/daemon.json
来使用加速器
tee /etc/docker/daemon.json <<-'EOF'
{
"registry-mirrors": ["https://xxxxxxxx.mirror.aliyuncs.com"]
}
EOF
# 重启 Docker
systemctl daemon-reload
systemctl restart docker
验证配置是否成功
docker info
# 输出如下
Containers: 38
Running: 18
Paused: 0
Stopped: 20
Images: 10
Server Version: 18.09.6
Storage Driver: overlay2
Backing Filesystem: extfs
Supports d_type: true
Native Overlay Diff: true
Logging Driver: json-file
Cgroup Driver: cgroupfs
Plugins:
Volume: local
Network: bridge host macvlan null overlay
Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog
Swarm: inactive
Runtimes: runc
Default Runtime: runc
Init Binary: docker-init
containerd version: bb71b10fd8f58240ca47fbb579b9d1028eea7c84
runc version: 2b18fe1d885ee5083ef9f0838fee39b62d653e30
init version: fec3683
Security Options:
apparmor
seccomp
Profile: default
Kernel Version: 4.15.0-51-generic
Operating System: Ubuntu 18.04.2 LTS
OSType: linux
Architecture: x86_64
CPUs: 2
Total Memory: 1.924GiB
Name: kubernetes-master
ID: PJ4H:7AF2:P5UT:6FMR:W4DI:SSWR:IQQR:J6QO:ARES:BOAC:ZVMO:SV2Y
Docker Root Dir: /var/lib/docker
Debug Mode (client): false
Debug Mode (server): false
Registry: https://index.docker.io/v1/
Labels:
Experimental: false
Insecure Registries:
127.0.0.0/8
## 这里是你配置的镜像加速器
Registry Mirrors:
https://xxxxxxxx.mirror.aliyuncs.com/
Live Restore Enabled: false
Product License: Community Engine
WARNING: No swap limit support
运行第一个容器
我们以 Nginx 为例,体验 Docker 是如何运行容器的
# 下载镜像
docker pull nginx
# 运行容器
docker run --name nginx-container -p 80:80 -d nginx
浏览器输入虚拟机地址即可访问 Nginx