1. ![image.png](https://cdn.nlark.com/yuque/0/2022/png/26240381/1654841146821-adc0bfcc-146b-4b10-b187-0bcce0017804.png#clientId=u65ce1ebf-8c91-4&crop=0&crop=0&crop=1&crop=1&from=paste&height=101&id=fhkKZ&margin=%5Bobject%20Object%5D&name=image.png&originHeight=151&originWidth=151&originalType=binary&ratio=1&rotation=0&showTitle=false&size=40438&status=done&style=none&taskId=u04a006c7-219d-4e68-ba54-5357d8c592a&title=&width=100.66666666666667)** 江西现代职业技术学院** <br />**容器云技术**<br />**实训报告**<br />** 学 号: 20628670112 **<br />** 姓 名: 黄文杰 **<br />** 学 院: 信息工程学院 **<br />** 专 业: 云计算技术与应用 **<br />** 班 级: 2020级 **<br />** 指导教师: 刘芃 **

一、项目目的

容器技术是近几年云行业发展中不可缺少的一环。Docker和k8s的大热极大可能会推动云计算PAAS层的完善和普及。以Docker为基础,本次项目任务与目的分为五点:
1、了解Docker相关基础知识;
2、掌握DOCKER安装;
3、了解Docker镜像、容器和操作命令;
4、掌握Docker file的编写;
5、使学生养成在学习中相互配合,团结协作的习惯。


二、项目要求

综合项目的内容应该包括以下知识点:
1、安装DOCKER
2、安装并使用本地仓库
3、使用Dockerfile构建镜像
4、DockerCompose
5、Docker SWARM搭建
6、使用DOCKER STACK搭建服务栈


三、项目实施过程

3.1 docker 安装

3.1.1 准备一台最小安装的CentOS主机,保证此CentOS主机可以顺利访问Internet

  1. [root@hwj ~]# ip add
  2. 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1
  3. link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
  4. inet 127.0.0.1/8 scope host lo
  5. valid_lft forever preferred_lft forever
  6. inet6 ::1/128 scope host
  7. valid_lft forever preferred_lft forever
  8. 2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
  9. link/ether 00:0c:29:a0:0d:4a brd ff:ff:ff:ff:ff:ff
  10. inet 192.168.20.200/24 brd 192.168.20.255 scope global ens33
  11. valid_lft forever preferred_lft forever
  12. inet6 fe80::4158:b4b2:c6b4:819e/64 scope link
  13. valid_lft forever preferred_lft forever
  14. [root@hwj ~]# ping -c 3 www.baidu.com
  15. PING www.wshifen.com (45.113.192.101) 56(84) bytes of data.
  16. 64 bytes from 45.113.192.101 (45.113.192.101): icmp_seq=1 ttl=128 time=236 ms
  17. 64 bytes from 45.113.192.101 (45.113.192.101): icmp_seq=2 ttl=128 time=247 ms
  18. 64 bytes from 45.113.192.101 (45.113.192.101): icmp_seq=3 ttl=128 time=235 ms
  19. --- www.wshifen.com ping statistics ---
  20. 3 packets transmitted, 3 received, 0% packet loss, time 2005ms
  21. rtt min/avg/max/mdev = 235.514/240.084/247.955/5.603 ms

3.1.2 关闭防火墙、selinux和清空iptables

  1. [root@hwj ~]# systemctl stop firewalld
  2. [root@hwj ~]# systemctl disable firewalld
  3. Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
  4. Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
  5. [root@hwj ~]# setenforce 0
  6. [root@hwj ~]# vim /etc/sysconfig/selinux
  7. [root@hwj ~]# cat /etc/sysconfig/selinux
  8. # This file controls the state of SELinux on the system.
  9. # SELINUX= can take one of these three values:
  10. # enforcing - SELinux security policy is enforced.
  11. # permissive - SELinux prints warnings instead of enforcing.
  12. # disabled - No SELinux policy is loaded.
  13. SELINUX=disabled
  14. [root@hwj ~]# getenforce
  15. Permissive
  16. [root@hwj ~]# iptables -F
  17. [root@hwj ~]# iptables -X
  18. [root@hwj ~]# iptables -Z
  19. [root@hwj ~]# iptables -save
  20. [root@hwj ~]# iptables-save
  21. # Generated by iptables-save v1.4.21 on Thu Jun 9 08:48:05 2022
  22. *filter
  23. :INPUT ACCEPT [36:2196]
  24. :FORWARD ACCEPT [0:0]
  25. :OUTPUT ACCEPT [22:1912]
  26. COMMIT
  27. # Completed on Thu Jun 9 08:48:05 2022

3.1.3 修改yum源为阿里云

  1. [root@hwj ~]# yum install wget
  2. [root@hwj ~]# cd /etc/yum.repos.d
  3. [root@hwj yum.repos.d]# mkdir upload
  4. [root@hwj yum.repos.d]# mv CentOS-*.repo upload
  5. [root@hwj yum.repos.d]# ls
  6. upload
  7. [root@hwj yum.repos.d]# wget -O /etc/yum.repos.d/Centos-7.repo
  8. http://mirrors.aliyun.com/repo/Centos-7.repo
  9. --2022-06-09 08:54:45-- http://mirrors.aliyun.com/repo/Centos-7.repo
  10. 正在解析主机 mirrors.aliyun.com (mirrors.aliyun.com)
  11. ... 106.225.243.214, 106.225.243.215, 106.225.243.216, ...
  12. 正在连接 mirrors.aliyun.com (mirrors.aliyun.com)|106.225.243.214|:80... 已连接。
  13. 已发出 HTTP 请求,正在等待回应... 200 OK
  14. 长度:2523 (2.5K) [application/octet-stream]
  15. 正在保存至: “/etc/yum.repos.d/Centos-7.repo
  16. 100%[====================================================>] 2,523 --.-K/s 用时 0.01s
  17. 2022-06-09 08:54:46 (202 KB/s) - 已保存 “/etc/yum.repos.d/Centos-7.repo [2523/2523])
  18. [root@hwj yum.repos.d]# yum clean all &&yum makecache&&yum repolist
  19. Determining fastest mirrors
  20. * base: mirrors.aliyun.com
  21. * extras: mirrors.aliyun.com
  22. * updates: mirrors.aliyun.com
  23. 元数据缓存已建立
  24. 已加载插件:fastestmirror
  25. Loading mirror speeds from cached hostfile
  26. * base: mirrors.aliyun.com
  27. * extras: mirrors.aliyun.com
  28. * updates: mirrors.aliyun.com
  29. 源标识 源名称 状态
  30. base/7/x86_64 CentOS-7 - Base - mirrors.aliyun.com 10,072
  31. extras/7/x86_64 CentOS-7 - Extras - mirrors.aliyun.com 512
  32. updates/7/x86_64 CentOS-7 - Updates - mirrors.aliyun.com 3,863
  33. repolist: 14,447

