各位用户为了找寻关于docker部署nginx并且挂载文件夹和文件操作的资料费劲了很多周折。这里教程网为您整理了关于docker部署nginx并且挂载文件夹和文件操作的相关资料,仅供查阅,以下为您介绍关于docker部署nginx并且挂载文件夹和文件操作的详细内容

这段时间在研究docker,在部署nginx时遇到了坑,最主要的问题是在挂载文件和文件夹的时候不知道怎么挂载,经过反复实验以及查看网上的教程,先总结如下:

1首先pull下载nginx镜像包

docker pull nginx

2(关键)查看nginx镜像里面配置文件、日志等文件的具体位置,只有找到镜像配置文件的路径,后面挂载文件和文件夹才能覆盖这些路径

以终端的方式打开镜像容器

? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 [root@docker2 nginx]# docker run -i -t nginx /bin/bash root@3b39da9212fe:/# ls -l total 8 drwxr-xr-x  2 root root 4096 Apr 26 00:00 bin drwxr-xr-x  2 root root  6 Feb 23 23:23 boot drwxr-xr-x  5 root root 360 May 30 01:39 dev drwxr-xr-x  1 root root  66 May 30 01:39 etc drwxr-xr-x  2 root root  6 Feb 23 23:23 home drwxr-xr-x  1 root root  45 Apr 26 00:00 lib drwxr-xr-x  2 root root  34 Apr 26 00:00 lib64 drwxr-xr-x  2 root root  6 Apr 26 00:00 media drwxr-xr-x  2 root root  6 Apr 26 00:00 mnt drwxr-xr-x  2 root root  6 Apr 26 00:00 opt dr-xr-xr-x 176 root root  0 May 30 01:39 proc drwx------  2 root root  37 Apr 26 00:00 root drwxr-xr-x  4 root root  43 Apr 26 00:00 run drwxr-xr-x  2 root root 4096 Apr 26 00:00 sbin drwxr-xr-x  2 root root  6 Apr 26 00:00 srv dr-xr-xr-x 13 root root  0 May 25 06:07 sys drwxrwxrwt  1 root root  6 Apr 30 13:55 tmp drwxr-xr-x  1 root root  66 Apr 26 00:00 usr drwxr-xr-x  1 root root  17 Apr 26 00:00 var

找到镜像中nginx.conf配置文件路径/etc/nginx/nginx.conf

? 1 2 3 4 5 6 7 8 9 10 11 12 root@3b39da9212fe:/etc/nginx# ls -l /etc/nginx/ total 36 drwxr-xr-x 2 root root  26 Apr 30 13:55 conf.d -rw-r--r-- 1 root root 1007 Apr 9 16:01 fastcgi_params -rw-r--r-- 1 root root 2837 Apr 9 16:01 koi-utf -rw-r--r-- 1 root root 2223 Apr 9 16:01 koi-win -rw-r--r-- 1 root root 5170 Apr 9 16:01 mime.types lrwxrwxrwx 1 root root  22 Apr 9 16:01 modules -> /usr/lib/nginx/modules -rw-r--r-- 1 root root 643 Apr 9 16:01 nginx.conf -rw-r--r-- 1 root root 636 Apr 9 16:01 scgi_params -rw-r--r-- 1 root root 664 Apr 9 16:01 uwsgi_params -rw-r--r-- 1 root root 3610 Apr 9 16:01 win-utf

找到default.conf配置文件的路径/etc/nginx/conf.d/default.conf

? 1 2 3 root@3b39da9212fe:/etc# ls -l /etc/nginx/conf.d/       total 4 -rw-r--r-- 1 root root 1093 Apr 9 16:01 default.conf

找到默认首页文件夹html路径/usr/share/nginx/html

? 1 2 3 root@3b39da9212fe:/etc# ls -l /usr/share/nginx/   total 0 drwxr-xr-x 2 root root 40 Apr 30 13:55 html

找到日志文件路径/var/log/nginx

? 1 2 3 4 5 6 7 8 9 ls -l /var/log/   total 96 drwxr-xr-x 1 root root  60 Apr 30 13:55 apt -rw-rw---- 1 root utmp   0 Apr 26 00:00 btmp -rw-r--r-- 1 root root 57602 Apr 30 13:55 dpkg.log -rw-r--r-- 1 root root 3264 Apr 30 13:55 faillog -rw-rw-r-- 1 root utmp 29784 Apr 30 13:55 lastlog drwxr-xr-x 1 root root  41 Apr 30 13:55 nginx -rw-rw-r-- 1 root utmp   0 Apr 26 00:00 wtmp

