環境1:html
環境2:nginx
目錄結構redis
├── nginx
│ ├── dist.zip # 自定義項目
│ ├── Dockerfile
│ ├── nginx-1.15.7.tar.gz
│ ├── nginx.conf
│ ├── openssl-1.1.1a.tar.gz
│ ├── pcre-8.42.tar.gz
│ ├── vhosts.conf
│ └── zlib-1.2.11.tar.gzdocker
└── service_nginx.ymlcentos
下載網絡
一、建立dockerfiledom
FROM centos:6 MAINTAINER xiangsikai ENV LANG en_US.UTF-8 ENV TZ=Asia/Shanghai RUN yum install sudo unzip -y RUN sudo yum update -y && \ sudo yum groupinstall -y 'Development Tools' && \ sudo yum install -y epel-release && \ sudo yum install -y perl perl-devel perl-ExtUtils-Embed libxslt libxslt-devel libxml2 libxml2-devel gd gd-devel GeoIP GeoIP-devel ADD nginx-1.15.7.tar.gz /home/root/ ADD pcre-8.42.tar.gz /home/root/ ADD zlib-1.2.11.tar.gz /home/root/ ADD openssl-1.1.1a.tar.gz /home/root/ RUN cd /home/root/nginx-1.15.7/ && ./configure --prefix=/usr/local/nginx \ --sbin-path=/usr/sbin/nginx \ --conf-path=/etc/nginx/nginx.conf \ --user=nginx \ --group=nginx \ --pid-path=/var/run/nginx.pid \ --lock-path=/var/run/nginx.lock \ --error-log-path=/var/log/nginx/error.log \ --http-log-path=/var/log/nginx/access.log \ --with-select_module \ --with-poll_module \ --with-threads \ --with-file-aio \ --with-http_ssl_module \ --with-http_v2_module \ --with-http_realip_module \ --with-http_addition_module \ --with-http_xslt_module=dynamic \ --with-http_image_filter_module=dynamic \ --with-http_geoip_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_auth_request_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-mail=dynamic \ --with-mail_ssl_module \ --with-stream=dynamic \ --with-stream_ssl_module \ --with-stream_realip_module \ --with-stream_geoip_module=dynamic \ --with-stream_ssl_preread_module \ --with-compat \ --with-pcre=../pcre-8.42 \ --with-pcre-jit \ --with-zlib=../zlib-1.2.11 \ --with-openssl=../openssl-1.1.1a \ --with-openssl-opt=no-nextprotoneg \ --with-debug && \ make && make install RUN sudo useradd nginx && sudo mkdir /etc/nginx/conf.d COPY nginx.conf /etc/nginx/nginx.conf COPY vhosts.conf /etc/nginx/conf.d/ ADD dist.zip /usr/local/nginx/html/ RUN unzip /usr/local/nginx/html/dist.zip -d /usr/local/nginx/html/ CMD ["nginx","-g","daemon off;"] EXPOSE 80
# 指定系統鏡像版本 FROM centos:6 # 指定管理員名稱 MAINTAINER xiangsikai # 添加變量,指定中文編碼 ENV LANG en_US.UTF-8 # 添加變量,同步系統時間 ENV TZ=Asia/Shanghai # 添加命令 RUN yum install sudo unzip -y # 添加命令 RUN sudo yum update -y && \ sudo yum groupinstall -y 'Development Tools' && \ sudo yum install -y epel-release && \ sudo yum install -y perl perl-devel perl-ExtUtils-Embed libxslt libxslt-devel libxml2 libxml2-devel gd gd-devel GeoIP GeoIP-devel # 添加文件 ADD nginx-1.15.7.tar.gz /home/root/ # 添加文件 ADD pcre-8.42.tar.gz /home/root/ # 添加文件 ADD zlib-1.2.11.tar.gz /home/root/ # 添加文件 ADD openssl-1.1.1a.tar.gz /home/root/ # 添加命令 RUN cd /home/root/nginx-1.15.7/ && ./configure --prefix=/usr/local/nginx \ --sbin-path=/usr/sbin/nginx \ --conf-path=/etc/nginx/nginx.conf \ --user=nginx \ --group=nginx \ --pid-path=/var/run/nginx.pid \ --lock-path=/var/run/nginx.lock \ --error-log-path=/var/log/nginx/error.log \ --http-log-path=/var/log/nginx/access.log \ --with-select_module \ --with-poll_module \ --with-threads \ --with-file-aio \ --with-http_ssl_module \ --with-http_v2_module \ --with-http_realip_module \ --with-http_addition_module \ --with-http_xslt_module=dynamic \ --with-http_image_filter_module=dynamic \ --with-http_geoip_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_auth_request_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-mail=dynamic \ --with-mail_ssl_module \ --with-stream=dynamic \ --with-stream_ssl_module \ --with-stream_realip_module \ --with-stream_geoip_module=dynamic \ --with-stream_ssl_preread_module \ --with-compat \ --with-pcre=../pcre-8.42 \ --with-pcre-jit \ --with-zlib=../zlib-1.2.11 \ --with-openssl=../openssl-1.1.1a \ --with-openssl-opt=no-nextprotoneg \ --with-debug && \ make && make install # 添加命令 RUN sudo useradd nginx && sudo mkdir /etc/nginx/conf.d # 添加文件 COPY nginx.conf /etc/nginx/nginx.conf # 添加文件 COPY vhosts.conf /etc/nginx/conf.d/ # 添加文件 ADD dist.zip /usr/local/nginx/html/ # 添加命令 RUN unzip /usr/local/nginx/html/dist.zip -d /usr/local/nginx/html/ # 啓動命令 CMD ["nginx","-g","daemon off;"] # 開放端口 EXPOSE 80
二、建立鏡像(nginx目錄下)tcp
docker build -t 192.168.1.81:5000/nginx:v1 .
三、上傳鏡像ide
docker push 192.168.1.81:5000/nginx:v1
四、建立 service_nginx.ymlui
version: '3.7' services: nginx: image: 192.168.1.81:5000/nginx:v1 ports: - 2008:80 networks: - networkce deploy: mode: replicated replicas: 2 update_config: parallelism: 1 delay: 10s failure_action: rollback order: start-first rollback_config: parallelism: 1 delay: 10s failure_action: rollback order: start-first volumes: - type: volume source: nfs-nginx_log target: /var/log/nginx volume: nocopy: true configs: - source: nginx_config target: /etc/nginx/nginx.conf - source: nginx_vhosts target: /etc/nginx/conf.d/vhosts.conf networks: networkce: driver: overlay volumes: nfs-nginx_log: driver: local driver_opts: type: "nfs" o: "addr=192.168.1.81,vers=4,soft,timeo=180,bg,tcp,rw" device: "192.168.1.81:/docker/service/zs/nginx/log" configs: nginx_config: file: /docker/service/zs/nginx/config/nginx.conf nginx_vhosts: file: /docker/service/zs/nginx/config/vhosts.conf
# 指定版本 version: '3.7' # 服務 services: # 指定服務名 nginx: # 指定使用鏡像 image: 192.168.1.81:5000/nginx:v1 # 指定開放端口 ports: - 2008:80 # 指定網絡 networks: - networkce # 管理容器 deploy: # 設置副本模式 mode: replicated # 副本數 replicas: 2 # 更新配置 update_config: # 每次更新數量 parallelism: 1 # 每次更新時間 delay: 10s # 更新失敗設置,rollback回滾 failure_action: rollback # 更新狀態,start-firest 更新同時疊加舊版本,以後刪除 order: start-first # 回滾配置 rollback_config: # 每次回滾數量 parallelism: 1 # 每次回滾時間 delay: 10s # 回滾失敗設置,rollback回滾 failure_action: rollback # 回滾狀態,start-firest 回滾同時疊加舊版本,以後刪除 order: start-first # 配置持久化數據 volumes: # 數據類型 - type: volume # 設置名稱 source: nfs-nginx_log # 掛載容器路徑 target: /var/log/nginx # 默認 volume: nocopy: true # 配置文件配置 configs: # 配置文件名稱 - source: nginx_config # 上傳容器文件路徑 target: /etc/nginx/nginx.conf # 配置文件名稱 - source: nginx_vhosts # 上傳容器文件路徑 target: /etc/nginx/conf.d/vhosts.conf # 網絡 networks: # 添加網絡名稱 networkce: driver: overlay # 數據持久化 volumes: # 數據名稱 nfs-nginx_log: driver: local driver_opts: # 類型 type: "nfs" # 官方默認配置 o: "addr=192.168.1.81,vers=4,soft,timeo=180,bg,tcp,rw" device: "192.168.1.81:/docker/service/zs/nginx/log" # 本地配置文件配置 configs: # 配置文件名稱 nginx_config: # 本地配置文件路徑 file: /docker/service/zs/nginx/config/nginx.conf # 配置文件名稱 nginx_vhosts: # 本地配置文件路徑 file: /docker/service/zs/nginx/config/vhosts.conf
五、建立服務
docker stack deploy -c service_nginx.yml nginx