3.1.4 设置时间同步

  1. [root@hwj yum.repos.d]# yum install -y ntp net-tools
  2. [root@hwj yum.repos.d]# ntpdate cn.pool.ntp.org
  3. 9 Jun 09:05:26 ntpdate[12069]: adjust time server 84.16.73.33 offset -0.004139 sec

3.1.5 安装Docker依赖环境(运行必备主件)

  1. [root@hwj yum.repos.d]# yum install -y yum-utils device-mapper-persisitent-data lvm2

3.1.6 配置阿里云Docker-CE源

  1. [root@hwj ~]# yum-config-manager --add-repo
  2. http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
  3. 已加载插件:fastestmirror
  4. adding repo from: http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
  5. grabbing file http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
  6. to /etc/yum.repos.d/docker-ce.repo
  7. repo saved to /etc/yum.repos.d/docker-ce.repo

3.1.7 安装Docker-CE最新版本并查看是否正常安装

  1. [root@hwj ~]# yum install -y docker-ce
  2. [root@hwj ~]# docker -v
  3. Docker version 20.10.17, build 100c701
  4. [root@hwj ~]# docker version
  5. Client: Docker Engine - Community
  6. Version: 20.10.17
  7. API version: 1.41
  8. Go version: go1.17.11
  9. Git commit: 100c701
  10. Built: Mon Jun 6 23:05:12 2022
  11. OS/Arch: linux/amd64
  12. Context: default
  13. Experimental: true

3.1.8 登录阿里云、添加docker本地镜像加速器

image.png

  1. [root@hwj ~]# mkdir -p /etc/docker
  2. [root@hwj ~]# tee /etc/docker/daemon.json <<-'EOF'
  3. > {
  4. > "registry-mirrors": ["https://dgodf4l3.mirror.aliyuncs.com"]
  5. > }
  6. > EOF
  7. {
  8. "registry-mirrors": ["https://dgodf4l3.mirror.aliyuncs.com"]
  9. }
  10. [root@hwj ~]# systemctl daemon-reload
  11. [root@hwj ~]# systemctl restart docker

3.1.9 启动Docker服务,并检查信息

  1. [root@hwj ~]# systemctl start docker
  2. [root@hwj ~]# systemctl enable docker
  3. Created symlink from /etc/systemd/system/multi-user.target.wants/docker.service
  4. to /usr/lib/systemd/system/docker.service
  5. [root@hwj ~]# docker info
  6. Client:
  7. Context: default
  8. Debug Mode: false
  9. Plugins:
  10. app: Docker App (Docker Inc., v0.9.1-beta3)
  11. buildx: Docker Buildx (Docker Inc., v0.8.2-docker)
  12. scan: Docker Scan (Docker Inc., v0.17.0)
  13. Server:
  14. Containers: 0
  15. Running: 0
  16. Paused: 0
  17. Stopped: 0
  18. Images: 0
  19. Server Version: 20.10.17
  20. Storage Driver: overlay2
  21. Backing Filesystem: extfs
  22. Supports d_type: true
  23. Native Overlay Diff: true
  24. userxattr: false
  25. Logging Driver: json-file
  26. Cgroup Driver: cgroupfs
  27. Cgroup Version: 1
  28. Plugins:
  29. Volume: local
  30. Network: bridge host ipvlan macvlan null overlay
  31. Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog
  32. Swarm: inactive
  33. Runtimes: io.containerd.runc.v2 io.containerd.runtime.v1.linux runc
  34. Default Runtime: runc
  35. Init Binary: docker-init
  36. containerd version: 10c12954828e7c7c9b6e0ea9b0c02b01407d3ae1
  37. runc version: v1.1.2-0-ga916309
  38. init version: de40ad0
  39. Security Options:
  40. seccomp
  41. Profile: default
  42. Kernel Version: 3.10.0-693.el7.x86_64
  43. Operating System: CentOS Linux 7 (Core)
  44. OSType: linux
  45. Architecture: x86_64
  46. CPUs: 2
  47. Total Memory: 3.686GiB
  48. Name: hwj
  49. ID: GS3O:EENF:MDAA:RHSX:SAAW:DQ7A:XYVV:DC74:445I:VDR3:SQC6:ISAK
  50. Docker Root Dir: /var/lib/docker
  51. Debug Mode: false
  52. Registry: https://index.docker.io/v1/
  53. Labels:
  54. Experimental: false
  55. Insecure Registries:
  56. 127.0.0.0/8
  57. Registry Mirrors:
  58. https://dgodf4l3.mirror.aliyuncs.com/
  59. Live Restore Enabled: false

3.1.10 运行镜像进行测试

  1. [root@hwj ~]# docker run -d -p 1280:80 alexwhen/docker-2048
  2. Unable to find image 'alexwhen/docker-2048:latest' locally
  3. latest: Pulling from alexwhen/docker-2048
  4. Image docker.io/alexwhen/docker-2048:latest uses outdated schema1 manifest format. Please upgrade to a schema2 image for better future compatibility. More information at https://docs.docker.com/registry/spec/deprecated-schema-v1/
  5. c862d82a67a2: Pull complete
  6. a3ed95caeb02: Pull complete
  7. 69dbbd8c451d: Pull complete
  8. e9b345a0f742: Pull complete
  9. Digest: sha256:4913452e5bd092db9c8b005523127b8f62821867021e23a9acb1ae0f7d2432e1
  10. Status: Downloaded newer image for alexwhen/docker-2048:latest
  11. 4f15612768e00dd4059cfa02116ab526d2707cbb6a95331198e7ab69b5cae491

image.png

3.2安装并使用本地仓库

3.2.1 拉取本地仓库工具镜像registry

  1. [root@hwj ~]# docker pull registry
  2. Using default tag: latest
  3. latest: Pulling from library/registry
  4. 79e9f2f55bf5: Pull complete
  5. 0d96da54f60b: Pull complete
  6. 5b27040df4a2: Pull complete
  7. e2ead8259a04: Pull complete
  8. 3790aef225b9: Pull complete
  9. Digest: sha256:169211e20e2f2d5d115674681eb79d21a217b296b43374b8e39f97fcf866b375
  10. Status: Downloaded newer image for registry:latest
  11. docker.io/library/registry:latest

