Docker 安裝 nginx 並掛載宿主目錄到容器中


安裝 nginx

  1. 搜索 nginx 的鏡像

    docker search nginx
    

  2. 獲取 nginx 的官方鏡像

    docker pull nginx
    
  3. 查看本地鏡像

    docker images
    
  4. docker 啟動 nginx 鏡像,映射宿主端口 80 端口到 nginx 的 80 端口

    docker run --name nginx-test -p 8080:80 -d nginx
    
  5. 訪問宿主ip和端口,查看 nginx

掛載宿主目錄到鏡像中

  1. nginx 配置信息在容器中的位置

    • 日志位置:/var/log/nginx/
    • 配置文件位置:/etc/nginx/
    • 項目位置:/usr/share/nginx/html
  2. 創建宿主的 nginx 配置信息

    • 配置文件位置:/home/ubuntu/mynginx/nginx

      • /home/ubuntu/mynginx/nginx/conf/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;
        }
        
      • /home/ubuntu/mynginx/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  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;
            }
        
        }
        
    • 項目位置:/home/ubuntu/mynginx/nginx/html

      <!DOCTYPE html>
      <html>
      <head>
      <title>Welcome to nginx!</title>
      <style>
          body {
              width: 35em;
              margin: 0 auto;
              font-family: Tahoma, Verdana, Arial, sans-serif;
          }
      </style>
      </head>
      <body>
      <h1>hello docker!</h1>
      <h2>This is a test docker !</h2>
      </body>
      </html>
      
    • 日志位置:/home/ubuntu/mynginx/nginx/log

  3. 啟動 nginx 鏡像並掛載宿主目錄到鏡像

    docker run --name docker_nginx -d -p 80:80 -v /home/ubuntu/mynginx/nginx/log:/var/log/nginx -v /home/ubuntu/mynginx/nginx/conf/nginx.conf:/etc/nginx/nginx.conf -v /home/ubuntu/mynginx/nginx/conf.d:/etc/nginx/conf.d -v /home/ubuntu/mynginx/nginx/html:/usr/share/nginx/html nginx
    
  4. 查看 Nginx 效果


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM