Docker存储卷实战-Nginx

Docker存储卷实战-Nginx

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

映射配置文件和网站

// 真机网站存放位置
[root@localhost ~]# ls /var/www/html/
game.html  images  index.html  js  style

// 拉取一个nginx镜像
[root@localhost ~]# docker pull nginx


[root@localhost ~]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED             SIZE
busybox      latest    ffe9d497c324   About an hour ago   1.24MB
nginx        latest    f652ca386ed1   5 days ago          141MB
centos       latest    5d0da3dc9764   2 months ago        231MB

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


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

// 把测试的容器删除
[root@localhost ~]# docker ps -a
CONTAINER ID   IMAGE     COMMAND   CREATED              STATUS                          PORTS     NAMES
db86571f61a6   busybox   "sh"      About a minute ago   Exited (0) 5 seconds ago                  affectionate_dewdney
2f9e5d335efb   busybox   "sh"      2 minutes ago        Exited (0) About a minute ago             html
[root@localhost ~]# docker rm -f affectionate_dewdney 
affectionate_dewdney
[root@localhost ~]# docker ps -a
CONTAINER ID   IMAGE     COMMAND   CREATED         STATUS                     PORTS     NAMES
2f9e5d335efb   busybox   "sh"      2 minutes ago   Exited (0) 2 minutes ago             html

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

// 先用yum安装一个nginx
[root@localhost ~]# yum -y install nginx

// 默认配置文件存放位置
[root@localhost ~]# ls /etc/nginx/
conf.d                  koi-utf             scgi_params
default.d               koi-win             scgi_params.default
fastcgi.conf            mime.types          uwsgi_params
fastcgi.conf.default    mime.types.default  uwsgi_params.default
fastcgi_params          nginx.conf          win-utf
fastcgi_params.default  nginx.conf.default

// 复制配置文件到config目录下
[root@localhost ~]# cp -r /etc/nginx/* /config/
[root@localhost ~]# ls /config/
conf.d                  koi-utf             scgi_params
default.d               koi-win             scgi_params.default
fastcgi.conf            mime.types          uwsgi_params
fastcgi.conf.default    mime.types.default  uwsgi_params.default
fastcgi_params          nginx.conf          win-utf
fastcgi_params.default  nginx.conf.default

// 配置文件转移成功之后。可以删除真机上的nginx了
[root@localhost ~]# yum -y remove nginx

// 创建一个数据卷容器,数据来源于html容器,给配置文件做一个映射
[root@localhost ~]# docker run --name config --volumes-from html -v /config:/etc/nginx busybox
[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
e008c59e6db5   busybox   "sh"      6 seconds ago   Exited (0) 6 seconds ago             config
2f9e5d335efb   busybox   "sh"      9 minutes ago   Exited (0) 9 minutes ago             html

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

// 删除测试容器
/ # exit
[root@localhost ~]# docker ps -a
CONTAINER ID   IMAGE     COMMAND   CREATED          STATUS                      PORTS     NAMES
8f877c35c199   busybox   "sh"      2 minutes ago    Exited (0) 56 seconds ago             eager_keldysh
e008c59e6db5   busybox   "sh"      3 minutes ago    Exited (0) 3 minutes ago              config
2f9e5d335efb   busybox   "sh"      12 minutes ago   Exited (0) 12 minutes ago             html
[root@localhost ~]# docker rm -f eager_keldysh 
eager_keldysh
[root@localhost ~]# docker ps -a
CONTAINER ID   IMAGE     COMMAND   CREATED          STATUS                      PORTS     NAMES
e008c59e6db5   busybox   "sh"      11 minutes ago   Exited (0) 11 minutes ago             config
2f9e5d335efb   busybox   "sh"      20 minutes ago   Exited (0) 20 minutes ago             html

// 删除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/
game.html   images      index.html  js          style
/ # ls /etc/nginx/
conf.d                  mime.types.default
default.d               nginx.conf
fastcgi.conf            nginx.conf.default
fastcgi.conf.default    scgi_params
fastcgi_params          scgi_params.default
fastcgi_params.default  uwsgi_params
koi-utf                 uwsgi_params.default
koi-win                 win-utf
mime.types
/ # exit
[root@localhost ~]# docker ps -a
CONTAINER ID   IMAGE     COMMAND   CREATED          STATUS                      PORTS     NAMES
e008c59e6db5   busybox   "sh"      13 minutes ago   Exited (0) 13 minutes ago             config

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

页面访问

在这里插入图片描述

更换网站内容

[root@localhost ~]# cd /var/www/html/
[root@localhost html]# mv index.html 1.html
[root@localhost html]# ls
1.html  game.html  images  js  style
[root@localhost html]# echo "hello world" > index.html
[root@localhost html]# cat index.html 
hello world

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

制作第二个网站访问页面

[root@localhost html]# mkdir test
[root@localhost html]# echo "test page" > test/index.html

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

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

// 使用真机修改配置文件,用两个端口号进行访问,映射到容器里面
[root@localhost html]# cd /config/
[root@localhost config]# ls
conf.d                  koi-utf             scgi_params
default.d               koi-win             scgi_params.default
fastcgi.conf            mime.types          uwsgi_params
fastcgi.conf.default    mime.types.default  uwsgi_params.default
fastcgi_params          nginx.conf          win-utf
fastcgi_params.default  nginx.conf.default

[root@localhost config]# vim nginx.conf
    # for more information.
    include /etc/nginx/conf.d/*.conf;

    server {
        listen      8080;								// 第二个网站使用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]# docker ps -a
CONTAINER ID   IMAGE     COMMAND                  CREATED         STATUS                   PORTS                                                                          NAMES
c730e1ec7a03   nginx     "/docker-entrypoint...."   8 minutes ago   Up 8 minutes             0.0.0.0:80->80/tcp, :::80->80/tcp, 0.0.0.0:8080->8080/tcp, :::8080->8080/tcp   web
e008c59e6db5   busybox   "sh"                     2 hours ago     Exited (0) 2 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
e008c59e6db5   busybox   "sh"      2 hours ago   Exited (0) 2 hours ago             config

// 做80端口和8080端口映射
[root@localhost config]# docker run -d -p 80:80 -p 8080:8080 --name web --volumes-from config nginx
c730e1ec7a03b6622c1ecdaeff66ce64c15aec98dfdda3bc3f4905cfbf603d63
[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

直接访问
在这里插入图片描述
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值