(学习到实践)六、docker自定义nginx/openresty

前言

为什么要使用openresty?

官方说明:OpenResty® 是一个基于 Nginx 与 Lua 的高性能 Web 平台,其内部集成了大量精良的 Lua 库、第三方模块以及大多数的依赖项。所以。
但openresty官方没有发布docker相关东西,所以以:结合openresty安装、参考docker-nginx官方的为原则编写。

1.依赖安装测试

官方声明依赖:perl 5.6.1+, libpcre, libssl

a.按nginx镜像测试

# 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端正常。

你可能感兴趣的:(dockerfile,nginx,openresty)