3.2.2 运行一个Registry镜像仓库的容器实例

  1. [root@hwj ~]# docker run -itd --restart=always \ #在后台运行交互模式,并给予一个伪终端
  2. > --name hwj_registry \
  3. > -p 12500:5000 \
  4. > -v /hwj_registry/registry:/var/lib/registry \ #持久储存
  5. > registry
  6. 5405c590ef0a66a445aa95c0b6b5f01e110b85386375672ffb3b20cca9d8ac3a

3.2.3 查看当前仓库下的镜像

  1. [root@hwj ~]# curl 192.168.20.200:12500/v2/_catalog
  2. {"repositories":[]}

3.2.4 添加本地不安全的仓库信息

  1. [root@hwj ~]# vim /etc/docker/daemon.json
  2. [root@hwj ~]# cat /etc/docker/daemon.json
  3. {
  4. "insecure-registries": ["192.168.20.200:12500"],
  5. "registry-mirrors": ["https://dgodf4l3.mirror.aliyuncs.com"]
  6. }

3.2.5 重新加载Docker服务配置文件并重新启动docker

  1. [root@hwj ~]# systemctl daemon-reload
  2. [root@hwj ~]# systemctl restart docker

3.2.6 修改镜像标签(重命名)

  1. [root@hwj ~]# docker images
  2. REPOSITORY TAG IMAGE ID CREATED SIZE
  3. nginx latest 605c77e624dd 5 months ago 141MB
  4. registry latest b8604a3fe854 6 months ago 26.2MB
  5. [root@hwj ~]# docker tag 605 192.168.20.200:12500/nginx:latest

3.2.7 上传镜像

  1. [root@hwj ~]# docker push 192.168.20.200:12500/nginx:latest
  2. The push refers to repository [192.168.20.200:12500/nginx]
  3. d874fd2bc83b: Pushed
  4. 32ce5f6a5106: Pushed
  5. f1db227348d0: Pushed
  6. b8d6e692a25e: Pushed
  7. e379e8aedd4d: Pushed
  8. 2edcec3590a4: Pushed
  9. latest: digest: sha256:ee89b00528ff4f02f2405e4ee221743ebc3f8e8dd0bfd5c4c20a2fa2aaa7ede3
  10. size: 1570

3.2.8 查看本次仓库镜像

  1. [root@hwj ~]# curl 192.168.20.200:12500/v2/_catalog
  2. {"repositories":["nginx"]}

3.3 使用Dockerfile构建镜像

3.3.1 创建上下文环境并创建首页文件(压缩)

  1. [root@hwj ~]# mkdir httpd&&cd httpd
  2. [root@hwj httpd]# vim index.html
  3. [root@hwj httpd]# cat index.html
  4. well,so good which httpd mode in hwj's compute
  5. [root@hwj httpd]# tar -czvf index.html.tar index.html
  6. index.html
  7. [root@hwj httpd]# ls
  8. index.html index.html.tar

