(學習到到實踐)六、docker自定義nginx/openresty

前言

爲什麼要使用openresty?

官方說明:OpenResty® 是一個基於 Nginx 與 Lua 的高性能 Web 平臺,其內部集成了大量精良的 Lua 庫、第三方模塊以及大多數的依賴項。所以。
但openresty官方沒有發佈docker相關東西,所以以:結合openresty安裝、參考docker-nginx官方的爲原則編寫。

1.依賴安裝測試

官方聲明依賴:perl 5.6.1+, libpcre, libssl

a.按nginx的alpine3.9測試

# perl查找
/ # apk search perl5
perl-5.26.3-r0  #就一個可選

/ # apk search libpcre
libpcre2-32-10.32-r1 #貌似最新版本了,32位的?
pcre-dev-8.42-r1
libpcre2-16-10.32-r1
pcre2-dev-10.32-r1
libpcre16-8.42-r1
libpcre32-8.42-r1
pcre2-10.32-r1
pcre-8.42-r1
libpcrecpp-8.42-r1

/ # apk search libssl
openssl-dev-1.1.1b-r1
libressl-dev-2.7.5-r0  #看起來最合適
nss-3.41-r0
libssl1.1-1.1.1b-r1
dovecot-2.3.6-r0
libressl2.7-libssl-2.7.5-r0

/ # apk add perl-5.26.3-r0 libpcre2-32-10.32-r1 libressl-dev-2.7.5-r0
# 完全報錯,這個alpine依賴搞不了,官方https://pkgs.alpinelinux.org/包的搜索頁捉襟見肘,不得不放棄“小而美”。

b.容器測試

參考官方選則debian,github上構建平臺鏡像的許多鏡像選擇stretch-slim精簡版的,看下大小最新的只有55.3M,比較滿意。
環境安裝測試:

[]:~/tmp/dk/openresty# docker run -itd --name df -v /root/tmp/dk/openresty:/tmp/host debian:stretch-slim
[]:~/tmp/dk/openresty# docker exec df -it /bin/bash

初始安裝清華源,切換百度源(雲本機),apt-get update超慢,後迴歸官源,後期卡死用網易源。
問題:
1、./configure: error: ngx_postgres addon was unable to detect version of the libpq library.

apt-get libpq-dev -y

2、./configure: error: the HTTP gzip module requires the zlib library.

apt-get install openssl libssl-dev libperl-dev -y
apt-get install zlib1g-dev -y

到此OK。

c.第一步通過的代碼

FROM debian:stretch-slim

#MAINTAINER 維護者信息
MAINTAINER [email protected]
ENV RESOURCE "\
deb http://mirrors.163.com/debian/ stretch main non-free contrib \n\
deb http://mirrors.163.com/debian/ stretch-updates main non-free contrib \n\
deb http://mirrors.163.com/debian/ stretch-backports main non-free contrib \n\
deb-src http://mirrors.163.com/debian/ stretch main non-free contrib \n\
deb-src http://mirrors.163.com/debian/ stretch-updates main non-free contrib \n\
deb-src http://mirrors.163.com/debian/ stretch-backports main non-free contrib \n\
deb http://mirrors.163.com/debian-security/ stretch/updates main non-free contrib \n\
deb-src http://mirrors.163.com/debian-security/ stretch/updates main non-free contrib \
"
ENV VERSION 1.15.8.1

# !!!注意字符串變量、轉義自動解析
RUN echo $RESOURCE > /etc/apt/sources.list && cat /etc/apt/sources.list \
    && apt-get update \
    && apt-get install libpcre3-dev libssl-dev perl make build-essential curl \
        -y

COPY openresty-$VERSION.tar.gz /tmp/openresty.tar.gz
RUN groupadd -r openresty && useradd -r -g openresty openresty \
    # && curl  \
    && mkdir -p /usr/src && cd /usr/src && mv /tmp/openresty.tar.gz ./ \
    && tar -zxf openresty.tar.gz --strip-components 1 \
    && ./configure \
           --prefix=/usrl/ocal/openresty \
           --with-luajit \
           --without-http_redis2_module \
           --with-http_iconv_module \
           --with-http_postgres_module \
    \
    && make -j "$(nproc)" \
    && make install \
    && rm -rf /usr/src
# COPY nginx.conf /etc/nginx/nginx.conf
# COPY nginx.vh.default.conf /etc/nginx/conf.d/default.conf
# COPY vhost.conf /etc/nginx/conf.d/vhost.conf
VOLUME ["/etc/nginx","/var/www/html"]

EXPOSE 80
STOPSIGNAL SIGTERM

# CMD ["nginx", "-g", "daemon off;"] #注意官方提示

2.運行測試

準備配置數據(之前舊配置),注意 html和nginx文件夾 的參數共享:

[]:~/tmp/dk/openresty# tree ./
./
├── config
│   ├── conf.d
│   │   └── nginx.vh.default.conf
│   │   └── vhost.conf
│   └── nginx.conf
└── html

a.測試啓動

測試容器df(手動搭建,系統無法ps),運行正常:

root@df08a646aaa0:/# nginx
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)

但Dockerfile所建鏡像的容器啓動關閉無日誌,更換openresty官方配置:

[]:~/tmp/dk/openresty# docker run -itd --name n1 -v /root/tmp/dk/openresty/config:/etc/nginx \
    -v /root/tmp/dk/openresty/html:/var/www/html cffycls/openresty:0.9 \
     /usrl/ocal/openresty/nginx/sbin/nginx 
#無法啓動,容器運行終止,無日誌。應該是配置文件問題

b.修改配置文件

使用官方原包未修改:

# 官包解壓路徑:./bundle/nginx-1.15.8/conf/nginx.conf
[]:~/tmp/dk/openresty# tree conf
conf
├── fastcgi.conf
├── fastcgi_params
├── koi-utf
├── koi-win
├── mime.types
├── nginx.conf
├── scgi_params
├── uwsgi_params
└── win-utf

去掉run加的命令,加上端口,運行Up了。

c.運行測試

CMD ["/usrl/ocal/openresty/nginx/sbin/nginx", "-g", "daemon off;"]
[]:~/tmp/dk/openresty# docker run -itd --name n1 -p 80:80 \
    -v /root/tmp/dk/openresty/conf:/usrl/ocal/openresty/nginx/conf \
    -v /root/tmp/dk/openresty/html:/usrl/ocal/openresty/nginx/html \
    openresty/openresty:1.0

運行成功,大小357M,太大了感覺,提交到雲端。

3.啓動官方鏡像

提交到雲端,偶然想搜索下有沒有,docker-hub竟然(反問)有官方鏡像,pull了個下來,144M還不錯。真是重複造輪子了!!!
折騰半天,算是個教訓了,算是測得到了運行方法,結果也還行。

[]:~/tmp/dk/openresty# docker run -itd --name n1 -p 80:80 \
    -v /root/tmp/dk/openresty/conf:/usrl/local/openresty/nginx/conf \
    -v /root/tmp/dk/openresty/html:/usr/local/openresty/nginx/html \
    openresty/openresty

訪問web端正常

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章