Docker 之 Nginx環境搭建

ZACSNZ發表於2018-07-15

Docker搭建微服務自動部署 <架構探險之路>,讓我們來了解下Docker中如何安裝、使用nginx吧!


Docker 之 Nginx環境搭建

Nginx 安裝教程

Linux 環境

Linux 中安裝

  • 安裝編譯工具及庫檔案

    yum -y install make zlib zlib-devel gcc-c++ libtool  openssl openssl-devel
    複製程式碼
  • 首先要安裝 PCRE

    PCRE 作用是讓 Nginx 支援 Rewrite 功能

    若提示無wget則安裝(在 Docker 拉取的源 centos 中預設是沒安裝的)

    yum -y install wget
    複製程式碼

    進入程式安裝目錄

    cd /usr/local/src/
    複製程式碼
    • 下載pcre安裝包

      wget http://downloads.sourceforge.net/project/pcre/pcre/8.35/pcre-8.35.tar.gz
      複製程式碼
    • 解壓安裝包:

      tar -zxvf pcre-8.35.tar.gz
      複製程式碼
    • 刪除安裝包

      rm -rf pcre-8.35.tar.gz
      複製程式碼
    • 進入安裝包

      cd pcre-8.35
      複製程式碼
    • 編譯安裝

      ./configure
      make && make install
      複製程式碼
    • 檢視pcre版本

      preconv --version
      複製程式碼
      [root@de790c02f7b6 pcre-8.35]# preconv --version
      GNU preconv (groff) version 1.22.2 with iconv support
      複製程式碼
  • 安裝 Nginx

    • 下載安裝包

      wget http://nginx.org/download/nginx-1.6.2.tar.gz
      複製程式碼
    • 安裝

       cd nginx-1.8.0
       ./configure --prefix=/usr/local/nginx
       make
       make install
      複製程式碼
    • 檢查安裝結果

        /usr/local/nginx/sbin/nginx -v
      複製程式碼

Dockerfile 安裝

指令碼

FROM centos:latest
## 製作者資訊
MAINTAINER xiachaoyang xiazhaoyang@live.com
RUN yum install -y pcre-devel wget net-tools gcc zlib zlib-devel make openssl-devel
## 建立目錄
RUN mkdir -p /usr/local/nginx
## 本地安裝 直接自動解壓
## ADD nginx-1.8.0.tar.gz /usr/local/src
## 線上安裝
RUN cd /usr/local/src
ADD http://nginx.org/download/nginx-1.8.0.tar.gz .
RUN tar -zxvf nginx-1.8.0.tar.gz
RUN ln -s /usr/local/src /componets
RUN cd nginx-1.8.0 && ./configure --prefix=/usr/local/nginx && make && make install
RUN rm -vf /usr/local/nginx/conf/nginx.conf
ADD http://www.apelearn.com/study_v2/.nginx_conf /usr/local/nginx/conf/nginx.conf
EXPOSE 80
# 設定啟動時執行命令
#ENTRYPOINT /usr/local/nginx/sbin/nginx -v
複製程式碼

映象

docker build -t env-nginx . docker images docker tag [映象ID] env-nginx:1.0 docker run -i -t -p 80:80 env-nginx:1.0

清空 Nginx Docker

docker rm -f docker container ls -a -q docker rmi -f env-nginx:2.0 docker build -t env-nginx:2.0 . docker run -i -t -p 8001:80 --name nginx --restart=always env-nginx:2.0

執行後執行nginx