3.3.2 编写Dockerfile

  1. [root@hwj httpd]# vim Dockerfile
  2. [root@hwj httpd]# cat Dockerfile
  3. ARG version=7
  4. FROM centos:${version}
  5. MAINTAINER HWJ
  6. RUN mkdir /hwjvol
  7. VOLUME ["vdata1","hwjvol"]
  8. LABEL version="12" author=hwj description="This is hwj's nginx"
  9. RUN yum install -y wget
  10. RUN wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
  11. RUN yum clean all&&yum makecache
  12. RUN yum install -y nginx
  13. RUN echo "daemon off;" >> /etc/nginx/nginx.conf
  14. ADD index.html.tar /usr/share/nginx/html/index.html
  15. EXPOSE 80 443
  16. CMD ["/usr/sbin/nginx"]
  1. [root@localhost httpd]# docker build -t hwj_ng:12 -f /root/httpd/Dockerfile .
  2. Sending build context to Docker daemon 4.096kB
  3. Step 1/13 : ARG version=7
  4. Step 2/13 : FROM centos:${version}
  5. ---> eeb6ee3f44bd
  6. Step 3/13 : MAINTAINER HWJ
  7. ---> Running in 0c3164b512b5
  8. Removing intermediate container 0c3164b512b5
  9. ---> 5f923fd3c365
  10. Step 4/13 : RUN mkdir /hwjvol
  11. ---> Running in 381cdfc7c89f
  12. Removing intermediate container 381cdfc7c89f
  13. ---> f2b8dcb77635
  14. Step 5/13 : VOLUME ["vdata1","hwjvol"]
  15. ---> Running in 54a6373b9f44
  16. Removing intermediate container 54a6373b9f44
  17. ---> 266371f1aa24
  18. Step 6/13 : LABEL version="12" author=hwj description="This is hwj's nginx"
  19. ---> Running in cfa53128198d
  20. Removing intermediate container cfa53128198d
  21. ---> d6dc212c1f18
  22. Step 7/13 : RUN yum install -y wget
  23. ---> Running in bf4d940bf763
  24. Loaded plugins: fastestmirror, ovl
  25. Determining fastest mirrors
  26. * base: mirrors.huaweicloud.com
  27. * extras: mirrors.huaweicloud.com
  28. * updates: mirrors.ustc.edu.cn
  29. Resolving Dependencies
  30. --> Running transaction check
  31. ---> Package wget.x86_64 0:1.14-18.el7_6.1 will be installed
  32. --> Finished Dependency Resolution
  33. Dependencies Resolved
  34. ================================================================================
  35. Package Arch Version Repository Size
  36. ================================================================================
  37. Installing:
  38. wget x86_64 1.14-18.el7_6.1 base 547 k
  39. Transaction Summary
  40. ================================================================================
  41. Install 1 Package
  42. Total download size: 547 k
  43. Installed size: 2.0 M
  44. Downloading packages:
  45. warning: /var/cache/yum/x86_64/7/base/packages/wget-1.14-18.el7_6.1.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID f4a80eb5: NOKEY
  46. Public key for wget-1.14-18.el7_6.1.x86_64.rpm is not installed
  47. Retrieving key from file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
  48. Importing GPG key 0xF4A80EB5:
  49. Userid : "CentOS-7 Key (CentOS 7 Official Signing Key) <security@centos.org>"
  50. Fingerprint: 6341 ab27 53d7 8a78 a7c2 7bb1 24c6 a8a7 f4a8 0eb5
  51. Package : centos-release-7-9.2009.0.el7.centos.x86_64 (@CentOS)
  52. From : /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
  53. Running transaction check
  54. Running transaction test
  55. Transaction test succeeded
  56. Running transaction
  57. Installing : wget-1.14-18.el7_6.1.x86_64 1/1
  58. install-info: No such file or directory for /usr/share/info/wget.info.gz
  59. Verifying : wget-1.14-18.el7_6.1.x86_64 1/1
  60. Installed:
  61. wget.x86_64 0:1.14-18.el7_6.1
  62. Complete!
  63. Removing intermediate container bf4d940bf763
  64. ---> 2aa125d5d77b
  65. Step 8/13 : RUN wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
  66. ---> Running in f51f912e28ee
  67. --2022-06-09 06:42:30-- http://mirrors.aliyun.com/repo/epel-7.repo
  68. Resolving mirrors.aliyun.com (mirrors.aliyun.com)... 117.167.122.240, 117.167.122.1, 117.167.122.241, ...
  69. Connecting to mirrors.aliyun.com (mirrors.aliyun.com)|117.167.122.240|:80... connected.
  70. HTTP request sent, awaiting response... 200 OK
  71. Length: 664 [application/octet-stream]
  72. Saving to: '/etc/yum.repos.d/epel.repo'
  73. 0K 100% 323M=0s
  74. 2022-06-09 06:42:30 (323 MB/s) - '/etc/yum.repos.d/epel.repo' saved [664/664]
  75. Removing intermediate container f51f912e28ee
  76. ---> adfd63435aa0
  77. Step 9/13 : RUN yum install -y nginx
  78. ---> Running in 7cd959c04e31
  79. Loaded plugins: fastestmirror, ovl
  80. Loading mirror speeds from cached hostfile
  81. * base: mirrors.huaweicloud.com
  82. * extras: mirrors.huaweicloud.com
  83. * updates: mirrors.ustc.edu.cn
  84. Resolving Dependencies
  85. --> Running transaction check
  86. ---> Package nginx.x86_64 1:1.20.1-9.el7 will be installed
  87. --> Processing Dependency: nginx-filesystem = 1:1.20.1-9.el7 for package: 1:nginx-1.20.1-9.el7.x86_64
  88. --> Processing Dependency: libcrypto.so.1.1(OPENSSL_1_1_0)(64bit) for package: 1:nginx-1.20.1-9.el7.x86_64
  89. --> Processing Dependency: libssl.so.1.1(OPENSSL_1_1_0)(64bit) for package: 1:nginx-1.20.1-9.el7.x86_64
  90. --> Processing Dependency: libssl.so.1.1(OPENSSL_1_1_1)(64bit) for package: 1:nginx-1.20.1-9.el7.x86_64
  91. --> Processing Dependency: nginx-filesystem for package: 1:nginx-1.20.1-9.el7.x86_64
  92. --> Processing Dependency: openssl for package: 1:nginx-1.20.1-9.el7.x86_64
  93. --> Processing Dependency: redhat-indexhtml for package: 1:nginx-1.20.1-9.el7.x86_64
  94. --> Processing Dependency: system-logos for package: 1:nginx-1.20.1-9.el7.x86_64
  95. --> Processing Dependency: libcrypto.so.1.1()(64bit) for package: 1:nginx-1.20.1-9.el7.x86_64
  96. --> Processing Dependency: libprofiler.so.0()(64bit) for package: 1:nginx-1.20.1-9.el7.x86_64
  97. --> Processing Dependency: libssl.so.1.1()(64bit) for package: 1:nginx-1.20.1-9.el7.x86_64
  98. --> Running transaction check
  99. ---> Package centos-indexhtml.noarch 0:7-9.el7.centos will be installed
  100. ---> Package centos-logos.noarch 0:70.0.6-3.el7.centos will be installed
  101. ---> Package gperftools-libs.x86_64 0:2.6.1-1.el7 will be installed
  102. ---> Package nginx-filesystem.noarch 1:1.20.1-9.el7 will be installed
  103. ---> Package openssl.x86_64 1:1.0.2k-25.el7_9 will be installed
  104. --> Processing Dependency: openssl-libs(x86-64) = 1:1.0.2k-25.el7_9 for package: 1:openssl-1.0.2k-25.el7_9.x86_64
  105. --> Processing Dependency: make for package: 1:openssl-1.0.2k-25.el7_9.x86_64
  106. ---> Package openssl11-libs.x86_64 1:1.1.1k-3.el7 will be installed
  107. --> Running transaction check
  108. ---> Package make.x86_64 1:3.82-24.el7 will be installed
  109. ---> Package openssl-libs.x86_64 1:1.0.2k-19.el7 will be updated
  110. ---> Package openssl-libs.x86_64 1:1.0.2k-25.el7_9 will be an update
  111. --> Finished Dependency Resolution
  112. Dependencies Resolved
  113. ================================================================================
  114. Package Arch Version Repository Size
  115. ================================================================================
  116. Installing:
  117. nginx x86_64 1:1.20.1-9.el7 epel 587 k
  118. Installing for dependencies:
  119. centos-indexhtml noarch 7-9.el7.centos base 92 k
  120. centos-logos noarch 70.0.6-3.el7.centos base 21 M
  121. gperftools-libs x86_64 2.6.1-1.el7 base 272 k
  122. make x86_64 1:3.82-24.el7 base 421 k
  123. nginx-filesystem noarch 1:1.20.1-9.el7 epel 24 k
  124. openssl x86_64 1:1.0.2k-25.el7_9 updates 494 k
  125. openssl11-libs x86_64 1:1.1.1k-3.el7 epel 1.5 M
  126. Updating for dependencies:
  127. openssl-libs x86_64 1:1.0.2k-25.el7_9 updates 1.2 M
  128. Transaction Summary
  129. ================================================================================
  130. Install 1 Package (+7 Dependent packages)
  131. Upgrade ( 1 Dependent package)
  132. Total download size: 26 M
  133. Downloading packages:
  134. Delta RPMs disabled because /usr/bin/applydeltarpm not installed.
  135. ^C
  136. [root@localhost httpd]# vim Dockerfile
  137. [root@localhost httpd]# docker build -t hwj_ng:12 -f /root/httpd/Dockerfile .
  138. Sending build context to Docker daemon 4.096kB
  139. Step 1/14 : ARG version=7
  140. Step 2/14 : FROM centos:${version}
  141. ---> eeb6ee3f44bd
  142. Step 3/14 : MAINTAINER HWJ
  143. ---> Using cache
  144. ---> 5f923fd3c365
  145. Step 4/14 : RUN mkdir /hwjvol
  146. ---> Using cache
  147. ---> f2b8dcb77635
  148. Step 5/14 : VOLUME ["vdata1","hwjvol"]
  149. ---> Using cache
  150. ---> 266371f1aa24
  151. Step 6/14 : LABEL version="12" author=hwj description="This is hwj's nginx"
  152. ---> Using cache
  153. ---> d6dc212c1f18
  154. Step 7/14 : RUN yum install -y wget
  155. ---> Using cache
  156. ---> 2aa125d5d77b
  157. Step 8/14 : RUN wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
  158. ---> Using cache
  159. ---> adfd63435aa0
  160. Step 9/14 : RUN yum clean all&&yum makecache
  161. ---> Running in 9e48660d4870
  162. Loaded plugins: fastestmirror, ovl
  163. Cleaning repos: base epel extras updates
  164. Cleaning up list of fastest mirrors
  165. Loaded plugins: fastestmirror, ovl
  166. Determining fastest mirrors
  167. * base: mirrors.dgut.edu.cn
  168. * extras: ftp.sjtu.edu.cn
  169. * updates: mirrors.ustc.edu.cn
  170. Metadata Cache Created
  171. Removing intermediate container 9e48660d4870
  172. ---> e8169943f047
  173. Step 10/14 : RUN yum install -y nginx
  174. ---> Running in f31c6dda10ad
  175. Loaded plugins: fastestmirror, ovl
  176. Loading mirror speeds from cached hostfile
  177. * base: mirrors.dgut.edu.cn
  178. * extras: ftp.sjtu.edu.cn
  179. * updates: mirrors.ustc.edu.cn
  180. Resolving Dependencies
  181. --> Running transaction check
  182. ---> Package nginx.x86_64 1:1.20.1-9.el7 will be installed
  183. --> Processing Dependency: nginx-filesystem = 1:1.20.1-9.el7 for package: 1:nginx-1.20.1-9.el7.x86_64
  184. --> Processing Dependency: libcrypto.so.1.1(OPENSSL_1_1_0)(64bit) for package: 1:nginx-1.20.1-9.el7.x86_64
  185. --> Processing Dependency: libssl.so.1.1(OPENSSL_1_1_0)(64bit) for package: 1:nginx-1.20.1-9.el7.x86_64
  186. --> Processing Dependency: libssl.so.1.1(OPENSSL_1_1_1)(64bit) for package: 1:nginx-1.20.1-9.el7.x86_64
  187. --> Processing Dependency: nginx-filesystem for package: 1:nginx-1.20.1-9.el7.x86_64
  188. --> Processing Dependency: openssl for package: 1:nginx-1.20.1-9.el7.x86_64
  189. --> Processing Dependency: redhat-indexhtml for package: 1:nginx-1.20.1-9.el7.x86_64
  190. --> Processing Dependency: system-logos for package: 1:nginx-1.20.1-9.el7.x86_64
  191. --> Processing Dependency: libcrypto.so.1.1()(64bit) for package: 1:nginx-1.20.1-9.el7.x86_64
  192. --> Processing Dependency: libprofiler.so.0()(64bit) for package: 1:nginx-1.20.1-9.el7.x86_64
  193. --> Processing Dependency: libssl.so.1.1()(64bit) for package: 1:nginx-1.20.1-9.el7.x86_64
  194. --> Running transaction check
  195. ---> Package centos-indexhtml.noarch 0:7-9.el7.centos will be installed
  196. ---> Package centos-logos.noarch 0:70.0.6-3.el7.centos will be installed
  197. ---> Package gperftools-libs.x86_64 0:2.6.1-1.el7 will be installed
  198. ---> Package nginx-filesystem.noarch 1:1.20.1-9.el7 will be installed
  199. ---> Package openssl.x86_64 1:1.0.2k-25.el7_9 will be installed
  200. --> Processing Dependency: openssl-libs(x86-64) = 1:1.0.2k-25.el7_9 for package: 1:openssl-1.0.2k-25.el7_9.x86_64
  201. --> Processing Dependency: make for package: 1:openssl-1.0.2k-25.el7_9.x86_64
  202. ---> Package openssl11-libs.x86_64 1:1.1.1k-3.el7 will be installed
  203. --> Running transaction check
  204. ---> Package make.x86_64 1:3.82-24.el7 will be installed
  205. ---> Package openssl-libs.x86_64 1:1.0.2k-19.el7 will be updated
  206. ---> Package openssl-libs.x86_64 1:1.0.2k-25.el7_9 will be an update
  207. --> Finished Dependency Resolution
  208. Dependencies Resolved
  209. ================================================================================
  210. Package Arch Version Repository Size
  211. ================================================================================
  212. Installing:
  213. nginx x86_64 1:1.20.1-9.el7 epel 587 k
  214. Installing for dependencies:
  215. centos-indexhtml noarch 7-9.el7.centos base 92 k
  216. centos-logos noarch 70.0.6-3.el7.centos base 21 M
  217. gperftools-libs x86_64 2.6.1-1.el7 base 272 k
  218. make x86_64 1:3.82-24.el7 base 421 k
  219. nginx-filesystem noarch 1:1.20.1-9.el7 epel 24 k
  220. openssl x86_64 1:1.0.2k-25.el7_9 updates 494 k
  221. openssl11-libs x86_64 1:1.1.1k-3.el7 epel 1.5 M
  222. Updating for dependencies:
  223. openssl-libs x86_64 1:1.0.2k-25.el7_9 updates 1.2 M
  224. Transaction Summary
  225. ================================================================================
  226. Install 1 Package (+7 Dependent packages)
  227. Upgrade ( 1 Dependent package)
  228. Total download size: 26 M
  229. Downloading packages:
  230. Delta RPMs disabled because /usr/bin/applydeltarpm not installed.
  231. Message from syslogd@localhost at Jun 9 14:53:49 ...
  232. kernel:unregister_netdevice: waiting for lo to become free. Usage count = 1
  233. --------------------------------------------------------------------------------
  234. Total 563 kB/s | 26 MB 00:46
  235. Running transaction check
  236. Running transaction test
  237. Transaction test succeeded
  238. Running transaction
  239. Installing : centos-logos-70.0.6-3.el7.centos.noarch 1/10
  240. Installing : centos-indexhtml-7-9.el7.centos.noarch 2/10
  241. Installing : 1:openssl11-libs-1.1.1k-3.el7.x86_64 3/10
  242. Installing : 1:make-3.82-24.el7.x86_64 4/10
  243. Installing : gperftools-libs-2.6.1-1.el7.x86_64 5/10
  244. Updating : 1:openssl-libs-1.0.2k-25.el7_9.x86_64 6/10
  245. Installing : 1:openssl-1.0.2k-25.el7_9.x86_64 7/10
  246. Installing : 1:nginx-filesystem-1.20.1-9.el7.noarch 8/10
  247. Installing : 1:nginx-1.20.1-9.el7.x86_64 9/10
  248. Cleanup : 1:openssl-libs-1.0.2k-19.el7.x86_64 10/10
  249. Verifying : 1:nginx-filesystem-1.20.1-9.el7.noarch 1/10
  250. Verifying : 1:nginx-1.20.1-9.el7.x86_64 2/10
  251. Verifying : 1:openssl-libs-1.0.2k-25.el7_9.x86_64 3/10
  252. Verifying : 1:openssl-1.0.2k-25.el7_9.x86_64 4/10
  253. Verifying : gperftools-libs-2.6.1-1.el7.x86_64 5/10
  254. Verifying : 1:make-3.82-24.el7.x86_64 6/10
  255. Verifying : 1:openssl11-libs-1.1.1k-3.el7.x86_64 7/10
  256. Verifying : centos-indexhtml-7-9.el7.centos.noarch 8/10
  257. Verifying : centos-logos-70.0.6-3.el7.centos.noarch 9/10
  258. Verifying : 1:openssl-libs-1.0.2k-19.el7.x86_64 10/10
  259. Installed:
  260. nginx.x86_64 1:1.20.1-9.el7
  261. Dependency Installed:
  262. centos-indexhtml.noarch 0:7-9.el7.centos
  263. centos-logos.noarch 0:70.0.6-3.el7.centos
  264. gperftools-libs.x86_64 0:2.6.1-1.el7
  265. make.x86_64 1:3.82-24.el7
  266. nginx-filesystem.noarch 1:1.20.1-9.el7
  267. openssl.x86_64 1:1.0.2k-25.el7_9
  268. openssl11-libs.x86_64 1:1.1.1k-3.el7
  269. Dependency Updated:
  270. openssl-libs.x86_64 1:1.0.2k-25.el7_9
  271. Complete!
  272. Removing intermediate container f31c6dda10ad
  273. ---> 0a73e810765b
  274. Step 11/14 : RUN echo "daemon off;" >> /etc/nginx/nginx.conf
  275. ---> Running in 910d281b823f
  276. Removing intermediate container 910d281b823f
  277. ---> 3d6c5271e013
  278. Step 12/14 : ADD index.html.tar /usr/share/nginx/html/index.html
  279. ---> 6718e5b433de
  280. Step 13/14 : EXPOSE 80 443
  281. ---> Running in c986cab88516
  282. Removing intermediate container c986cab88516
  283. ---> 5e7325370764
  284. Step 14/14 : CMD ["/usr/sbin/nginx"]
  285. ---> Running in 57e755be5d93
  286. Removing intermediate container 57e755be5d93
  287. ---> 9a383288af3b
  288. Successfully built 9a383288af3b
  289. Successfully tagged hwj_ng:12

