欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

docker部署nginx并且掛載文件夾和文件操作

 更新時間:2020年11月18日 16:41:56   作者:丟你劉某  
這篇文章主要介紹了docker部署nginx并且掛載文件夾和文件操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧

這段時間在研究docker,在部署nginx時遇到了坑,最主要的問題是在掛載文件和文件夾的時候不知道怎么掛載,經(jīng)過反復實驗以及查看網(wǎng)上的教程,先總結(jié)如下:

1首先pull下載nginx鏡像包

docker pull nginx

2(關(guān)鍵)查看nginx鏡像里面配置文件、日志等文件的具體位置,只有找到鏡像配置文件的路徑,后面掛載文件和文件夾才能覆蓋這些路徑

以終端的方式打開鏡像容器

[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

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

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

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

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并且掛載文件夾和文件到容器中

這里說明一下為什么我要掛載配置文件和文件夾,如果你部署應(yīng)用并且很輕易地修改nginx的配置文件,如果掛載了文件或者文件夾那么你只需要修改掛載源的文件或者文件夾里面的文件就可以了,而不用每次都要使用docker run -i -t nginx /bin/bash命令進入到鏡像終端中去修改配置文件,下面我將演示修改自己的nginx首頁,并且將其掛載上去容器中覆蓋掉原來的默認的首頁

在linux系統(tǒng)中創(chuàng)建掛載源文件和文件夾(我的是centos7)

mkdir -p /data/nginx/conf
mkdir -p /data/nginx/conf.d
mkdir -p /data/nginx/html
mkdir -p /data/nginx/logs

然后創(chuàng)建在conf文件夾里面創(chuàng)建一個nginx.conf配置文件,并且輸入一下內(nèi)容,建議大家不要照抄我的配置,用我上面介紹的第一步的方法進入到nginx容器的終端中復制nginx.conf配置文件的內(nèi)容到linux系統(tǒng)中這個新創(chuàng)建的nginx.conf文件中進行修改,這樣子就保證了配置文件中的路徑與鏡像中配置文件的路徑能保持一致

[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里面創(chuàng)建一個default.conf文件,并且輸入一下內(nèi)容,同樣這個內(nèi)容也是我從鏡像中default.conf默認的配置文件中復制過來修改的,同樣建議大家不要照抄我的內(nèi)容,因為涉及到路徑那些可能會與你們nginx鏡像中的路徑不一致,這樣子在啟動鏡像創(chuàng)建容器的時候就無法用掛載的方法覆蓋掉容器中的路徑

[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;
  #}
}

大家注意了,這里我修改了一下原來默認配置文件里面的內(nèi)容,在上面的其中一個location中,我把nginx默認首頁index改成了1.html,1.html是我自己創(chuàng)建的首頁名

在html文件夾下創(chuàng)建1.html首頁文件,并且編寫屬于自己的首頁,這里我是用notepadd++在windows上面寫好了1.html文件再通過工具拷過去linux系統(tǒng)里面的,注意有中文的可能要轉(zhuǎn)換下編碼,不然可能會亂碼,例如我這里用的是ansi的編碼

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

現(xiàn)在是創(chuàng)建容器并且掛載文件和文件夾了

[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,這個路徑在第二步里面已經(jīng)找出來了

docker ps 查看有沒有啟動成功

[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再啟動容器,如果在確保掛載的目錄和文件沒有問題還是不能啟動的話,那么就是權(quán)限問題了,網(wǎng)上說的就是在docker run后面加個 --privileged=true參數(shù)

http://IP 打開網(wǎng)頁看看效果把

以上這篇docker部署nginx并且掛載文件夾和文件操作就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • nginx如何根據(jù)報文里字段轉(zhuǎn)發(fā)至不同地址

    nginx如何根據(jù)報文里字段轉(zhuǎn)發(fā)至不同地址

    要在 Nginx 中根據(jù) POST 請求的 JSON 負載中的 id 字段的值進行轉(zhuǎn)發(fā),你可以使用 Nginx 的 ngx_http_lua_module 模塊,這個模塊允許你在 Nginx 配置中使用 Lua 腳本,本文介紹nginx如何根據(jù)報文里字段轉(zhuǎn)發(fā)至不同地址,感興趣的朋友一起看看吧
    2024-12-12
  • Nginx添加ngx-fancyindex模塊的方法

    Nginx添加ngx-fancyindex模塊的方法

    這篇文章主要介紹了Nginx添加ngx-fancyindex模塊的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-06-06
  • nginx 負載均衡的三種參數(shù)設(shè)置

    nginx 負載均衡的三種參數(shù)設(shè)置

    這篇文章主要介紹了nginx 負載均衡的三種參數(shù)設(shè)置,需要的朋友可以參考下
    2017-07-07
  • Nginx中l(wèi)ocation匹配以及rewrite重寫跳轉(zhuǎn)詳解

    Nginx中l(wèi)ocation匹配以及rewrite重寫跳轉(zhuǎn)詳解

    訪問重寫 rewrite 是 Nginx HTTP 請求處理過程中的一個重要功能,下面這篇文章主要給大家介紹了Nginx中l(wèi)ocation匹配以及rewrite重寫跳轉(zhuǎn)的相關(guān)資料,需要的朋友可以參考下
    2022-03-03
  • 詳解使用Nginx和uWSGI配置Python的web項目的方法

    詳解使用Nginx和uWSGI配置Python的web項目的方法

    這篇文章主要介紹了使用Nginx和uWSGI配置Python的web項目的方法,與其他CGI連接方式相比uwsgi的連接性能也較為出眾,需要的朋友可以參考下
    2015-12-12
  • nginx源碼分析configure腳本詳解

    nginx源碼分析configure腳本詳解

    這篇文章主要介紹了nginx源碼分析configure腳本詳解的相關(guān)資料,需要的朋友可以參考下
    2017-05-05
  • Nginx、Tomcat等項目部署問題以及解決流程

    Nginx、Tomcat等項目部署問題以及解決流程

    本文總結(jié)了項目部署中常見的 four 類問題及其解決方法:Nginx 未按預(yù)期顯示結(jié)果、端口未開啟、日志分析的重要性以及開發(fā)環(huán)境與生產(chǎn)環(huán)境運行結(jié)果不一致的問題,通過提供詳細的解決方案和思路,希望開發(fā)者能夠更好地應(yīng)對部署過程中的挑戰(zhàn),確保項目順利上線
    2024-12-12
  • nginx配置https加密訪問的詳細教程

    nginx配置https加密訪問的詳細教程

    這篇文章主要介紹了nginx配置https加密訪問的詳細教程,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-11-11
  • Nginx配置實現(xiàn)用IP灰度測試(不同用戶ID)

    Nginx配置實現(xiàn)用IP灰度測試(不同用戶ID)

    本文主要介紹了使用Nginx配置實現(xiàn)基于IP的灰度發(fā)布實驗,以及如何通過不同用戶ID測試灰度發(fā)布,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2024-11-11
  • 修改nginx站點根目錄總結(jié)經(jīng)驗(小結(jié))

    修改nginx站點根目錄總結(jié)經(jīng)驗(小結(jié))

    這篇文章主要介紹了修改nginx站點根目錄總結(jié)經(jīng)驗(小結(jié)),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-06-06

最新評論