当前位置:   article > 正文

Docker(三)----Dockerfile搭建Nginx环境与文件挂载_dockerfile nginx 挂载

dockerfile nginx 挂载

1.Dockerfile文件

  1. # This my first nginx Dockerfile
  2. # Version 1.0
  3. # Base images 基础镜像
  4. FROM centos:centos7
  5. #MAINTAINER 维护者信息
  6. MAINTAINER fendo 2312892206@qq.com
  7. #ADD 获取url中的文件,放在当前目录下
  8. ADD http://nginx.org/download/nginx-1.14.0.tar.gz .
  9. #RUN 执行以下命令
  10. RUN yum install -y pcre-devel wget net-tools gcc zlib zlib-devel make openssl-devel
  11. RUN useradd -M -s /sbin/nologin nginx
  12. RUN tar -zxvf nginx-1.14.0.tar.gz
  13. RUN mkdir -p /usr/local/nginx
  14. RUN cd nginx-1.14.0 && ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module && make && make install
  15. RUN ln -s /usr/local/nginx/sbin/* /usr/local/sbin/
  16. #EXPOSE 映射端口
  17. EXPOSE 80
  18. #CMD 运行以下命令
  19. CMD ["nginx"]

2、执行构建镜像命令

[root@swarm01 docker]# docker build --rm --tag centos_nginx:centos7 .


3、查看镜像是否安装构建成功 

[root@swarm01 docker]# docker images


4、启动容器

可以通过以下两种方式启动容器:

  1. 不挂载文件:
  2. docker run -i -t -d -p 192.168.182.110:80:80 centos_nginx /bin/bash
  3. 挂载文件:
  4. docker run \
  5. --name centos_nginx \
  6. -d -p 80:80 \
  7. -v /home/nginx/html:/usr/share/nginx/html \
  8. -v /home/nginx/log:/var/log/nginx \
  9. -v /home/nginx/nginx.conf:/usr/local/nginx/nginx.conf:ro \
  10. -v /home/nginx/conf.d:/usr/local/nginx/conf.d \
  11. nginx

注意:

(1)第一个"-v",是项目位置,把项目放到挂载到的目录下即可;
(2)第二个"-v",是挂载的主配置文件"nginx.conf",注意"nginx.conf"文件内有一行"include /etc/nginx/conf.d/*.conf;",这个include指向了子配置文件的路径,此处注意include后所跟的路径一定不要出错。
(3)第三个"-v",把docker内子配置文件的路径也挂载了出来,注意要与(2)中include指向路径一致

(4)重点强调一下,nginx.conf是挂载了一个文件(docker是不推荐这样用的),conf.d挂载的是一个目录

启动成功之后如下:


5.在系统中创建Nginx配置文件

通过上面的命令启动之后,会在/home/nginx目录下多出以下文件


这些文件就是与Docker容器共享的配置文件,在nginx.conf目录下创建Nginx配置文件nginx.conf,内容如下:

  1. user root;
  2. worker_processes 1;
  3. error_log /var/log/nginx/error.log warn;
  4. pid /var/run/nginx.pid;
  5. events {
  6. worker_connections 1024;
  7. }
  8. http {
  9. include /etc/nginx/mime.types;
  10. default_type application/octet-stream;
  11. log_format main '$remote_addr - $remote_user [$time_local] "$request" '
  12. '$status $body_bytes_sent "$http_referer" '
  13. '"$http_user_agent" "$http_x_forwarded_for"';
  14. access_log /var/log/nginx/access.log main;
  15. sendfile on;
  16. #tcp_nopush on;
  17. keepalive_timeout 65;
  18. autoindex on;
  19. #gzip on;
  20. include /etc/nginx/conf.d/*.conf;
  21. client_max_body_size 100M;
  22. client_header_buffer_size 128k;
  23. large_client_header_buffers 4 128k;
  24. }

然后在conf.d目录下创建default.conf文件,内容如下:

  1. server {
  2. listen 80;
  3. server_name localhost;
  4. #charset koi8-r;
  5. #access_log /var/log/nginx/log/host.access.log main;
  6. location / {
  7. root /home/nginx/html/fendo;
  8. index index.html index.htm;
  9. autoindex on;
  10. try_files $uri /index/index/page.html;
  11. #try_files $uri /index/map/page.html;
  12. }
  13. #error_page 404 /404.html;
  14. # redirect server error pages to the static page /50x.html
  15. #
  16. error_page 500 502 503 504 /50x.html;
  17. location = /50x.html {
  18. root /usr/share/nginx/html;
  19. }
  20. # proxy the PHP scripts to Apache listening on 127.0.0.1:80
  21. #
  22. #location ~ \.php$ {
  23. # proxy_pass http://127.0.0.1;
  24. #}
  25. # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
  26. #
  27. #location ~ \.php$ {
  28. # root html;
  29. # fastcgi_pass 127.0.0.1:9000;
  30. # fastcgi_index index.php;
  31. # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
  32. # include fastcgi_params;
  33. #}
  34. # deny access to .htaccess files, if Apache's document root
  35. # concurs with nginx's one
  36. #
  37. #location ~ /\.ht {
  38. # deny all;
  39. #}
  40. }

然后在/home/nginx/fendo目录下创建Index.html文件,内容为:

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>Welcome to nginx! This is a demo!</title>
  5. <style>
  6. body {
  7. width: 35em;
  8. margin: 0 auto;
  9. font-family: Tahoma, Verdana, Arial, sans-serif;
  10. }
  11. </style>
  12. </head>
  13. <body>
  14. <h1>*********************************** Welcome to nginx! Fendo ***********************************</h1>
  15. <p>If you see this page, the nginx web server is successfully installed and
  16. working. Further configuration is required.</p>
  17. <p>For online documentation and support please refer to
  18. <a href="http://nginx.org/">nginx.org</a>.<br/>
  19. Commercial support is available at
  20. <a href="http://nginx.com/">nginx.com</a>.</p>
  21. <p><em>Thank you for using nginx.</em></p>
  22. </body>
  23. </html>

修改好之后,重启下Nginx

  1. [root@swarm01 conf.d]# docker restart centos_nginx
  2. centos_nginx

6.测试Nginx

访问http://192.168.182.110/fendo/效果如下:


7.一些错误

7.1、访问 curl http://192.168.182.110/fendo/出现拒绝连接,解决方法就是进入容器 docker exec -i -t centos_nginx /bin/sh接着在容器里面执行(直接输入即可)nginx 启动Nginx。

7.2、如过挂载Nginx文件时报错,基本就是路径的问题。

unknown: Are you trying to mount a directory onto a file (or vice-versa)? Check if the specified host path exists and is the expected type

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/196086
推荐阅读
相关标签
  

闽ICP备14008679号