3.3.3 运行镜像

[root@localhost httpd]# docker run -d -p 1280:80  --name hwj_ng 9a3
3cb0dd0032bb399fdc393377fb987eb59e8e611b3b7882e7a1e59687c95b1994
[root@manager httpd]# curl 192.168.20.200:80
<html>
<head><title>301 Moved Permanently</title></head>
<body>
<center><h1>301 Moved Permanently</h1></center>
<hr><center>nginx/1.20.1</center>
</body>
</html>

image.png

3.4 Docker Compose搭建使用(以minio文件服务器为例)

3.4.1 安装Docker compose

docker-compose.rar
下载解压后移动为/usr/local/bin/docker-compose

[root@localhost ~]# chmod +x /usr/local/bin/docker-compose
[root@localhost ~]# ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose
[root@localhost ~]# docker-compose --version
docker-compose version 1.29.2, build 5becea4c

3.4.2 创建一个空的项目目录并切换到该项目目录

[root@localhost ~]# mkdir my_minio&&cd my_minio

3.4.3 准备所需镜像并上传本地仓库

[root@manager my_minio]# docker pull minio/minio:RELEASE.2021-06-17T00-10-46Z 
Using default tag: latest
latest: Pulling from minio/minio
d46336f50433: Pull complete 
be961ec68663: Pull complete 
44173c602141: Pull complete 
a9809a6a679b: Pull complete 
df29d4a76971: Pull complete 
2b5a8853d302: Pull complete 
84f01ee8dfc1: Pull complete 
Digest: sha256:d786220feef7d8fe0239d41b5d74501dc824f6e7dd0e5a05749c502fff225bf3
Status: Downloaded newer image for minio/minio:latest
docker.io/minio/minio:latest
[root@manager my_minio]# docker pull postgres:10.0
10.0: Pulling from library/postgres
3e17c6eae66c: Pull complete 
edb9dc1cfcb8: Pull complete 
7a344e5b3663: Pull complete 
d619f9c5def6: Pull complete 
225b82de1e67: Pull complete 
10daa23c1d9a: Pull complete 
4ba4a4b2d69b: Pull complete 
3fc3e282a06c: Pull complete 
db8508199c1a: Pull complete 
99445470b9b6: Pull complete 
dd01bf2076d4: Pull complete 
292c6e56e62f: Pull complete 
7a07813393c7: Pull complete 
Digest: sha256:95e54755cb3a59a56dc2d98d8e8a0b4436537185c7b240eb140069208ec35e22
Status: Downloaded newer image for postgres:10.0
docker.io/library/postgres:10.0
[root@manager my_minio]# docker tag 486 manager:12500/postgre:10.0
[root@manager my_minio]# docker push  manager:12500/postgre:10.0
The push refers to repository [manager:12500/postgre]
fd5630d46a12: Pushed 
a7081ac6936d: Pushed 
f61a0d61c68d: Pushed 
5b40c79cc039: Pushed 
df8b904bd3d8: Pushed 
9b49ef35a744: Pushed 
61e70493df47: Pushed 
b52d45345a58: Pushed 
87838a6465f4: Pushed 
d82ca2d81d59: Pushed 
c5af23d9ec6a: Pushed 
154fff363662: Pushed 
a75caa09eb1f: Pushed 
10.0: digest: sha256:061cfd1ede70e0536bc2ca69cd3dcaf0baa32e7211089b32782ba96ab7465fa1 size: 3033
[root@manager my_minio]# docker pull minio/mc
Using default tag: latest
latest: Pulling from minio/mc
d46336f50433: Already exists 
be961ec68663: Already exists 
61a5592daf49: Pull complete 
dfba010b74b1: Pull complete 
b60995d41f27: Downloading [=========>                                         ]  13.12MB/70.73MB
latest: Pulling from minio/mc
54e56e6f8572: Pull complete 
4f8ddd7f5a75: Pull complete 
09b72602014c: Pull complete 
b38e8de6b5d9: Pull complete 
c3b3b83f5fc9: Pull complete 
Digest: sha256:76f22dba106471fda3c2377d6b537587b1bf5ba942d88f2e068307708203987e
Status: Downloaded newer image for minio/mc:latest
docker.io/minio/mc:latest
[root@manager my_minio]# docker tag c73 manager:12500/minio/mc:latest
[root@manager my_minio]# docker push manager:12500/minio/mc:latest
The push refers to repository [manager:12500/minio/mc]
a78878a2432b: Mounted from minio/mc/latest 
80c9e3f0e4b9: Mounted from minio/mc/latest 
c09fb7c93f16: Mounted from minio/mc/latest 
f1ccf0afcb5e: Mounted from minio/mc/latest 
235cb1df51fd: Mounted from minio/mc/latest 
latest: digest: sha256:817b423528c92044b3647ee7e5ab9330cc5675705c8bb534626789c37aae03d4 size: 1367
[root@manager my_minio]# docker tag c73 manager:12500/minio/mc/latest
[root@manager my_minio]# docker push manager:12500/minio/mc/latest
Using default tag: latest
The push refers to repository [manager:12500/minio/mc/latest]
a78878a2432b: Pushed 
80c9e3f0e4b9: Pushed 
c09fb7c93f16: Pushed 
f1ccf0afcb5e: Pushed 
235cb1df51fd: Pushed 
latest: digest: sha256:817b423528c92044b3647ee7e5ab9330cc5675705c8bb534626789c37aae03d4 size: 1367
[root@manager my_minio]# docker pull elasticsearch:7.6.0
7.6.0: Pulling from library/elasticsearch
ab5ef0e58194: Pull complete 
c7bddb370e64: Pull complete 
b477f7f946d3: Pull complete 
5a7d0cebbd37: Pull complete 
72685965d8ec: Pull complete 
9fdbb1d5d01e: Pull complete 
f96afe9ae49e: Pull complete 
Digest: sha256:578266a8f2de6e1a6896c487dd45cfc901a82ddf50be13bb7c56f97ecd693f77
Status: Downloaded newer image for elasticsearch:7.6.0
docker.io/library/elasticsearch:7.6.0
[root@manager my_minio]# docker tag 5d2 manager:12500/elasticsearch:7.6.0
[root@manager my_minio]# docker push  manager:12500/elasticsearch:7.6.0
The push refers to repository [manager:12500/elasticsearch]
a18779c7c0ef: Pushed 
29cf507845ab: Pushed 
97e12b10a622: Pushed 
d261ef39581a: Pushed 
e67ea6265347: Pushed 
05efe25e0619: Pushed 
77b174a6a187: Pushed 
7.6.0: digest: sha256:80326aed1c82da6aa25d333e934553dc2f01e3573fe21798e5a5966125d407bb size: 1785

