一、docker拉取nginx

  1. docker pull nginx

二、创建数据卷映射文件

1.创建映射保存文件夹

  1. cd /opt
  2. mkdir nginx
  3. cd nginx
  4. mkdir conf conf.d html logs

2.编写相关配置文件

  • /opt/nginx/conf/nginx.conf
  1. cd /opt/nginx/conf
  2. vim nginx.conf
  1. user nginx;
  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. #gzip on;
  19. include /etc/nginx/conf.d/*.conf;
  20. }
  • /opt/nginx/conf.d/default.conf
  1. cd /opt/nginx/conf.d
  2. vim 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 /usr/share/nginx/html;
  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. }
  • /opt/nginx/html/index.html
  1. cd /opt/nginx/html
  2. vim index.html
  1. <html>
  2. <head>
  3. <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
  4. <title>nginx</title>
  5. </head>
  6. <body>
  7. <div>
  8. docker nignx
  9. </div>
  10. </body>

3.docker 启动nginx

docker run --name nginx80 -d -p 80:80 -v /opt/nginx/html:/usr/share/nginx/html -v /opt/nginx/conf/nginx.conf:/etc/nginx/nginx.conf -v /opt/nginx/logs:/var/log/nginx -v /opt/nginx/conf.d:/etc/nginx/conf.d nginx

7ba29edc6408a80270d6887c5ac8701c.jpg