您好,欢迎访问一九零五行业门户网

Docker下nginx外挂文件的方法是什么

外挂文件的目的:文件不受docker镜像文件的约束,可以修改,重启容器,可以使用更新后的文件,不会被镜像还原
容器运行过程中记录的文件如日志等信息,可以被自动保存在外部存储上,不会由于容器重启而丢失
而运行容器有两种方式:docker run命令
docker-compose命令
docker run命令方式,通过-v参数挂载外部主机目录到容器内的路径上,有多个挂载点,就通过多个-v参数指定,而且只能使用绝对路径;docker-compose命令则通过service的方式描述容易,准确的说一个服务下面可以包含多个容器,也是通过-v参数配置外部路径的挂载配置,好处是可以使用相对路径,当然是相对与docker-compose.yml文件的路径。还有一个好处是,docker-compose启动容器的命令比较简单。
假设镜像打包路径结构如下:├── build.sh├── docker-compose.yml├── dockerfile├── mynginx.conf├── nginx-vol│ ├── conf.d│ │ └── mynginx.conf│ ├── html│ │ └── index.html│ └── logs│ ├── access.log│ └── error.log└── run.sh
dockerfile为构建镜像的配置文件,内容如下:
from nginxlabel maintainer="xxx" email="<xxx@xxx.com>" app="nginx test" version="v1.0"env webdir="/data/web/html"run mkdir -p ${webdir}expose 5180
以nginx为基础,指定新的数据文件路径为/data/web/html,暴露端口为5180。
通过以下命令编译新的镜像:docker build -t nginx:test-v1 .
编译出来的镜像tag为test-v1,可以查看本地镜像:
docker imagesrepository tag image id created sizenginx test-v1 d2a0eaea3fac 56 minutes ago 141mbnginx latest 605c77e624dd 9 days ago 141mb
可以看到tag为test-v1的镜像是刚刚编译出来的新镜像。
创建nginx外挂卷nginx-vol以及相关的conf.d、logs、html文件夹,并把对应的内容放入各自对应的目录下。如html文件夹下的iindex.html内容如下:
<html> <head> <meta http-equiv="content-type" content="text/html;charset=utf-8" /> <title>系统时间</title> <body> <div id="datetime"> <script> setinterval("document.getelementbyid('datetime').innerhtml=new date().tolocalestring();",1000); </script> </div> </body> </head></html>
其实就是显示当前时间的一个页面而已。
logs下面为空,目的是让容器运行时的日志写到外部存储,即使容器停止或镜像销毁,运行过的日志仍然可以保留。
conf.d下面为nginx个性化配置,内容如下:
server { listen 5180; #listen [::]:5180; server_name localhost; #access_log /var/log/nginx/host.access.log main; location / { root /data/web/html; index index.html index.htm; } #error_page 404 /404.html; # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; # proxy the php scripts to apache listening on 127.0.0.1:80 #location ~ \.php$ { # proxy_pass http://127.0.0.1; #} # pass the php scripts to fastcgi server listening on 127.0.0.1:9000 # root html; # fastcgi_pass 127.0.0.1:9000; # fastcgi_index index.php; # fastcgi_param script_filename /scripts$fastcgi_script_name; # include fastcgi_params; # deny access to .htaccess files, if apache's document root # concurs with nginx's one #location ~ /\.ht { # deny all;}
其实也就是在nginx默认的default.conf基础上修改了端口和root路径,目的是说明nginx的配置文件也可以使用外部存储的,如果是自己的程序可以修改配置文件,那通过这样的方式,可以在容器运行过程中修改配置文件;修改的配置文件实际存储在外部存储上,因此不会随着容器的停止运行而消失,也不会恢复为镜像内部的文件。
docker run模式为了方便,可以把运行命令写到shell脚本中,如run.sh,内容如下:
docker run --name nginx-v1 -p 15180:5180 -v /home/project/nginx-test/nginx-vol/logs:/var/log/nginx -v /home/project/nginx-test/nginx-vol/conf.d:/etc/nginx/conf.d -v /home/project/nginx-test/nginx-vol/html:/data/web/html -d nginx:test-v1
可以看到命令中有3个-v,分别对应不同的外部存储的挂载,映射到容器内的不同目录下。
-p(注意是小写)后面的端口分别为主机端口和容器端口,也就是主机的15180端口映射到容器的5180端口,这样容器所启动的nginx服务端口5180就可以通过访问主机的15180端口而被映射起来。
查看运行中的容器:
docker ps -acontainer id image command created status ports namescf2275da5130 nginx:test-v1 "/docker-entrypoint.…" 6 seconds ago up 5 seconds 80/tcp, 0.0.0.0:15180->5180/tcp, :::15180->5180/tcp nginx-v1
详细映射查看:
docker inspect nginx-v1
会显示完整的信息,其中"mounts"部分可以看到完整的存储挂载映射情况。
直接看主机的nginx-vol/logs下面,可以看到容器中的nginx运行日志自动写到了外部主机的存储上。
ls -l nginx-vol/logs/total 12-rw-r--r-- 1 root root 1397 1月 8 15:08 access.log-rw-r--r-- 1 root root 4255 1月 8 15:59 error.log
停止容器:
docker stop nginx-v1
删除容器:
docker rm nginx-v1
docker-compose模式安装docker-compose
apt-get install docker-compose
编写docker-compose.yml文件
version: "3"services: nginx: container_name: mynginx image: nginx:test-v1 ports: - 80:5180 volumes: - ./nginx-vol/html:/data/web/html - ./nginx-vol/logs:/var/log/nginx - ./nginx-vol/conf.d:/etc/nginx/conf.d restart: always
container_name:指定容器名称
image:要使用的镜像以及对应的标签
ports:主机端口与容器端口映射
volumes:外部存储挂载映射
启动容器
docker-compose up -dcreating network "nginxtest_default" with the default drivercreating mynginx ...creating mynginx ... done
查看容器
docker ps -acontainer id image command created status ports names635e2999c825 nginx:test-v1 "/docker-entrypoint.…" 24 seconds ago up 22 seconds 80/tcp, 0.0.0.0:80->5180/tcp, :::80->5180/tcp mynginx
可以看到容器按照docker-compose.yml配置运行,端口、名称、挂载都正常。访问主机的80端口即可对应容器的5180服务。
停止容器
docker-compose downstopping mynginx ... doneremoving mynginx ... doneremoving network nginxtest_default
可以看到,使用docker-compose更简单。
以上就是docker下nginx外挂文件的方法是什么的详细内容。
其它类似信息

推荐信息