3.4.4 创建Docker-compose文件

[root@manager my_minio]# vim docker-compose.yml 
[root@manager my_minio]# cat docker-compose.yml 
version: "3.7"
services: 
  minio: 
    container_name: hwj_minio    #运行容器的名称
    image: minio/minio:RELEASE.2021-06-17T00-10-46Z 
    restart: always
    volumes: 
      - /opt/data/minio/data:/data
      - /etc/localtime:/etc/localtime
    ports:
      - 12900:9000
    environment: 
      MINIO_ACCESS_KEY: name         #minio的账户名称
      MINIO_SECRET_KEY: password     #minio的账户密码
    command: server /data       
    networks: 
      - hwj_net
  minio_init: 
    container_name: hwj_minio_init
    image: manager:12500/minio/mc:latest
    entrypoint:                     #运行位置
      - /bin/bash
      - -c
    depends_on:                     #依赖关系
      - minio                   
    networks: 
      - hwj_net
  db:
    container_name: hwj_db
    image: manager:12500/postgre:10.0
    restart: always
    environment:                    #sql数据库环境
      - POSTGRES_DB=minio
      - POSTGRES_USER=minio
      - POSTGRES_PASSWORD=P@ssw0rd
      - PGDATA=/var/lib/postgresql/data
    volumes: 
      - /opt/data/postgresql/data:/var/lib/postgresql/data
    ports:
      - "5432:5432"
    networks: 
      - hwj_net
  elasticsearch: 
    container_name: hwj_elastic
    image: manager:12500/elasticsearch:7.6.0
    restart: always
    ports: 
      - "9300:9300"
      - "9200:9200"
    environment: 
      - discovery.type=single-node      #发现方式
    volumes:
      - /opt/data/elasticsearch/data:/usr/share/elasticsearch/data
    networks: 
      - hwj_net
