首先开启路由转发

[root@localhost ~]# vim /etc/sysctl.conf
net.ipv4.ip_forward = 1

[root@localhost web]# sysctl -p

1. 创建data volume映射冷数据index.html

创建web前端文件

[root@localhost ~]# mkdir -p web/html 
[root@localhost ~]# echo www.shuaiguoer.com > web/html/index.html

创建Dockerfile文件

[root@localhost ~]# cd web/
[root@localhost web]# vim Dockerfile

FROM busybox
ADD html /var/www/html
VOLUME /var/www/html

构建数据卷镜像

[root@localhost web]# docker build -t vc_data .

创建数据卷容器

[root@localhost web]# docker create --name data vc_data
9e50a7ef89d649ac5f73f73379b3dd68a750edee0a73d967ef786fda14e728c1

2. 热数据Apache页面持久化到本地

[root@localhost web]# cd

[root@localhost ~]# vim dockerfile
FROM centos
RUN yum -y install httpd httpd-tools net-tools zlib zlib-devel
EXPOSE 80
EXPOSE 443
CMD ["/usr/sbin/httpd","-D","FOREGROUND"]

构建http镜像

[root@localhost ~]# docker build -t http_centos .

运行两个http镜像,并指定映射数据卷

[root@localhost ~]# docker run -itd --name http1 --volumes-from data http_centos
923ccedabff8c9717796d081659b54aca0439bac2bf63c4a6328f74a23ddd06b

[root@localhost ~]# docker run -itd --name http2 --volumes-from data http_centos
32e7a660c1fc23799622271fa5aaa0b084226fc67bda17a0e2b97be10f6c3907

3. http镜像可以使用ssh进行远程管理

安装ssh所需服务

yum -y install passwd openssl openssh-server openssh-clients

启动sshd

[root@4762a1cdd5f9 /]# /usr/sbin/sshd -D &
这时报以下错误:
Could not load host key: /etc/ssh/ssh_host_rsa_key
Could not load host key: /etc/ssh/ssh_host_ecdsa_key
Could not load host key: /etc/ssh/ssh_host_ed25519_key
sshd: no hostkeys available -- exiting.
# 执行以下命令解决:
[root@4762a1cdd5f9 /]# ssh-keygen -q -t rsa -b 2048 -f /etc/ssh/ssh_host_rsa_key -N ''
[root@4762a1cdd5f9 /]# ssh-keygen -q -t ecdsa -f /etc/ssh/ssh_host_ecdsa_key -N ''
[root@4762a1cdd5f9 /]# ssh-keygen -t dsa -f /etc/ssh/ssh_host_ed25519_key  -N ''

修改 /etc/ssh/sshd_config 配置信息

[root@4762a1cdd5f9 /]# vi /etc/ssh/sshd_config 
  • UsePAM yes 改为 UsePAM no(直接注释掉!
  • UsePrivilegeSeparation sandbox 改为 UsePrivilegeSeparation no

设置root密码

[root@4762a1cdd5f9 /]# passwd root
输入两次确认密码

启动ssh服务

[root@4762a1cdd5f9 /]# /usr/sbin/sshd –D &

测试是否可以远程登录

[root@923ccedabff8 /]# ssh 172.17.0.4
root@172.17.0.4's password:
Last login: Wed May 15 05:31:56 2019 from 172.17.0.3

4. data volume和http镜像上传至私有仓库

搭建registry私有仓库

拉取registry镜像

[root@localhost web]# docker pull registry:2

修改docker.server配置文件,增加:

--insecure-registry 192.168.1.11:5000
[root@localhost web]# vim /usr/lib/systemd/system/docker.service


 重启docker

[root@localhost web]# systemctl daemon-reload 
[root@localhost web]# systemctl restart docker.service

运行registry私有仓库容器

[root@localhost web]# docker run -d --restart=always -p 5000:5000 -v /opt/data/registry:/tmp/registry registry:2
a3b4c36b3595ece049db7705b7f4dc2605ca6ddceadaae9384d717bdf1f75b52

修改标签并上传镜像到registry私有仓库

[root@localhost web]# docker tag vc_data:latest 192.168.1.11:5000/vc_data
[root@localhost web]# docker push 192.168.1.11:5000/vc_data

[root@localhost ~]# docker tag http_centos:latest 192.168.1.11:5000/http_centos
[root@localhost ~]# docker push 192.168.1.11:5000/http_centos

5. 启动容器,可访问httpd页面

[root@localhost ~]# curl 172.17.0.3
www.shuaiguoer.com

[root@localhost ~]# curl 172.17.0.4
www.shuaiguoer.com

扩展题

查看registry私有仓库中的镜像

[root@localhost web]# curl 192.168.1.11:5000/v2/_catalog
{"repositories":["http_centos","vc_data"]}

拉取镜像

[root@localhost ~]# docker pull 192.168.1.11:5000/http_centos
[root@localhost ~]# docker pull 192.168.1.11:5000/vc_data

查看镜像是否拉取成功

[root@localhost web]# docker images
REPOSITORY                      TAG                 IMAGE ID            CREATED             SIZE
192.168.1.11:5000/http_centos   latest              489663df371f        About an hour ago   323MB
192.168.1.11:5000/vc_data       latest              4c6f94c3e839        About an hour ago   1.2MB
busybox                         latest              64f5d945efcc        5 days ago          1.2MB
httpd                           latest              d4a07e6ce470        6 weeks ago         132MB
centos                          latest              9f38484d220f        2 months ago        202MB
Last modification:July 23rd, 2020 at 11:19 pm
如果觉得我的文章对你有用,请随意赞赏