然后输入exit退出容器的终端

3用nginx镜像启动容器mynginx并且挂载文件夹和文件到容器中

这里说明一下为什么我要挂载配置文件和文件夹,如果你部署应用并且很轻易地修改nginx的配置文件,如果挂载了文件或者文件夹那么你只需要修改挂载源的文件或者文件夹里面的文件就可以了,而不用每次都要使用docker run -i -t nginx /bin/bash命令进入到镜像终端中去修改配置文件,下面我将演示修改自己的nginx首页,并且将其挂载上去容器中覆盖掉原来的默认的首页

在linux系统中创建挂载源文件和文件夹(我的是centos7)

? 1 2 3 4 mkdir -p /data/nginx/conf mkdir -p /data/nginx/conf.d mkdir -p /data/nginx/html mkdir -p /data/nginx/logs

然后创建在conf文件夹里面创建一个nginx.conf配置文件,并且输入一下内容,建议大家不要照抄我的配置,用我上面介绍的第一步的方法进入到nginx容器的终端中复制nginx.conf配置文件的内容到linux系统中这个新创建的nginx.conf文件中进行修改,这样子就保证了配置文件中的路径与镜像中配置文件的路径能保持一致

? 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 [root@docker2 /]# cd /data/nginx/conf [root@docker2 conf]# more nginx.conf user nginx; worker_processes 1;   error_log /var/log/nginx/error.log warn; pid    /var/run/nginx.pid;   events {   worker_connections 1024; }   http {   include    /etc/nginx/mime.types;   default_type application/octet-stream;     log_format main '$remote_addr - $remote_user [$time_local] "$request" '            '$status $body_bytes_sent "$http_referer" '            '"$http_user_agent" "$http_x_forwarded_for"';     access_log /var/log/nginx/access.log main;     sendfile    on;   #tcp_nopush   on;     keepalive_timeout 65;     #gzip on;     include /etc/nginx/conf.d/*.conf; }

在conf.d里面创建一个default.conf文件,并且输入一下内容,同样这个内容也是我从镜像中default.conf默认的配置文件中复制过来修改的,同样建议大家不要照抄我的内容,因为涉及到路径那些可能会与你们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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 [root@docker2 conf]# more /data/nginx/conf.d/default.conf  server {   listen    80;   server_name localhost;     #charset koi8-r;   #access_log /var/log/nginx/host.access.log main;     location / {     root  /usr/share/nginx/html;     index 1.html;   }     #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   #   #location ~ .php$ {   #  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;   #} }

大家注意了,这里我修改了一下原来默认配置文件里面的内容,在上面的其中一个location中,我把nginx默认首页index改成了1.html,1.html是我自己创建的首页名

在html文件夹下创建1.html首页文件,并且编写属于自己的首页,这里我是用notepadd++在windows上面写好了1.html文件再通过工具拷过去linux系统里面的,注意有中文的可能要转换下编码,不然可能会乱码,例如我这里用的是ansi的编码

? 1 2 3 4 5 6 7 8 9 10 <html> <head> <title>Mynginx</title> </head> <body> <h1> 欢迎使用nginx! </h1> </body> </html>

现在是创建容器并且挂载文件和文件夹了

? 1 [root@docker2 conf]# docker run --name mynginx -d -p 80:80 -v /data/nginx/html:/usr/share/nginx/html -v /data/nginx/conf/nginx.conf:/etc/nginx/nginx.conf -v /data/nginx/conf.d/default.conf:/etc/nginx/conf.d/default.conf -v /data/nginx/logs:/var/log/nginx nginx

记住挂载的目标目录或者文件路径要与镜像中的路径保持一致如/etc/nginx/nginx.conf,这个路径在第二步里面已经找出来了

docker ps 查看有没有启动成功

? 1 2 3 [root@docker2 conf]# docker ps CONTAINER ID    IMAGE        COMMAND         CREATED       STATUS       PORTS          NAMES 32ad171d34a2    nginx        "nginx -g 'daemon of…"  17 hours ago    Up 17 hours     0.0.0.0:80->80/tcp    mynginx

如果没有启动成功要先用docker ps -a查看失败的容器,并且用docker rm CONTAILNER ID删除容器ID,再查找问题,然后docker run再启动容器,如果在确保挂载的目录和文件没有问题还是不能启动的话,那么就是权限问题了,网上说的就是在docker run后面加个 --privileged=true参数

http://IP 打开网页看看效果把

以上这篇docker部署nginx并且挂载文件夹和文件操作就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/qq_26614295/article/details/80505246