networks:
  hwj_net:
    name: hwj_net
    driver: bridge

3.4.5 运行docker-compose

[root@manager my_minio]# vim docker-compose.yml 
[root@manager my_minio]# docker-compose up -d
Creating network "hwj_net" with driver "bridge"
Creating hwj_db      ... done
Creating hwj_elastic ... done
Creating hwj_minio   ... done
Creating hwj_minio_init ... done

image.png
image.png
minio是一种专注于对象存储的软件,可以兼容亚马逊的s3接口,同时内置了webserver,运行主程序之后可以直接的使用网页进行连接。
image.png
通过左下角可以创建文件夹和上传文件,也可以通过安装的minio/mc客户端管理工具管理
image.png

3.4.6 mc客户端工具和下载使用方法(非友情连接)

https://blog.csdn.net/qq_42402854/article/details/121474018?ops_request_misc=&request_id=&biz_id=102&spm=1018.2226.3001.4187

3.5 搭建docker swarm集群

3.5.1 准备三台服务器

主机 操作系统 IP地址 主要软件
manager centos 7 192.168.20.200 Docker
worker01 centos 7 192.168.20.201 Docker
worker02 centos 7 192.168.20.202 Docker

3.5.2 关闭三台主机的防火墙及SELINUX,设置系统主机名以及 Host 文件的相互解析

