1. 什么是 Docker
云计算重构了 ICT 系统,给社会各行各业带来了极大的变革与便利,但同样带来了新的问题:业务怎么上云?
传统 PAAS 的应用打包困难,因本地环境与云端环境不一致,用户须为每种语言、框架乃至每个版本的应用维护一个打好的包。而打包过程中,需要进行大量修改、配置、试错才能使本地应用运行环境和云端环境匹配。
而 Docker 镜像包含了应用运行所需要的所有依赖。只需在隔离的“沙盒”中运行该镜像,无需进行任何修改和配置即可运行应用。其核心在于实现应用及其运行环境整体打包以及打包格式统一。实现本地环境与云端环境的高一致性。
Docker 中文译名为 “码头工人”,而容器的英文 Container 本身也有集装箱的意思,Docker 与容器的关系也像是码头工人与集装箱之间的关系,集装箱诞生后,码头工人不再为需要专用的工具装卸不同的货物而烦恼,同理,Docker 诞生后,开发、测试以及运维人员也不用再为繁琐的环境配置而烦恼
2. 安装 Docker
2.1 虚拟机准备
从模板克隆一台虚拟机,命名
CentOS 8 Docker
修改 IP 地址为
10.1.8.30
nmcli connection modify ens160 ipv4.method manual ipv4.addresses 10.1.8.30/24 ipv4.gateway 10.1.8.2 ipv4.dns 10.1.8.2 autoconnect yes
- 保险起见,先删除旧版本 docker
yum remove docker-ce
- 安装必要组件
yum install -y yum-utils device-mapper-persistent-data lvm2
- 配置阿里 docker 镜像源
yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
2.2 安装与配置 Docker 服务
- 安装 docker
yum -y install docker-ce
- 启动 docker 服务
systemctl enable docker.service --now
docker --version # 测试安装
- 配置镜像加速
访问 华为云官网,点击 “产品”、“容器”、“容器镜像服务 SMR”
找到 “容器镜像服务 SMR”,点击 “控制台”
按要求注册或登录,到达控制台页面,点击 “镜像资源”、“镜像中心”、“镜像加速器”,按照提示配置镜像加速器
记得重启服务!
完成后在命令行测试配置
# 1. 检查注册的镜像
[root@docker ~ 14:28:33]# docker info | tail -n4
Registry Mirrors:
https://xxxxxxxxxxxxxxxxxxxxxxxxx.mirror.swr.myhuaweicloud.com/
Live Restore Enabled: false
# 2. 安装容器测试
[root@docker ~ 14:25:46]# docker run hello-world
Unable to find image 'hello-world:latest' locally # docker 先查询本地
latest: Pulling from library/hello-world
17eec7bbc9d7: Pull complete
Digest: sha256:a0dfb0xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx8737b18567
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/
# 3. 查看容器列表,检查下载是否成功
[root@docker ~ 14:26:03]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest 1b44b5a3e06a 3 weeks ago 10.1kB # success
安装完成后,拍摄一个快照(后续实验要用)
2.3 第一个实践:docker 部署 tomcat
# 部署 docker
docker run -d -p 80:80 httpd
# 验证安装
curl 10.1.8.30
3. docker C/S 分离部署
C/S 分离,即 Client(客户机)与 Server(服务器)分离
从 docker
虚拟机克隆出两台新的虚拟机,一台扮演客户机角色,一台扮演服务器角色,并对它们分别进行以下修改:
新虚拟机 1 | 新虚拟机 2 | |
---|---|---|
主机名 | docker-client | docker-server |
IP 地址 | 10.1.8.30/24 | 10.1.8.31/24 |
3.1 配置服务器
- 配置 docker 服务
docker 的配置文件位于 /usr/lib/systemd/system/docker.service
,要将主机作为服务器使用,需要配置端口开放与监听,在 ExecStart
值末尾加上 -H 监听协议://开放监听地址:端口
[root@docker-server ~ 19:10:33]# vim /usr/lib/systemd/system/docker.service
ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock -H tcp://0.0.0.0:2375
# |------- HERE -------|
- 刷新配置文件信息,重启 docker 服务,停用防火墙
systemctl daemon-reload
systemctl restart docker.service
systemctl stop firewalld
- 借助
lsof
工具检查端口监听
yum -y install lsof
lsof -i :2375
3.2 配置客户端
- 删除原来安装的 docker 服务
dnf remove docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin docker-ce-rootless-extras
- 安装 docker 客户端
yum -y install docker-ce-cli
- 直接执行命令,验证环境配置正确,如果出现报错,则说明配置正确(因为没有指定 docker 服务器)
[root@docker-client ~ 19:33:55]# docker run hello-world
docker: Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?. # error
- 连接至 docker 服务器,拉取镜像
[root@docker-client ~ 19:34:02]# docker -H 10.1.8.30 run hello-world
Hello from Docker!
This message shows that your installation appears to be working correctly.
- 验证
[root@docker-client ~ 19:43:11]# docker -H 10.1.8.30 images
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest 1b44b5a3e06a 3 weeks ago 10.1kB # success