今天收到一個需求,要部署一個H5頁面,多簡單。既然是靜態頁面,那只需要構建一個nginx就可以了。
第一步:
構建nginx鏡像:我嘗試了兩種方法:
第一種:
直接拉取nginx鏡像,把程序打包拷貝進去就OK。
Dockerfile
FROM nginx # COPY nginx.conf /etc/nginx/nginx.conf COPY . /usr/share/nginx/html COPY nginx.conf /etc/nginx/nginx.conf WORKDIR /usr/share/nginx/html CMD ["nginx","-g","daemon off;"]
跑一下build就行了。
docker build -f dockerfile -t nginx:v1 .
缺點:nginx版本無法自定義。
第二種:拉取一個centos:7基礎鏡像,打包nginx成新鏡像
FROM centos:7 MAINTAINER llody WORKDIR /usr/local/src RUN yum install -y wget RUN wget http://nginx.org/download/nginx-1.7.8.tar.gz RUN tar zxvf nginx-1.7.8.tar.gz WORKDIR nginx-1.7.8 RUN yum -y install gcc gcc-c++ pcre-devel zlib-devel openssl* RUN useradd -M -u 40 -s /sbin/nologin nginx RUN ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-file-aio --with-http_stub_status_module --with-http_gzip_static_module --with-http_flv_module --with-http_ssl_module --with-http_realip_module RUN make RUN make install RUN ln -s /usr/local/nginx/sbin/* /usr/local/sbin/ RUN /usr/local/nginx/sbin/nginx RUN echo "daemon off;">>/usr/local/nginx/conf/nginx.conf ADD run.sh /usr/local/sbin/run.sh RUN chmod 755 /usr/local/sbin/run.sh RUN mkdir -p /data/www && chown -R 755 /data/www WORKDIR /data/www #COPY index.html /data/www #COPY nginx.conf /usr/local/nginx/conf/nginx.conf EXPOSE 80 443 CMD ["/usr/local/sbin/run.sh"]
兩個 COPY的作用是把程序文件和nginx配置文件拷貝進容器進行替換。
nginx.conf配置文件:
user root; #modify worker_processes auto; #modify worker_rlimit_nofile 51200; events { use epoll; worker_connections 51200; multi_accept on; } http { include types; default_type application/octet-stream; client_max_body_size 100m; sendfile on; keepalive_timeout 120; #65; #gzip on; server { listen 80; server_name cj.juyunka.com; #charset koi8-r; #access_log logs/host.access.log main; root /usr/share/nginx/html; index index.html index.htm; error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } #include vhost/*.conf; }