一,下载centos的image
1,下载centos最新image
[root@localhost ~]# docker pull centos
2,查看是否成功下载到本地image
[root@localhost ~]# docker images | grep centos centos latest 470671670cac 7 weeks ago 237MB
说明:刘宏缔的架构森林是一个专注架构的博客,地址:https://www.cnblogs.com/architectforest
对应的源码可以访问这里获取: https://github.com/liuhongdi/
说明:作者:刘宏缔 邮箱: 371125307@qq.com
二,从docker运行centos8
1,宿主机上创建目录,供docker运行后的容器挂载
[root@localhost liuhongdi]# mkdir /data/nfs [root@localhost liuhongdi]# mkdir /data/nfs/nfs1
2,运行centos这个image
[root@localhost ~]# docker run --name nfs -v /data/nfs/nfs1:/data/nfs/nfs1 --privileged -d -i -t centos:latest /usr/sbin/init
2e6fa31c71936674bb048e663d0652e3b227db84df576e1cd1d103608ad2fe56
3,查看此容器是否运行成功
[root@localhost ~]# docker ps -a | grep centos d0fa7ffc318d centos:latest "/usr/sbin/init" 16 seconds ago Up 15 seconds
4,登录到容器
[root@localhost ~]# docker exec -it nfs /bin/bash
[root@d0fa7ffc318d /]#
三,安装并启动nfs4
1,检查是否有安装nfs
[root@d0fa7ffc318d /]# systemctl status nfs-server
Unit nfs-server.service could not be found.
看来image很简单,自带的软件非常少
从yum安装
[root@d0fa7ffc318d /]# yum install nfs-utils
2,安装成功后,配置nfs
查看nfs的运行状态 :
[root@d0fa7ffc318d /]# systemctl start nfs-server
[root@d0fa7ffc318d /]# systemctl status nfs-server
3,检查nfs是否成功启动:查看端口:
[root@d0fa7ffc318d /]# netstat -anp 20048 mountd 2049 nfs 111 portmap
四,新加nginx账号,用来供nfs的访问使用
1,建立组
[root@d0fa7ffc318d /]# groupadd nginx [root@d0fa7ffc318d /]# groupmod -g 973 nginx [root@d0fa7ffc318d /]# grep nginx /etc/group nginx:x:973:
2,建立用户
[root@d0fa7ffc318d /]# useradd -g nginx -s /sbin/nologin -M nginx [root@d0fa7ffc318d /]# usermod -u 973 nginx [root@d0fa7ffc318d /]# grep nginx /etc/passwd nginx:x:973:973::/home/nginx:/sbin/nologin
五,nfs上配置export一个目录
1,配置export一个目录
[root@2e6fa31c7193 /]# vi /etc/exports
内容:
/data/nfs/nfs1 192.168.1.8(rw,sync,all_squash,anonuid=973,anongid=973) /data/nfs/nfs1 172.17.0.1(rw,sync,all_squash,anonuid=973,anongid=973)
2,使配置文件的修改生效:
[root@2e6fa31c7193 /]# exportfs -rv exporting 192.168.1.8:/data/nfs/nfs1 exporting 172.17.0.1:/data/nfs/nfs1
3,在客户端机器上检查nfs服务端输出的文件系统:
[root@localhost ~]# showmount -e 172.17.0.3 Export list for 172.17.0.3: /data/nfs/nfs1 172.17.0.1,192.168.1.8
4,在客户端上测试挂载
[root@localhost ~]# mount -t nfs 172.17.0.3:/data/nfs/nfs1 /data/mnt/nfs1
5,查看挂载是否成功:列出所有的nfs文件系统
[root@localhost ~]# df -hT | grep nfs 172.17.0.3:/data/nfs/nfs1 nfs 50G 24G 23G 51% /data/mnt/nfs1
六,安装nginx
1,安装nginx
[root@2e6fa31c7193 /]# yum install nginx
2,配置nginx
1,查看nginx.conf,找到默认的server的配置
[root@2e6fa31c7193 nginx]# more /etc/nginx/nginx.conf server { listen 80 default_server; listen [::]:80 default_server; server_name _; root /usr/share/nginx/html; # Load configuration files for the default server block. include /etc/nginx/default.d/*.conf; location / { } }
2,在root /usr/share/nginx/html; 这个目录下,给nfs建立符号链接
[root@2e6fa31c7193 html]# ln -s /data/nfs/nfs1 ./nfs
3,启动nginx
[root@2e6fa31c7193 html]# systemctl start nginx
4,测试nginx是否生效
从宿主机复制一张图片a1.jpg到/data/nfs/nfs1目录下,
然后访问这个地址进行测试:
http://172.17.0.3/nfs/a1.jpg
说明:172.17.0.3 是nfs服务所在容器的ip
七,commit一个新镜像
[root@localhost ~]# docker commit nfs nfsnginx:0.1 sha256:fa72df9fb74483e90335e99985d8c560d6b6376a66a666e42c3260f79e925691
八,查看新镜像是否生成
[root@localhost ~]# docker images | grep nfsnginx nfsnginx 0.1 fa72df9fb744 54 seconds ago 328MB
九,测试运行新镜像:
1,启动
[root@localhost ~]# docker run --name nfsnginx -v /data/nfs/nfs1:/data/nfs/nfs1 --privileged -d -i -t nfsnginx:0.1 /usr/sbin/init ce03496a89b0fcb4011d1c3b945449fabcc27c3438449ce856cbbdf3f2513acb
2,登录,并查看ip:
[root@localhost ~]# docker exec -it nfsnginx /bin/bash [root@ce03496a89b0 /]# ifconfig | grep inet inet 172.17.0.4 netmask 255.255.0.0 broadcast 172.17.255.255
3,启动服务:
[root@ce03496a89b0 /]# systemctl start nginx
[root@ce03496a89b0 /]# systemctl start nfs-server
十,查看docker的版本
[root@localhost source]# docker --version Docker version 19.03.7, build 7141c19