docker制作nginx带upsync模块镜像

 Dockerfile

FROM alpine:latest
ADD nginx-1.21.6.tar.gz /home/
ADD pcre-8.44.tar.gz /home/    
ADD openssl-1.0.2q.tar.gz /home/
ADD zlib.tar.gz /home/
ADD v2.1.3.tar.gz /home/
RUN echo http://mirrors.aliyun.com/alpine/v3.10/main/ > /etc/apk/repositories && \
    echo http://mirrors.aliyun.com/alpine/v3.10/community/ >> /etc/apk/repositories
RUN apk update && apk upgrade && \
    apk add  gcc g++ make && \
    addgroup -S nginx && \
    adduser -DHS -s /sbin/nologin -G nginx nginx && \
    cd /home/nginx-1.21.6 && \
    ./configure --prefix=/usr/local/nginx --with-pcre=/home/pcre-8.44 --with-openssl=/home/openssl-1.0.2q --with-zlib=/home/zlib-1.3 --without-http_gzip_module --add-module=/home/nginx-upsync-module-2.1.3 && \
    make && make install && \
    ln -s /usr/local/nginx/sbin/nginx /usr/sbin/ && \
    rm -rf /home/nginx-1.21.6 && \
    rm -rf /home/pcre-8.44 && \
	rm -rf /home/openssl-1.0.2q && \
	rm -rf /home/zlib-1.3 && \
	rm -rf /home/nginx-upsync-module-2.1.3 && \
    apk del gcc && \
    apk del g++ && \
    rm -rf /apk/repositories && \
    mkdir -p /usr/local/nginx/conf/vhost/
EXPOSE 80
CMD ["/usr/sbin/nginx","-g","daemon off;"]

docker build -t nginx:alpine .

docker run -d --name vcupsync -p 88:80 nginx:alpine

最好是让自己的网络可以访问外网这样apk update && apk upgrade时基本不会出现网络问题导致失败

这种就是网络问题导致的失败

所需资源包https://download.csdn.net/download/qq_30920479/88750435

docker build -t nginx:upsync .
docker save -o nginx-upsync.tar nginx

资源包里面已包含打好的docker镜像包,直接下载load到docker容器就可以使用
docker load < nginx-upsync.tar

 

docker-compose.yml启动

version: '3.3'
services:
      nginx:
        #image: nginx
        image: nginx:upsync
        restart: always
        hostname: nginx
        container_name: nginx
        privileged: true
        ports:
          - "80:80"
          - "443:443"
        volumes:
          - ./nginx/conf/:/etc/nginx/
          - ./nginx/cert/:/etc/cert/
          - ./nginx/www/:/usr/share/nginx/html/
          - ./nginx/logs/:/var/log/nginx/

上传到nexus私库

docker tag nginx:alpine 192.168.99.182:8082/nginx:alpine


docker login -u username -p password 192.168.99.182:8082

docker push 192.168.99.182:8082/nginx:alpine

由于是简化版alpine 

docker images

docker exec -it 34738f7c2627 /bin/sh

docker-compose部署测试

nginx:
  image: 192.168.99.182:8082/nginx:alpine
  container_name: nginx
  volumes:
    - /home/data/nginx/conf:/usr/local/nginx/conf
    - /home/data/nginx/logs:/var/log/nginx
    - /home/data/nginx/html:/usr/local/nginx/html
    - /home/data/nginx/proxy_temp:/usr/local/nginx/proxy_temp
  ports:
    - 80:80
    - 443:443
    - 5885:5885


