NGINX dockerfile与deployment配置

案例1:

FROM centos:7
LABEL maintainer   example
RUN useradd  www -u 1200 -M -s /sbin/nologin
RUN mkdir -p /var/log/nginx
RUN yum install -y cmake pcre pcre-devel openssl openssl-devel gd-devel 
    zlib-devel gcc gcc-c++ net-tools iproute telnet wget curl &&
    yum clean all && 
    rm -rf /var/cache/yum/*
RUN wget nginx-1.16.1.tar.gz
RUN tar xf nginx-1.16.1.tar.gz
WORKDIR nginx-1.16.1
RUN ./configure --prefix=/usr/local/nginx --with-http_image_filter_module --user=www --group=www 
    --with-http_ssl_module --with-http_v2_module --with-http_stub_status_module 
    --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log 
    --pid-path=/var/run/nginx/nginx.pid
RUN make -j 4 && make install && 
    rm -rf /usr/local/nginx/html/*  && 
    echo "leilei hello" >/usr/local/nginx/html/index.html  && 
    rm -rf nginx* && 
    ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime &&
    ln -sf /dev/stdout /var/log/nginx/access.log && 
    ln -sf /dev/stderr /var/log/nginx/error.log
RUN chown -R www.www /var/log/nginx
ENV LOG_DIR /var/log/nginx
ENV PATH $PATH:/usr/local/nginx/sbin
#COPY nginx.conf /usr/local/nginx/conf/nginx.conf
EXPOSE 80
WORKDIR /usr/local/nginx
CMD ["nginx","-g","daemon off;"]

案例2:

FROM centos:7
LABEL maintainer example
RUN yum install -y openssl && 
useradd  www -u 1000 -M -s /sbin/nologin && 
rm -fr /etc/yum.repos.d/* && 
rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm && yum clean all && 
yum install -y nginx-1.18.0-2.el7.ngx.x86_64  &&  yum clean all &&  rm -rf /var/cache/yum/* && 
ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && 
ln -sf /dev/stdout /var/log/nginx/access.log && 
ln -sf /dev/stderr /var/log/nginx/error.log && 
sed -i "1,3s#nginx#www#g" /etc/nginx/nginx.conf
EXPOSE 80
EXPOSE 443
WORKDIR /etc/nginx
CMD ["nginx","-g","daemon off;"]

案例3:

FROM  centos7-openresty-1.19-base:20210429
# 基于官方centos7精简版基础镜像,添加中文支持
ENV APP_BASE_HOME="/opt" 
ENV APP_HOME="${APP_BASE_HOME}"
ENV LANG zh_CN.UTF-8
ENV LC_ALL zh_CN.UTF-8
WORKDIR $APP_HOME

RUN rm -rf  /opt/openresty/nginx/conf
RUN rm -rf   /opt/openresty/nginx/lua
RUN cp /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup
#COPY  *.repo /etc/yum.repos.d/
ADD  ./conf   /opt/openresty/nginx/conf/
ADD  ./lua  /opt/openresty/nginx/lua/
RUN yum install wget telnet curl -y 
RUN wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
RUN wget -O /etc/yum.repos.d/epel-7.repo  http://mirrors.aliyun.com/repo/epel-7.repo
# 安装中文包
RUN yum install -y kde-l10n-Chinese
# 重新安装glibc-common
RUN yum -y reinstall glibc-common
# 编译生成语言库
RUN localedef -c -f UTF-8 -i zh_CN zh_CN.utf8
# 设置语言默认值为中文,时区改为东八区
RUN echo 'LANG="zh_CN.UTF-8"' > /etc/locale.conf

RUN chmod -R 777 /opt/openresty/nginx/sbin/*
EXPOSE 80

CMD ["/opt/openresty/nginx/sbin/nginx", "-g" ,"daemon off;"]

流水线配置文件deployment.yml

apiVersion: v1
kind: ConfigMap
metadata:
  name: __APPNAME__-config
data:
  SERVERNAME_HOST: "test.1234.com"
---
apiVersion: apps/v1  # for versions before 1.8.0 use apps/v1beta1
kind: Deployment
metadata:
  name: __APPNAME__-deployment
  labels:
    app: __APPNAME__
spec:
  strategy:
    type: RollingUpdate
    rollingUpdate:
      maxSurge: 1
      maxUnavailable: 0
  replicas: 2
  selector:
    matchLabels:
      app: __APPNAME__
  template:
    metadata:
      labels:
        app: __APPNAME__
    spec:
      containers:
        - name: __APPNAME__
          image: __PRE_PROD_IMAGE__
          imagePullPolicy: Always
          envFrom:
            - configMapRef:
                name: __APPNAME__-config
          ports: 
            - containerPort: 80
            - containerPort: 22
---
apiVersion: v1
kind: Service
metadata:
  name: __APPNAME__
spec:
  ports:
    - port: 80
      protocol: TCP
      targetPort: 80
  selector:
    app: __APPNAME__
---
apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  annotations:
    nginx.ingress.kubernetes.io/proxy-connect-timeout: "20"
    nginx.ingress.kubernetes.io/proxy-send-timeout: "20"
    nginx.ingress.kubernetes.io/proxy-read-timeout: "20"
    nginx.ingress.kubernetes.io/connection-proxy-header: "keep-alive"
    nginx.ingress.kubernetes.io/proxy-http-version: "1.1"
    nginx.ingress.kubernetes.io/proxy-body-size: 80m
  name: __APPNAME__
spec:
  rules:
    - host: test.1234.com
      http:
        paths:
        - path: /
          backend:
            serviceName: __APPNAME__
            servicePort: 80

流水线脚本:

# input your command here
pwd
ls -la

echo "##########################################"
sed -i "s/__APPNAME__/${APPNAME}/g" $(grep __APPNAME__  -rl  deploy/)
PUBLIC_PRE_PROD_IMAGE=registry.cn-beijing.aliyuncs.com/test/testrepository:${APPTAG}
echo "tag:" ${APPTAG}
echo "img:" ${PUBLIC_PRE_PROD_IMAGE}
sed -i "s|__PRE_PROD_IMAGE__|${PUBLIC_PRE_PROD_IMAGE}|g" $(grep __PRE_PROD_IMAGE__ -rl  deploy/)
cat deploy/test/app/deployment.yaml

 参考:

https://www.cnblogs.com/superlinux/p/12581742.html

原文地址:https://www.cnblogs.com/sfnz/p/14744966.html