外挂文件的目的:
- 文件不受docker镜像文件的约束,可以修改,重启容器,可以使用更新后的文件,不会被镜像还原
- 容器运行过程中记录的文件如日志等信息,可以被自动保存在外部存储上,不会由于容器重启而丢失
而运行容器有两种方式:
- docker run命令
- docker-compose命令
docker run命令方式,通过-v参数挂载外部主机目录到容器内的路径上,有多个挂载点,就通过多个-v参数指定,而且只能使用绝对路径;docker-compose命令则通过service的方式描述容易,准确的说一个服务下面可以包含多个容器,也是通过-v参数配置外部路径的挂载配置,好处是可以使用相对路径,当然是相对与docker-compose.yml文件的路径。还有一个好处是,docker-compose启动容器的命令比较简单。
假设镜像打包路径结构如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
|
├── 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为构建镜像的配置文件,内容如下:
1
2
3
4
5
|
FROM nginx LABEL 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。
通过以下命令编译新的镜像:
1
|
docker build -t nginx: test -v1 . |
编译出来的镜像tag为test-v1,可以查看本地镜像:
1
2
3
4
|
docker images REPOSITORY TAG IMAGE ID CREATED SIZE nginx test -v1 d2a0eaea3fac 56 minutes ago 141MB nginx latest 605c77e624dd 9 days ago 141MB |
可以看到TAG为test-v1的镜像是刚刚编译出来的新镜像。
创建nginx外挂卷nginx-vol以及相关的conf.d、logs、html文件夹,并把对应的内容放入各自对应的目录下。如html文件夹下的iindex.html内容如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
|
< 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个性化配置,内容如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
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,内容如下:
1
|
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端口而被映射起来。
查看运行中的容器:
1
2
3
|
docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES cf2275da5130 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 |
详细映射查看:
1
|
docker inspect nginx-v1 |
会显示完整的信息,其中"Mounts"部分可以看到完整的存储挂载映射情况。
直接看主机的nginx-vol/logs下面,可以看到容器中的nginx运行日志自动写到了外部主机的存储上。
1
2
3
4
|
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 |
停止容器:
1
|
docker stop nginx-v1 |
删除容器:
1
|
docker rm nginx-v1 |
docker-compose模式
安装docker-compose
1
|
apt-get install docker-compose |
编写docker-compose.yml文件
1
2
3
4
5
6
7
8
9
10
11
12
|
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:外部存储挂载映射
启动容器
1
2
3
4
|
docker-compose up -d Creating network "nginxtest_default" with the default driver Creating mynginx ... Creating mynginx ... done |
查看容器
1
2
3
|
docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 635e2999c825 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服务。
停止容器
1
2
3
4
|
docker-compose down Stopping mynginx ... done Removing mynginx ... done Removing network nginxtest_default |
可以看到,使用docker-compose更简单。
到此这篇关于Docker下nginx外挂配置和文件的文章就介绍到这了,更多相关Docker nginx外挂配置内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!
原文链接:https://blog.csdn.net/brucemiao/article/details/122617768