Docker存储卷实战(nginx)

Docker存储卷实战(nginx)


创建nginx容器,同时提供配置文件和网页文件

//真机网站文件存放位置
[root@localhost ~]# cd /var/www/html/
[root@localhost html]# ls
Images  index.html
[root@localhost html]# 

//拉取nginx镜像
[root@localhost ~]# docker pull nginx
Using default tag: latest
latest: Pulling from library/nginx
e5ae68f74026: Pull complete 
21e0df283cd6: Pull complete 
ed835de16acd: Pull complete 
881ff011f1c9: Pull complete 
77700c52c969: Pull complete 
44be98c0fab6: Pull complete 
Digest: sha256:9522864dd661dcadfd9958f9e0de192a1fdda2c162a35668ab6ac42b465f0603
Status: Downloaded newer image for nginx:latest
docker.io/library/nginx:latest
[root@localhost ~]# docker images
REPOSITORY         TAG       IMAGE ID       CREATED        SIZE
pengyudong/httpd   latest    fc94b7069f41   15 hours ago   702MB
pengyudong/httpd   v1        b0f43183575f   36 hours ago   713MB
nginx              latest    f652ca386ed1   5 days ago     141MB
busybox            latest    d23834f29b38   8 days ago     1.24MB
httpd              latest    ad17c88403e2   2 weeks ago    143MB
centos             latest    5d0da3dc9764   2 months ago   231MB
[root@localhost ~]# 

//制作一个网站映射
[root@localhost ~]# docker run -it --name html -v /var/www/html:/usr/share/nginx/html busybox
/ # exit
[root@localhost ~]# docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
[root@localhost ~]# docker ps -a
CONTAINER ID   IMAGE                     COMMAND                  CREATED          STATUS                       PORTS                               NAMES
094a90e53cb8   busybox                   "sh"                     15 seconds ago   Exited (0) 9 seconds ago                                         html

//使用一个测试容器登入进去查看映射情况
[root@localhost ~]# docker run -it --volumes-from html busybox
/ # ls /usr/share/nginx/html/    //映射成功
Images      index.html
/ # 

//把测试的容器删除
[root@localhost ~]# docker rm -f 8e6cb7c394f8
8e6cb7c394f8
[root@localhost ~]# 

//创建nginx配置文件存放位置
[root@localhost ~]# mkdir /config
[root@localhost ~]# ls /config/
[root@localhost ~]# 

//提供nginx配置文件
root@localhost ~]# yum -y install nginx
[root@localhost ~]# cp -r /etc/nginx/* /config/
[root@localhost ~]# ls /config/
conf.d                fastcgi_params          mime.types          scgi_params           win-utf
default.d             fastcgi_params.default  mime.types.default  scgi_params.default
fastcgi.conf          koi-utf                 nginx.conf          uwsgi_params
fastcgi.conf.default  koi-win                 nginx.conf.default  uwsgi_params.default
[root@localhost ~]# 
[root@localhost ~]# yum -y remove nginx
//创建一个数据卷容器,数据来源于html容器,给配置文件做一个映射
[root@localhost ~]# docker run --name config --volumes-from html -v /config:/etc/nginx busybox

//做一个测试容器查看网站和配置文件映射情况
[root@localhost ~]# docker run -it --volumes-from config busybox
/ # ls /etc/nginx/   //配置文件映射成功
conf.d                  fastcgi_params.default  nginx.conf              uwsgi_params.default
default.d               koi-utf                 nginx.conf.default      win-utf
fastcgi.conf            koi-win                 scgi_params
fastcgi.conf.default    mime.types              scgi_params.default
fastcgi_params          mime.types.default      uwsgi_params
/ # ls /usr/share/nginx/html/   //配置文件映射成功
Images      index.html

//删除测试容器
[root@localhost ~]# docker rm -f wizardly_goldberg