xiazhaoyangdeMacBook-Pro:~ xiazhaoyang$ docker run -i -t -p 8001:80 --restart=always 127.0.0.1:5000/env-nginx:1.0
[root@56dfba762876 /]# cd usr/local/nginx/sbin/
[root@56dfba762876 sbin]# ls
nginx
[root@56dfba762876 sbin]# ./nginx -v    
nginx version: nginx/1.8.0
[root@56dfba762876 sbin]# ps -ef|grep nginx
root        18     1  0 07:44 pts/0    00:00:00 grep --color=auto nginx
[root@56dfba762876 sbin]# ./nginx
[root@56dfba762876 sbin]# ps -ef|grep nginx
root        20     1  0 07:45 ?        00:00:00 nginx: master process ./nginx
nobody      21    20  0 07:45 ?        00:00:00 nginx: worker process
nobody      22    20  0 07:45 ?        00:00:00 nginx: worker process
複製程式碼

配置路由

  • 關閉nginx

    ./nginx -s stop

    xiazhaoyangdeMacBook-Pro:~ xiazhaoyang$ docker ps
    CONTAINER ID        IMAGE                          COMMAND                  CREATED             STATUS              PORTS                    NAMES
    56dfba762876        127.0.0.1:5000/env-nginx:1.0   "/bin/bash"              10 minutes ago      Up 10 minutes       0.0.0.0:8001->80/tcp     brave_kowalevski
    d75f05b4d7f8        hyper/docker-registry-web      "start.sh"               5 days ago          Up About an hour    0.0.0.0:8000->8080/tcp   local-docker-res-web
    1b5239bc943b        registry                       "/entrypoint.sh /etc…"   5 days ago          Up About an hour    0.0.0.0:5000->5000/tcp   local-docker-res
    xiazhaoyangdeMacBook-Pro:~ xiazhaoyang$ docker attach 56dfba762876
    [root@56dfba762876 sbin]# ls
    nginx
    [root@56dfba762876 sbin]# cd ..
    [root@56dfba762876 nginx]# ls
    client_body_temp  conf  fastcgi_temp  html  logs  proxy_temp  sbin  scgi_temp  uwsgi_temp
    [root@56dfba762876 nginx]# cd c
    client_body_temp/ conf/             
    [root@56dfba762876 nginx]# cd conf/
    [root@56dfba762876 conf]# ls
    fastcgi.conf          fastcgi_params          koi-utf  mime.types          nginx.conf          scgi_params          uwsgi_params          win-utf
    fastcgi.conf.default  fastcgi_params.default  koi-win  mime.types.default  nginx.conf.default  scgi_params.default  uwsgi_params.default
    [root@56dfba762876 conf]# cd ../sbin/             
    [root@56dfba762876 sbin]# ./nginx -s stop
    [root@56dfba762876 sbin]# ps -ef|grep nginx
    root        30     1  0 07:55 pts/0    00:00:00 grep --color=auto nginx
    複製程式碼
  • 修改路由配置

    vi nginx.config

    [root@56dfba762876 conf]# cd ../sbin/
    [root@56dfba762876 sbin]# ./nginx
    [root@56dfba762876 sbin]# cd ..
    [root@56dfba762876 nginx]# cd conf/
    [root@56dfba762876 conf]# vi nginx.conf
    [root@56dfba762876 conf]# cd ../sbin/
    [root@56dfba762876 sbin]# ./nginx -s stop
    [root@56dfba762876 sbin]# ./nginx        
    [root@56dfba762876 sbin]#   
    
    server
    {
        listen 80;
        server_name localhost;
        index index.html index.htm index.php;
        root /usr/local/nginx/html;
    
        location ~ \.php$ {
            include fastcgi_params;
            fastcgi_pass unix:/tmp/php-fcgi.sock;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name;
        }
    
       location /docker-registry-web/ {
                    proxy_pass   http://192.168.2.102:8000/;
                    proxy_redirect default;
            }
       location /jenkins/ {
                    proxy_pass   http://192.168.2.102:8002/;
                    proxy_redirect default;
            }
    
    }
    複製程式碼

jenkins通過nginx對映效果

輸入圖片說明
螢幕截圖.png

  上圖靜態資源拉取失敗原因:容器內部埠訪問不通過本地主機,直接訪問資源無法獲取,具體原因後續跟進...
複製程式碼

未完待續....

相關文章