1. 程式人生 > >docker部署nginx並且掛載資料夾和檔案

docker部署nginx並且掛載資料夾和檔案

轉載來源:https://blog.csdn.net/qq_26614295/article/details/80505246

這段時間在研究docker,在部署nginx時遇到了坑,最主要的問題是在掛載檔案和資料夾的時候不知道怎麼掛載,經過反覆實驗以及檢視網上的教程,先總結如下:

1首先pull下載nginx映象包

docker pull nginx

2(關鍵)檢視nginx映象裡面配置檔案、日誌等檔案的具體位置,只有找到映象配置檔案的路徑,後面掛載檔案和資料夾才能覆蓋這些路徑

以終端的方式開啟映象容器

    [[email protected]

nginx]# docker run -i -t nginx /bin/bash
    [email protected]:/# 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

    [email protected]:/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

    [email protected]:/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

    [email protected]:/etc# ls -l /usr/share/nginx/     
    total 0
    drwxr-xr-x 2 root root 40 Apr 30 13:55 html

找到日誌檔案路徑/var/log/nginx

    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)

    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檔案中進行修改,這樣子就保證了配置檔案中的路徑與映象中配置檔案的路徑能保持一致

    [[email protected] /]# cd /data/nginx/conf
    [[email protected] 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映象中的路徑不一致,這樣子在啟動映象建立容器的時候就無法用掛載的方法覆蓋掉容器中的路徑

    [[email protected] 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的編碼

    <html>
    <head>
    <title>Mynginx</title>
    </head>
    <body>
    <h1>
    歡迎使用nginx!
    </h1>
    </body>
    </html>

現在是建立容器並且掛載檔案和檔案夾了

[[email protected] 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 檢視有沒有啟動成功

    [[email protected] 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  開啟網頁看看效果把

 

 

---------------------
作者:丟你劉某
來源:CSDN
原文:https://blog.csdn.net/qq_26614295/article/details/80505246
版權宣告:本文為博主原創文章,轉載請附上博文連結!