docker-nginx-容器化

雷硕
2023-12-01

一.开启测试容器

选定基础镜像:harbor.test.cn/share_x86/centos:7
docker 官网直接获取 centos:7 基础镜像

二.准备源码包

wget http://nginx.org/download/nginx-1.16.1.tar.gz
git clone https://github.com/Austinb/nginx-upload-module // 文件上传模块
git clone https://github.com/yaoweibin/nginx_upstream_check_module.git //j健康检查模块

三.进入容器编译安装

Dockerfile-t


FROM harbor.test.cn/share_arm/centos:7
 
LABEL maintainer="test@test.cn"
  
ENTRYPOINT ["sh","-c","sleep 360000"]
# docker build -t harbor.test.cn/share_arm/centos:7-t -f Dockerfile-t .

run-t.sh

docker stop nginx-t
docker rm nginx-t
docker run --name nginx-t \
    -v /data/docker-nginx:/data/nginx \
    -d harbor.test.cn/share_arm/centos:7-t
 
docker exec -it nginx-t bash

开始编译安装

 安装依赖包:
yum -y install libxml2 libxml2-devel libxslt-devel  gd-devel  perl-devel perl-ExtUtils-Embed gperftools
yum -y install openssl openssl-devel autoconf automake zlib-devel libxml2 \
libxml2-devel libxslt-devel gd-devel perl-devel perl-ExtUtils-Embed GeoIP GeoIP-devel GeoIP-data
yum -y install gcc gcc-c++  //独立安装,包大下下载慢
  
mkdir /opt/nginx
cd /opt/nginx
wget http://nginx.org/download/nginx-1.16.1.tar.gz
git clone https://github.com/Austinb/nginx-upload-module
tar zxvf nginx-1.16.1.tar.gz
cd nginx-1.16.1
./configure --prefix=/opt/nginx-1.16.1 \
--sbin-path=/opt/nginx-1.16.1/sbin/nginx \
--conf-path=/opt/nginx-1.16.1/nginx.conf \
--modules-path=/opt/nginx-1.16.1/modules \
--error-log-path=/opt/nginx-1.16.1/log/error.log \
--http-log-path=/opt/nginx-1.16.1/log/access.log \
--http-client-body-temp-path=/opt/nginx-1.16.1/tmp/client_body \
--http-proxy-temp-path=/opt/nginx-1.16.1/tmp/proxy \
--http-fastcgi-temp-path=/opt/nginx-1.16.1/tmp/fastcgi \
--http-uwsgi-temp-path=/opt/nginx-1.16.1/tmp/uwsgi \
--http-scgi-temp-path=/opt/nginx-1.16.1/tmp/scgi \
--pid-path=/opt/nginx-1.16.1/run/nginx.pid \
--lock-path=/run/lock/subsys/nginx \
--user=nginx --group=nginx \
--with-ipv6 \
--with-http_ssl_module \
--with-http_v2_module \
--with-http_realip_module \
--with-stream_ssl_preread_module \
--with-http_addition_module \
--with-http_xslt_module=dynamic \
--with-http_image_filter_module=dynamic \
--with-http_sub_module \
--with-http_dav_module \
--with-http_flv_module \
--with-http_mp4_module \
--with-http_gunzip_module \
--with-http_gzip_static_module \
--with-http_random_index_module \
--with-http_secure_link_module \
--with-http_degradation_module \
--with-http_slice_module \
--with-http_stub_status_module \
--with-http_perl_module=dynamic \
--with-http_auth_request_module \
--with-mail=dynamic --with-mail_ssl_module \
--with-pcre \
--with-pcre-jit \
--with-stream=dynamic \
--with-stream_ssl_module \
--with-google_perftools_module \
--with-debug \
--add-module=/data/nginx/src/nginx-upload-module \
  
删除以下编译参数
 --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic' \
 --with-ld-opt='-Wl,-z,relro -Wl,-E'  \
 --with-file-aio \
make
make install
  
编译安装后执行以下操作:
useradd nginx -M -s /sbin/nologin
mkdir /opt/nginx-1.16.1/tmp
yum install -y gperftools-libs
./opt/nginx-1.16.1/sbin/nginx -t
 
cp /opt/nginx-1.16.1  .   //保留编译好的二进制包
容器内,修改nginx.conf 配置文件。

问题汇总:

问题1: ./configure: error: can not detect int size

编辑auto/types/sizeof文件,大概36行的位置( $CC 改为 gcc )

ngx_test="$CC $CC_TEST_FLAGS $CC_AUX_FLAGS

该为ngx_test="gcc $CC_TEST_FLAGS $CC_AUX_FLAGS

问题2:./configure: error: can not detect int size

size 问题,修改 auto/types/sizeof 文件,找到 ngx_size= 这一行,改为 ngx_size=4

修改完之后,再次configure

问题3:

./configure: error: can not define uint32_t

删除 --with-cc-opt= 编译参数

五.提取编译后安装包,重新打入镜像

nginx-1.16.1 --获取编译好的二进制包
修改 nginx.conf 默认的配置文件
修改主要配置:
daemon off; --前台启动
user root; --文本标注 uplaod 模块要求(暂时不考虑安全入侵问题)

daemon off;
user root;
worker_processes  4;
  
#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;
  
#pid        logs/nginx.pid;
  
  
events {
    worker_connections  1024;
}
  
  
http {
    include       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  logs/access.log  main;
  
    sendfile        on;
    #tcp_nopush     on;
  
    #keepalive_timeout  0;
    keepalive_timeout  65;
  
    #gzip  on;
  
    include /opt/nginx/conf.d/*.conf;
}

dockerfile

FROM harbor.test.cn/share_arm/centos:7
 
LABEL maintainer="test@test.cn"
  
# edit time zone
RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
 
COPY ./nginx-1.16.1 /opt/nginx-1.16.1
 
RUN useradd nginx -M -s /sbin/nologin && \
    ln -s /opt/nginx-1.16.1 /opt/nginx && \
    chown nginx:nginx -R /opt/nginx-1.16.1 /opt/nginx
 
 
ENV PATH /opt/nginx/sbin:$PATH
 
#USER nginx
CMD ["sh","-c","nginx "]
 
#ENTRYPOINT ["sh","-c","sleep 360000"]
# docker build -t harbor.test.cn/share_arm/nginx:1.16.1 -f Dockerfile .
# docker build -t harbor.test.cn/wenbenbiaozhu_x86/web -f Dockerfile .

启动命令模板

mkdir /opt/nginx-test/conf/ -p
mkdir /opt/nginx-test/nginx-log
  
docker run --name nginx-t \
    --network=aisearch \
    --restart always \
    -p 19080:19080 \
    -v /opt/nginx-test/conf/:/opt/nginx/conf.d/ \
    -v /opt/nginx-test/nginx-log:/opt/nginx/log \
    -d harbor.test.cn/share_arm/nginx:1.16.1

/opt/nginx-test/conf/nginx-demo.conf

测试配置文件

 server {
    listen 19085;
    server_name test.nginx.cn;
    index  index.html index.htm;
     
    location / {
        root /opt/nginx/html;
        
    }
    include /opt/nginx/conf.d/app.d/*.conf;
}

 类似资料: