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

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

获取nginx配置文件

[root@localhost ~]# mkdir config
[root@localhost ~]# cd config/
[root@localhost config]# ls
[root@localhost config]# yum -y install nginx
[root@localhost config]# cp /etc/nginx/* ./
[root@localhost config]# ls
conf.d     fastcgi.conf          fastcgi_params          koi-utf  mime.types          nginx.conf          scgi_params          uwsgi_params          win-utf
default.d  fastcgi.conf.default  fastcgi_params.default  koi-win  mime.types.default  nginx.conf.default  scgi_params.default  uwsgi_params.default

修改配置文件

[root@localhost ~]# cat /config/nginx.conf
......
    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /etc/nginx/conf.d/*.conf;
	#添加以下8行内容
    server{									#添加
        listen       8080;					#添加
        server_name  web.example.com;		#添加

        location / {						#添加
            root /usr/share/nginx/html/web;	#添加
            index index.html;				#添加
        }									#添加
    }										#添加

    server {
        listen       80;								#将default_server删除
        listen       [::]:80 default_server;
        server_name  test.example.com;					#将_;修改为test.example.com;
        root         /usr/share/nginx/html/test;		#将/usr/share/nginx/html;修改为/usr/share/nginx/html/test;

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

创建网页存放目录

[root@localhost ~]# mkdir -p /var/www/html/
[root@localhost ~]# cd /var/www/html/
[root@localhost html]# mkdir web
[root@localhost html]# mkdir test
[root@localhost html]# ls
test  web

#上传网站
[root@localhost html]# cd web/
[root@localhost web]# ls
80站长网.url  css  index.html  js  免费素材下载.url  商业源码免费下载.url  织梦模板免费下载.url


#测试页面
[root@localhost html]# cd test/
[root@localhost test]# echo 'test' > index.html
[root@localhost test]# ls
index.html

--volumes-from

后面跟容器名,来寄存指定容器所拥有的存储库
与-v结合使用实现多目录的存储库绑定

[root@localhost ~]# docker run -d --name config -v /root/config/:/etc/nginx/ nginx
5f7ca3fb66bb2d9e598f6869d3949d5f73d5089373e6b5a5b92775e6f79ab825


[root@localhost config]# docker run -d --name nginx01 -p 80:80 -p 8080:8080 --volumes-from config -v /var/www/html/:/usr/share/nginx/html --restart always nginx
2f2412baaea55501e2a7aef463311ab64017c566ddb006b7e28590daa9ae1d44
[root@localhost config]# docker ps
CONTAINER ID   IMAGE     COMMAND                  CREATED          STATUS          PORTS                                                                          NAMES
2f2412baaea5   nginx     "/docker-entrypoint.…"   7 seconds ago    Up 7 seconds    0.0.0.0:80->80/tcp, :::80->80/tcp, 0.0.0.0:8080->8080/tcp, :::8080->8080/tcp   nginx01
5f7ca3fb66bb   nginx     "/docker-entrypoint.…"   20 minutes ago   Up 20 minutes   80/tcp   


#可以用docker rm -f来删除用来存储库依赖关系的容器,且新创建的容器不受影响(本机源目录不可以删除)
root@localhost config]# docker rm -f config
config
[root@localhost config]# docker ps
CONTAINER ID   IMAGE     COMMAND                  CREATED         STATUS         PORTS                                                                          NAMES
2f2412baaea5   nginx     "/docker-entrypoint.…"   9 minutes ago   Up 9 minutes   0.0.0.0:80->80/tcp, :::80->80/tcp, 0.0.0.0:8080->8080/tcp, :::8080->8080/tcp   nginx01
      

网页测试

在这里插入图片描述

在这里插入图片描述

--restart always

可以实现docker服务启动时启动容器

[root@localhost ~]# docker run -d --name test --restart always nginx
e42e9534e92aba1dc26c026c27bb48d97b95b5ee95697cfa9469356a3a07aca6

[root@localhost ~]# docker run -d --name test2 nginx
8a25f16a176faab6a47d67a3f423b665184e19888ce0e2934fe9cf232ca40731

[root@localhost config]# docker ps 
CONTAINER ID   IMAGE     COMMAND                  CREATED         STATUS         PORTS     NAMES
8a25f16a176f   nginx     "/docker-entrypoint.…"   2 minutes ago   Up 2 minutes   80/tcp    test2
e42e9534e92a   nginx     "/docker-entrypoint.…"   2 minutes ago   Up 2 minutes   80/tcp    test

[root@localhost ~]# systemctl restart docker

[root@localhost ~]# docker ps
CONTAINER ID   IMAGE     COMMAND                  CREATED         STATUS          PORTS     NAMES
e42e9534e92a   nginx     "/docker-entrypoint.…"   2 minutes ago   Up 11 seconds   80/tcp    test

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值