1、环境准备
如需用好容器技术,系统内核版本需要在3.8以上(也就是需要使用7以上的系统版本)
[root@wzx-docker ~]# cat /etc/redhat-release
CentOS Linux release 7.5.1804 (Core)
[root@wzx-docker ~]# uname -a
Linux wzx-docker 3.10.0-1127.8.2.el7.x86_64 #1 SMP Tue May 12 16:57:42 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux
2、安装 Docker Engine-Community
使用 Docker 仓库进行安装
在新主机上首次安装 Docker Engine-Community 之前,需要设置 Docker 仓库;之后,您可以从仓库安装和更新 Docker。
(1)设置仓库
安装所需的软件包。yum-utils 提供了 yum-config-manager
[root@wzx-docker ~]# yum install -y yum-utils
(2)使用yum-config-manager命令来设置稳定的仓库,本次下载使用的是阿里云的docker-ce的仓库
[root@wzx-docker ~]# yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
已加载插件:langpacks, versionlock
adding repo from: http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
grabbing file http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo to /etc/yum.repos.d/docker-ce.repo
repo saved to /etc/yum.repos.d/docker-ce.repo
(3)也可以使用其他仓库:
官方源地址(国内下载比较慢)
[root@wzx-docker ~]# yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
清华大学源
[root@wzx-docker ~]# yum-config-manager --add-repo https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/centos/docker-ce.repo
3、安装 Docker Engine-Community
要安装特定版本的 Docker Engine-Community,请在存储库中列出可用版本,然后选择并安装:
(1)列出并排序您存储库中可用的版本。此示例按版本号(从高到低)对结果进行排序。
[root@wzx-docker ~]# yum list docker-ce --show-duplicates |sort -r
已加载插件:langpacks, versionlock
可安装的软件包
Excluding 1 update due to versionlock (use "yum versionlock status" to show it)
docker-ce.x86_64 3:20.10.7-3.el7 docker-ce-stable
docker-ce.x86_64 3:20.10.6-3.el7 docker-ce-stable
docker-ce.x86_64 3:20.10.5-3.el7 docker-ce-stable
docker-ce.x86_64 3:20.10.4-3.el7 docker-ce-stable
docker-ce.x86_64 3:20.10.3-3.el7 docker-ce-stable
docker-ce.x86_64 3:20.10.2-3.el7 docker-ce-stable
(2)通过其完整的软件包名称安装特定版本,该软件包名称是软件包名称(docker-ce)加上版本字符串(第二列),从第一个冒号(:)一直到第一个连字符,并用连字符(-)分隔。例如:docker-ce.x86_64 3:19.03.9-3.el7
[root@wzx-docker ~]# yum install docker-ce.x86_64 3:19.03.9-3.el7
如果提示您接受 GPG 密钥,请选是
从 https://mirrors.aliyun.com/docker-ce/linux/centos/gpg 检索密钥
导入 GPG key 0x621E9F35:
是否继续?[y/N]:y
(3)配置Docker
[root@wzx-docker ~]# cd /etc/docker/ #进入到/etc/docker/
[root@wzx-docker docker]# vim daemon.json #配置daemon.json初始化文件
{
"graph": "/data/docker",
"storage-driver": "overlay2",
"insecure-registries": ["registry.access.redhat.com","quay.io"],
"registry-mirrors": ["https://q2gr04ke.mirror.aliyuncs.com"],
"bip": "192.168.4.20/24",
"exec-opts": ["native.cgroupdriver=systemd"],
"log-opts": {"max-size":"32M", "max-file":"2"},
"live-restore": true
}
(4)设置Docker为系统自启动
[root@wzx-docker ~]# systemctl enable docker.service
Created symlink from /etc/systemd/system/multi-user.target.wants/docker.service to /usr/lib/systemd/system/docker.service.
(5)启动Docker
[root@wzx-docker ~]# systemctl start docker.service
(6)验证Docker启动状态
[root@wzx-docker ~]# docker info
Client:
Context: default
Debug Mode: false
Plugins:
app: Docker App (Docker Inc., v0.9.1-beta3)
buildx: Build with BuildKit (Docker Inc., v0.5.1-docker)
scan: Docker Scan (Docker Inc., v0.8.0)
Server:
Containers: 0 #容器数量
Running: 0
Paused: 0
Stopped: 0
Images: 0 # 镜像数量
Server Version: 20.10.7 # server版本
Storage Driver: overlay2
Backing Filesystem: extfs # 宿主机底层文件系统
Supports d_type: true
Native Overlay Diff: true
userxattr: false
Logging Driver: json-file
Cgroup Driver: systemd # Cgroups 驱动
Cgroup Version: 1
Plugins:
Volume: local
Network: bridge host ipvlan macvlan null overlay
Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog
Swarm: inactive
Runtimes: io.containerd.runc.v2 io.containerd.runtime.v1.linux runc
Default Runtime: runc
Init Binary: docker-init
containerd version: d71fcd7d8303cbf684402823e425e9dd2e99285d
runc version: b9ee9c6314599f1b4a7f497e1f1f856fe433d3b7
init version: de40ad0
Security Options:
seccomp
Profile: default
Kernel Version: 3.10.0-1127.8.2.el7.x86_64 #宿主机内核版本
Operating System: CentOS Linux 7 (Core)
OSType: linux
Architecture: x86_64
CPUs: 1
Total Memory: 990.9MiB
Name: wzx-docker
ID: UQIF:YGVO:B6NI:EHQH:5UFW:U2RH:UQC2:I4Z7:56EV:JGPY:GKIH:OUYJ
Docker Root Dir: /data/docker # docker 数据存储目录
Debug Mode: false
Registry: https://index.docker.io/v1/ # registry 地址
Labels:
Experimental: false
Insecure Registries:
quay.io
registry.access.redhat.com
127.0.0.0/8
Registry Mirrors: # 仓库地址
https://q2gr04ke.mirror.aliyuncs.com/
Live Restore Enabled: true
4、Docker Hello world
Docker 允许你在容器内运行应用程序, 使用 docker run 命令来在容器内运行一个应用程序。
[root@wzx-docker ~]# docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
b8dfde127a29: Pull complete
Digest: sha256:9f6ad537c5132bcce57f7a0a20e317228d382c3cd61edae14650eec68b2b345c
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/
5、容器启动过程:
(1) Docker客户端联系Docker服务端。
(2)Docker服务端从Docker Hub中拉取hello-world”映像。
(3)Docker服务端(用新拉的镜像)创建了一个新的容器,该容器运行可执行文件(脚本),生成当前正在读取的输出的可执行文件。
(4)Docker服务端将信息流传输到Docker客户端,由Docker客户端展示在你的终端。