docker+nginx构建镜像,部署静态页面
今天收到一个需求,要部署一个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; }