这里以manager为例

[root@manager ~]# vim /etc/hosts
[root@manager ~]# cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.20.200  manager 
192.168.20.201 worker01
192.168.20.202 worker02

3.5.3 在manager节点执行

root@manager ~]# docker swarm init --advertise-addr 192.168.20.200
Swarm initialized: current node (bdbpqpnsphmhh3tbeum6u7jb4) is now a manager.

To add a worker to this swarm, run the following command:

    docker swarm join --token SWMTKN-1-10qmh43baxo5qp2wm6ye0kwun6g55n2hezi3p3qbd6hyytoclw-82ew9wteyw469bnrxutv9ubd7 192.168.20.200:2377

To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions.

3.5.4 在工作节点执行上面的加入指令

[root@worker01 ~]# docker swarm join --token SWMTKN-1-10qmh43baxo5qp2wm6ye0kwun6g55n2hezi3p3qbd6hyytoclw-82ew9wteyw469bnrxutv9ubd7 192.168.20.200:2377
This node joined a swarm as a worker.

3.5.5 查看集群中的节点

[root@manager ~]# docker node ls
ID                            HOSTNAME   STATUS    AVAILABILITY   MANAGER STATUS   ENGINE VERSION
bdbpqpnsphmhh3tbeum6u7jb4 *   manager    Ready     Active         Leader           20.10.12
q64qn6q4klsdthd0pffoh1s1a     worker01   Ready     Active                          20.10.12
291f7yfi6q9889549flh6e9sp     worker02   Ready     Active                          20.10.12

3.6 使用Docker Stack 部署(home-assistant)

3.6.1 拉取homeassistant/home-assistant和mysql的镜像

[root@manager ~]# docker pull homeassistan/home-assistant
[root@manager ~]# docker pull mysql

3.6.2 将镜像全部上传本地仓库

[root@manager ~]# docker tag 773 manager:12500/homeassisant/home-assistant:latest
[root@manager ~]# docker push manager:12500/homeassisant/home-assistant:latest
The push refers to repository [manager:12500/homeassisant/home-assistant]
e5b1dd04b331: Pushed 
ec542c62c153: Pushed 
1d13107313ed: Pushed 
77dc48b91677: Pushed 
be2828812449: Pushed 
9d2650ca7417: Pushed 
0e51ffe3acc2: Pushed 
140f8bec1872: Pushed 
de7afb379aa0: Pushed 
34a28ce4412c: Pushed 
d7b04a5cbce5: Pushed 
724086462346: Pushed 
ef173cabde8b: Pushed 
42322d697e88: Pushed 
d0d5bda440ff: Pushed 
969767cea71d: Pushed 
17e0deeb402c: Pushed 
5ecc7efaace6: Pushed 
f532417221f4: Pushed 
5b86a8d70922: Pushed 
20a92118600a: Pushed 
b2f43f6b25ef: Pushed 
eb83beb5f4c2: Pushed 
029d51f4d389: Pushed 
42d768421933: Pushed 
7b02b55000a2: Pushed 
92e486a42e39: Pushed 
e2eb06d8af82: Pushed 
latest: digest: sha256:3af366423713a6144adf9081b1ede2c21d8511c0048ac4c04615da26a5caf8d6 size: 6190
[root@manager ~]# docker tag 321 manager:12500/mysql:latest
[root@manager ~]# docker push  manager:12500/mysql:latest

3.6.3 编辑yml文件

[root@manager ~]# mkdir home.app&&cd home.app
[root@manager home.app]# vim home.yml
[root@manager home.app]# cat home.yml
version: "3.2"

services: 
  home: 
    image: manager:12500/homeassisant/home-assistant
    ports:
      - "8123:8123"
    networks: 
      - hwj_net
    depends_on: 
      - database
    volumes: 
      - config:/config
      - localtime:/etc/localtime:ro
    deploy: 
      mode: replicated   #复制3台 
      replicas: 3       
      restart_policy: 
        condition: on-failure
        delay: 5s
        max_attempts: 3     #环境是失败后延迟五秒 最多尝试3次
      update_config: 
        parallelism: 1
        delay: 10s
  database: 
    image: manager:12500/mysql:latest
    networks: 
      - hwj_net
    volumes: 
      - mysql_data:/var/lib/mysql
    environment: 
      MYSQL_ROOT_PASSWORD: homeassisant
      MYSQL_DATABASE: homeassisant
      MYSQL_USER: homeassisant
      MYSQL_PASSWORD: homeassisant
    deploy: 
      mode: global
      placement: 
        constraints: 
          - node.role == manager
networks: 
  hwj_net: 
    driver: overlay
volumes: 
  mysql_data: 
  config: 
  localtime:

3.6.4 通过docker stack部署服务

[root@manager home.app]# docker stack deploy -c home.yml hwj_home
Creating network hwj_home_hwj_net
Creating service hwj_home_home
Creating service hwj_home_database

3.6.5 查看服务

[root@manager home.app]# docker stack ps hwj_home
ID             NAME                                          IMAGE                                              NODE       DESIRED STATE   CURRENT STATE           ERROR     PORTS
oksd01bdyu86   hwj_home_database.bdbpqpnsphmhh3tbeum6u7jb4   manager:12500/mysql:latest                         manager    Running         Running 4 minutes ago             
y4bckix1dd9r   hwj_home_home.1                               manager:12500/homeassisant/home-assistant:latest   worker02   Running         Running 4 minutes ago             
vfjepib2bqc3   hwj_home_home.2                               manager:12500/homeassisant/home-assistant:latest   manager    Running         Running 4 minutes ago             
789lyiblqbhz   hwj_home_home.3                               manager:12500/homeassisant/home-assistant:latest   worker01   Running         Running 4 minutes ago

浏览器输入http://192.168.20.200:8123,可以访问居家系统
image.pngimage.pngimage.pngimage.png


四、 项目总结

通过本次项目实践,我掌握了Docker安装与基本使用,本地镜像仓库的搭建使用,Docker Compose安装与使用,DockerSWARM集群构建,熟练使用Docker stack,对这一轻量型的docker操作有了更深的见解,通过搭建minio的compose和homrassistant的集群项目,使我对“精益求精”有更深的认识,做到仔细列出拓扑规划,有条理的完成每一步过程。