//删除html容器,即使config容器数据来源于html容器,config里面的数据依旧不变
[root@localhost ~]# docker rm -f html 
html
[root@localhost ~]# docker run -it --rm --volumes-from config busybox
/ # ls /usr/share/nginx/html/
Images      index.html
/ # ls /etc/nginx/
conf.d                  fastcgi_params.default  nginx.conf              uwsgi_params.default
default.d               koi-utf                 nginx.conf.default      win-utf
fastcgi.conf            koi-win                 scgi_params
fastcgi.conf.default    mime.types              scgi_params.default
fastcgi_params          mime.types.default      uwsgi_params
/ # 
[root@localhost ~]# docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
[root@localhost ~]# docker ps -a
CONTAINER ID   IMAGE     COMMAND   CREATED        STATUS                    PORTS     NAMES
8237d7817211   busybox   "sh"      21 hours ago   Exited (0) 21 hours ago             config
[root@localhost ~]# 

//启动一个web容器
[root@localhost ~]# docker run -d -P --name web --volumes-from config nginx
6daf4db7177847de95c322ff4eae2d369e96d6434311efa8d01efe050fa5baea
[root@localhost ~]# docker ps
CONTAINER ID   IMAGE     COMMAND                  CREATED          STATUS          PORTS                                     NAMES
6daf4db71778   nginx     "/docker-entrypoint.…"   11 seconds ago   Up 10 seconds   0.0.0.0:49153->80/tcp, :::49153->80/tcp   web
[root@localhost ~]# 
[root@localhost ~]# docker port web 
80/tcp -> 0.0.0.0:49153
80/tcp -> :::49153
[root@localhost ~]# 

页面访问
在这里插入图片描述

//更新网站内容
[root@localhost ~]# cd /var/www/html/
[root@localhost html]# ls
Images  index.html
[root@localhost html]# mv index.html 1.html
[root@localhost html]# echo "hello world" > index.php
[root@localhost html]# mv index.php index.html
[root@localhost html]# cat index.html 
hello world
[root@localhost html]# 
[root@localhost html]# mv 1.html Images/ /opt/

页面访问
在这里插入图片描述

//制作第二个网站访问页面
[root@localhost html]# ls
index.html
[root@localhost html]# mkdir test
[root@localhost html]# echo "test page" > test/index.html
[root@localhost html]# sed -n '1p' test/index.html 
test page
[root@localhost html]# 

访问页面
在这里插入图片描述

//为了方便访问两个网站,整理一下目录
[root@localhost html]# ls
game  test
[root@localhost html]# ls game/
Images  index.html
[root@localhost html]# ls test/
index.html
[root@localhost html]# 

//使用真机修改配置文件,用两个端口号进行访问,映射到容器里面
[root@localhost ~]# cd /config/
[root@localhost config]# vim nginx.conf
[root@localhost config]# sed -n '35,58p' nginx.conf
    # for more information.
    include /etc/nginx/conf.d/*.conf;

        server {
        listen      8080;											
        server_name test.example.com;

        location / {
            root        /usr/share/nginx/html/test;	
            index       index.html;
        }
    }

    server {
        listen       80;
        server_name  game.example.com;

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        location / {
	    root	/usr/share/nginx/html/game;
	    index       index.html;
        }
[root@localhost config]# 
[root@localhost config]# docker ps -a
CONTAINER ID   IMAGE     COMMAND                  CREATED          STATUS                    PORTS                                     NAMES
6daf4db71778   nginx     "/docker-entrypoint.…"   48 minutes ago   Up 48 minutes             0.0.0.0:49153->80/tcp, :::49153->80/tcp   web
8237d7817211   busybox   "sh"                     22 hours ago     Exited (0) 22 hours ago                                             config
[root@localhost config]# docker rm -f web 
web
[root@localhost config]# docker ps -a
CONTAINER ID   IMAGE     COMMAND   CREATED        STATUS                    PORTS     NAMES
8237d7817211   busybox   "sh"      22 hours ago   Exited (0) 22 hours ago             config
[root@localhost config]# docker run -d -p 80:80 -p 8080:8080 --name web --volumes-from config nginx
1f57516abbc07dcb6930f6bfa4e0a939e32ebf2450baa0b3d8daa23bcc6f4972
[root@localhost config]# docker port web 
80/tcp -> 0.0.0.0:80
80/tcp -> :::80
8080/tcp -> 0.0.0.0:8080
8080/tcp -> :::8080
[root@localhost config]# 

在这里插入图片描述
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

彭宇栋

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值