#user  nobody;
worker_processes  1;

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

    
	
	upstream swoole_test {
		#ip_hash;
        server 192.168.99.203:8060;
        #server 192.168.100.27:80;
        #server 192.168.100.29:80;
        upsync 192.168.99.115:8500/v1/kv/upstreams/swoole_test upsync_timeout=6m upsync_interval=500ms upsync_type=consul strong_dependency=off;
        
        upsync_dump_path /usr/local/nginx/conf/vhost/swoole_test.conf;
        #include /www/server/nginx/conf/vhost/servers_skuprice.conf;
     }
     
	server
    {
         listen 5885;
         #server_name 10.10.1.57;
		 #listen       80;
        server_name  localhost;
         location / {
          proxy_pass         http://swoole_test; 
          proxy_set_header   Host             $host; 
          proxy_set_header   X-Real-IP        $remote_addr; 
          proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for; 
          # 显示具体负载的机器的ip,X-Route-Ip随便命名
          add_header X-Route-Ip $upstream_addr;
	        add_header X-Route-Status $upstream_status;
            # 文件不能下载 设置缓存
            proxy_buffering on;
            proxy_buffer_size 512k;
            proxy_buffers 2 512k;
            proxy_busy_buffers_size 512k;
            proxy_temp_path /usr/local/nginx/proxy_temp 1 2;
            proxy_max_temp_file_size 100M;
            proxy_temp_file_write_size 512k;
            # 文件不能下载结束
        }
    }

}

第二种方法

这种方式跟官方镜像的配置目录有差异,要跟官方的配置文件目录一致,可以使用yum安装的方式,先拉取一个centeros启动centeros进入容器,yum安装nginx,然后使用docker commit命令制作镜像

1.2 使用镜像创建容器并进入容器

拉取

docker pull centos:latest

创建容器并进入容器

# docker run -it --name centos.nginx centos /bin/bash

设置源

cd /etc/yum.repos.d/

sed -i 's/mirrorlist/#mirrorlist/g' /etc/yum.repos.d/CentOS-*
sed -i 's|#baseurl=http://mirror.centos.org|baseurl=http://vault.centos.org|g' /etc/yum.repos.d/CentOS-*


yum makecache

yum update -y

安装nginx





 yum install yum-utils //先决条件,这个可以不用管,官网有提
 yum install nginx -y

查看nginx详情
nginx -v

退出容器,但不要删除容器

查看容器获取ID       

docker ps -a
在这里插入图片描述
制作镜像

复制命令时注意中文问题,踩过的坑,大家不要再跳了

docker commit -m "nginx" -a "centos.nginx" -c "CMD ["/usr/sbin/nginx"]" 49c5d69bbdc4 centos-nginx

编译安装

cd /opt 
wget https://github.com/weibocom/nginx-upsync-module/archive/master.zip
yum install zip
unzip master

wget http://nginx.org/download/nginx-1.20.1.tar.gz 

tar xf nginx-1.20.1.tar.gz && cd nginx-1.20.1

yum -y install libxml2 libxml2-dev libxslt-devel

yum -y install gd-devel

yum -y install perl-devel perl-ExtUtils-Embed

yum -y install GeoIP GeoIP-devel GeoIP-data

yum -y install pcre-devel

yum -y install openssl openssl-devel

yum -y install gcc

yum install make


检查模块是否支持,比如这次添加 limit 限流模块 和 stream 模块:



./configure --help | grep limit

./configure –help | grep stream


加上本次需新增的模块:--with-stream

cd /opt/nginx-1.20.1
./configure --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_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 -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie' --add-module=/opt/nginx-upsync-module-master


cp /opt/nginx-1.12.2/objs/nginx /usr/sbin/ 



nginx -s reload


7.检查


nginx -V

第三种方法

新版本nginx支持动态加载模块

--with-stream=dynamic
load_module modules/ngx_http_add_header_module.so;

http {
    server {
        listen 80;
        server_name myweb.com;
        location / {
            add_header MyHeader "Hello Nginx!";
            root /var/www/html;
        }
    }
}

其实本质就是编译生成的nginx可执行文件镜像一般放在/usr/sbin/nginx,但是正常编译时一般放在/usr/local/nginx,所以如果缺少包需要重新编译生成nginx文件,其它是配置加载的路径,正常编译配置加载路径在编译文件生成的路劲下,但是镜像里面是加载的/etc/nginx/conf和/usr/share/nginx/html/及/var/log/nginx/以及/etc/cert/(其实如果另外的电动不想再编译安装,把可执行nginx文件及配置复制到新电脑上是可以运行的,当然前提是两台电脑系统和环境一样,你搞个一台windows一台nginx或者系统版本及环境差异太大肯定是GG)

推荐使用第一种,简化版linux,centeros太大了

  • 8
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值