1. Download Alpine image
[email protected] ~]# Docker pull alpineusing default tag:latesttrying to pull repository docker.io/library/alpine ... la Test:pulling from Docker.io/library/alpine4fe2ade4980c:pull completedigest:sha256 : 621c2f39f8133acb8e64023a94dbdf0d5ca81896102b9e57c0dc184cadaf5528status:downloaded newer image for docker.io/ Alpine:latest[[email protected] ~]# docker imagesrepository TAG IMAGE ID CREATED sizedocker.io/ Alpine Latest 196d12cf6ab1 3 weeks ago 4.41 MB
2, write Dockerfile (i) 2.1. Create a directory to store files
[[email protected] ~]# cd/opt/[[email protected] opt]# mkdir alpine_nginx && cd Alpine_nginx && touch Doc Kerfile && Touch nginx.conf && touch Nginx.vh.default.conf[[email protected] alpine_nginx]# ll total dosage 16- rw-r--r--1 root root 5652 October 4 18:15 dockerfile-rw-r--r--1 root root 638 October 4 15:23 nginx.conf-rw-r--r--1 Root root 472 October 4 15:24 nginx.vh.default.conf
2.2. Prepare the nginx.conf file
[email protected] alpine_nginx]# cat nginx.conf user nginx;worker_processes 1;error_log /var/log/nginx /error.log warn;pid /var/run/nginx.pid;events { worker_connections 10240;} HTTP { include /etc/nginx/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 /var/log/nginx/access.log main; Sendfile on ; #tcp_nopush on ; Keepalive_timeout ; #gzip on ; include/etc/nginx/conf.d/*.conf;}
2.3. Prepare the nginx.vh.default.conf file
[email protected] alpine_nginx]# cat nginx.vh.default.conf server { listen ; server_name localhost; #charset Koi8-r; #access_log /var/log/nginx/log/host.access.log main; Location/{ root /usr/share/nginx/html; Index index.html index.htm; } #error_page 404 /404.html; # REDIRECT Server error pages to the static page/50x.html # error_page 502 503 504/50x.html;
location =/50x.html { root /usr/share/nginx/html; }}
2.4.dockerfile file
# Base image from alpine# author info maintainer NGINX Docker maintainers "[email protected]" # Modify source Run echo "/http mirrors.aliyun.com/alpine/latest-stable/main/">/etc/apk/repositories && echo"/HTTP/ mirrors.aliyun.com/alpine/latest-stable/community/">>/etc/apk/repositories# install required software run APK update && APK add--no-cache ca-certificates && apk add--no-cache Curl Bash tree tzdata && Cp-rf/usr/shar e/zoneinfo/asia/shanghai/etc/localtime# Set Variable env nginx_version 1.14.0# compile install Nginxrun gpg_keys= B0f4253373f8f6f510d42178520a9993a1c052f8 && config= "--prefix=/etc/nginx--sbin-path=/usr/sbin/nginx-- Modules-path=/usr/lib/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-http_ssl_module--with-http_realip_module--with-http_ Addition_module--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_random_index_module--with-http_secure_link_ Module--with-http_stub_status_module--with-http_auth_request_module--with-http_xslt_module=dynamic--with-http_ Image_filter_module=dynamic--with-http_geoip_module=dynamic--with-threads--with-stream--with-stream_ssl_module --with-stream_ssl_preread_module--with-stream_realip_module--with-stream_geoip_module=dynamic--with-http_slice _module--with-mail--with-mail_ssl_module--with-compat--with-file-aio--with-http_v2_module "&& addgroup-s Nginx && adduser-d-s-h/var/cache/nginx-s/sbin/nologin-g Nginx && APK Add--no-cache--virtual build-deps gcc libc-dev make Openssl-dev pcre-dev zlib-dev linux-headers Curl GnuPG libxslt-de V Gd-dev geoip-dev && curl-fsl http://nginx.org/download/nginx-$NGINX _version.tar.gz-o nginx.tar.gz & & CURL-FSL http://nginx.org/download/nginx-$NGINX _version.tar.gz.asc-o nginx.tar.gz.asc && Export Gnupghome= "$ (mktemp-d)" && found= "; For server in Ha.pool.sks-keyservers.net hkp://keyserver.ubuntu.com:80 hkp://p80.pool.sks-keyservers.net:80 pgp.mit.edu; Do echo "fetching GPG key $GPG _keys from $server"; GPG--keyserver "$server"--keyserver-options timeout=10--recv-keys "$GPG _keys" && found=yes && break; Done Test-z "$found" && echo >&2 "error:failed to fetch GPG key $GPG _keys" && exit 1; GPG--batch--verify nginx.tar.gz.asc nginx.tar.gz && rm-r "$GNUPGHOME" nginx.tar.gz.asc && mkdir-p/usr /SRC && tar-zxc/usr/src-f nginx.tar.gz && RM nginx.tar.gz&& cd/usr/src/nginx-$NGINX _version &&/configure $CONFIG--with-debug && make-j$ (getconf _ NPROCESSORS_ONLN) && mv Objs/nginx objs/nginx-debug && MV objs/ngx_http_xslt_filter_module.so OBJS/NGX _http_xslt_filter_module-debug.so && MV objs/ngx_http_image_filter_module.so objs/ngx_http_image_filter_ module-debug.so && mv objs/ngx_http_geoip_module.so objs/ngx_http_geoip_module-debug.so && MV objs/ Ngx_stream_geoip_module.so objs/ngx_stream_geoip_module-debug.so &&./configure $CONFIG && make-j$ ( getconf _nprocessors_onln) && make install && rm-rf/etc/nginx/html/&& mkdir/etc/nginx/conf.d/ && mkdir-p/usr/share/nginx/html/&& install-m644 html/index.html/usr/share/nginx/html/&& in stall-m644 html/50x.html/usr/share/nginx/html/&& install-m755 objs/nginx-debug/usr/sbin/nginx-debug && Amp install-m755 objs/ngx_http_xslt_filter_module-debug.so/usr/lib/nginx/modules/ngx_http_xslt_filter_module-debug.so && install-m755 Objs/ngx_http_image_filter_ module-debug.so/usr/lib/nginx/modules/ngx_http_image_filter_module-debug.so && install-m755 objs/ngx_http _geoip_module-debug.so/usr/lib/nginx/modules/ngx_http_geoip_module-debug.so && install-m755 Objs/ngx_ Stream_geoip_module-debug.so/usr/lib/nginx/modules/ngx_stream_geoip_module-debug.so && ln-s. /.. /usr/lib/nginx/modules/etc/nginx/modules && strip/usr/sbin/nginx* && strip/usr/lib/nginx/modules/* . So && rm-rf/usr/src/nginx-$NGINX _version # Bring in GetText so we can get ' envsubst ' and then throw# the rest away . To does this, we need to install ' gettext ' # then move ' Envsubst ' out of the ' GetText ' can# is deleted completely, the N Move ' envsubst ' back.&& apk add--no-cache--virtual gettext gettext && mv/usr/bin/envsubst/tmp/& ;& rundeps= "$ (scanelf--needed--nobanner/usr/sbin/nginx/usr/liB/nginx/modules/*.so/tmp/envsubst | awk ' {gsub (/,/, "\nso:", $); Print "So:" | Sort-u | Xargs-r apk Info--installed | Sort-u) "&& apk add--no-cache--virtual. nginx-rundeps $runDeps && apk del. Build-deps && apk D El. GetText && mv/tmp/envsubst/usr/local/bin/# forward request and error logs to Docker log collector&& Ln-sf/dev/stdout/var/log/nginx/access.log && ln-sf/dev/stderr/var/log/nginx/error.log# Copy the files in the directory to the mirror copy nginx.conf/etc/nginx/nginx.confcopy nginx.vh.default.conf/etc/nginx/conf.d/default.conf# Open 80 port Expose 80STOPSIGNAL sigterm# start nginx command cmd ["Nginx", "-G", "daemon off;"]
2.5. Create a mirror
[email protected] alpine_nginx]# Docker build-t Alpine:nginx.
2.6. Create a container
# do not host profile log files mount Docker run-tid--name Zjznginx- p 80:80-m 2048m --memory-swap=2048m --cpu-shares=256 alpine:nginx# mount configuration files and logs Docker Run-tid--name Zjznginx- p 80:80-v/opt/webs/nginx/nginx.conf:/etc/nginx/ Nginx.conf-v/opt/webs/nginx/logs/:/var/log/nginx-m 2048m --memory-swap=2048m --cpu-shares=256 Alpine: Nginx
View Container
[[email protected] alpine_nginx]# Docker pscontainer ID IMAGE COMMAND CREATED STATUS PORTS names27c9ed6664ba alpine:nginx "nginx-g ' daemon ..." 1 second ago up 1 second 0.0.0.0:80->80/tcp Zjznginx
3, write Dockerfile (ii) 3.1. Create a directory of files to be stored
[[email protected] ~]# cd/opt/[[email protected] opt]# mkdir alpine_nginx && cd Alpine_nginx && touch Doc Kerfile && Touch nginx.conf && touch Nginx.vh.default.conf[[email protected] alpine_nginx]# ll total dosage 16- rw-r--r--1 root root 5652 October 4 18:15 dockerfile-rw-r--r--1 root root 638 October 4 15:23 nginx.conf-rw-r--r--1 Root root 472 October 4 15:24 nginx.vh.default.conf
3.2. Prepare the nginx.conf file
[email protected] alpine_nginx]# cat nginx.conf user nginx;worker_processes 1;error_log /var/log/nginx /error.log warn;pid /var/run/nginx.pid;events { worker_connections 10240;} HTTP { include /etc/nginx/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 /var/log/nginx/access.log main; Sendfile on ; #tcp_nopush on ; Keepalive_timeout ; #gzip on ; include/etc/nginx/conf.d/*.conf;}
3.3. Prepare the nginx.vh.default.conf file
[email protected] alpine_nginx]# cat nginx.vh.default.conf server { listen ; server_name localhost; #charset Koi8-r; #access_log /var/log/nginx/log/host.access.log main; Location/{ root /usr/share/nginx/html; Index index.html index.htm; } #error_page 404 /404.html; # REDIRECT Server error pages to the static page/50x.html # error_page 502 503 504/50x.html;
location =/50x.html { root /usr/share/nginx/html; }}
3.4.dockerfile file
# Base image from alpine# author info maintainer NGINX Docker maintainers "[email protected]" # Modify source Run echo "/http mirrors.aliyun.com/alpine/latest-stable/main/">/etc/apk/repositories && echo"/HTTP/ mirrors.aliyun.com/alpine/latest-stable/community/">>/etc/apk/repositories# install required software run APK update && APK add--no-cache ca-certificates && apk add--no-cache Curl Bash tree tzdata && Cp-rf/usr/shar e/zoneinfo/asia/shanghai/etc/localtime# Set Variable env nginx_version 1.14.0# compile install Nginxrun gpg_keys= B0f4253373f8f6f510d42178520a9993a1c052f8 && config= "--prefix=/etc/nginx--sbin-path=/usr/sbin/nginx-- Modules-path=/usr/lib/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-http_ssl_module--with-http_realip_module--with-http_ Addition_module--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_random_index_module--with-http_secure_link_ Module--with-http_stub_status_module--with-http_auth_request_module--with-http_xslt_module=dynamic--with-http_ Image_filter_module=dynamic--with-http_geoip_module=dynamic--with-threads--with-stream--with-stream_ssl_module --with-stream_ssl_preread_module--with-stream_realip_module--with-stream_geoip_module=dynamic--with-http_slice _module--with-mail--with-mail_ssl_module--with-compat--with-file-aio--with-http_v2_module "&& addgroup-s Nginx && adduser-d-s-h/var/cache/nginx-s/sbin/nologin-g Nginx && APK Add--no-cache--virtual build-deps gcc libc-dev make Openssl-dev pcre-dev zlib-dev linux-headers Curl GnuPG libxslt-de V Gd-dev geoip-dev && curl-fsl http://nginx.org/download/nginx-$NGINX _version.tar.gz-o nginx.tar.gz & & CURL-FSL http://nginx.org/download/nginx-$NGINX _version.tar.gz.asc-o nginx.tar.gz.asc && Export Gnupghome= "$ (mktemp-d)" && found= "; For server in Ha.pool.sks-keyservers.net hkp://keyserver.ubuntu.com:80 hkp://p80.pool.sks-keyservers.net:80 pgp.mit.edu; Do echo "fetching GPG key $GPG _keys from $server"; GPG--keyserver "$server"--keyserver-options timeout=10--recv-keys "$GPG _keys" && found=yes && break; Done Test-z "$found" && echo >&2 "error:failed to fetch GPG key $GPG _keys" && exit 1; GPG--batch--verify nginx.tar.gz.asc nginx.tar.gz && rm-r "$GNUPGHOME" nginx.tar.gz.asc && mkdir-p/usr /SRC && tar-zxc/usr/src-f nginx.tar.gz && RM nginx.tar.gz&& cd/usr/src/nginx-$NGINX _version &&/configure $CONFIG--with-debug && make-j$ (getconf _ NPROCESSORS_ONLN) && mv Objs/nginx objs/nginx-debug && MV objs/ngx_http_xslt_filter_module.so OBJS/NGX _http_xslt_filter_module-debug.so && MV objs/ngx_http_image_filter_module.so objs/ngx_http_image_filter_ module-debug.so && mv objs/ngx_http_geoip_module.so objs/ngx_http_geoip_module-debug.so && MV objs/ Ngx_stream_geoip_module.so objs/ngx_stream_geoip_module-debug.so &&./configure $CONFIG && make-j$ ( getconf _nprocessors_onln) && make install && rm-rf/etc/nginx/html/&& mkdir/etc/nginx/conf.d/ && mkdir-p/usr/share/nginx/html/&& install-m644 html/index.html/usr/share/nginx/html/&& in stall-m644 html/50x.html/usr/share/nginx/html/&& install-m755 objs/nginx-debug/usr/sbin/nginx-debug && Amp install-m755 objs/ngx_http_xslt_filter_module-debug.so/usr/lib/nginx/modules/ngx_http_xslt_filter_module-debug.so && install-m755 Objs/ngx_http_image_filter_ module-debug.so/usr/lib/nginx/modules/ngx_http_image_filter_module-debug.so && install-m755 objs/ngx_http _geoip_module-debug.so/usr/lib/nginx/modules/ngx_http_geoip_module-debug.so && install-m755 Objs/ngx_ Stream_geoip_module-debug.so/usr/lib/nginx/modules/ngx_stream_geoip_module-debug.so && ln-s. /.. /usr/lib/nginx/modules/etc/nginx/modules && strip/usr/sbin/nginx* && strip/usr/lib/nginx/modules/* . So && rm-rf/usr/src/nginx-$NGINX _version # Bring in GetText so we can get ' envsubst ' and then throw# the rest away . To does this, we need to install ' gettext ' # then move ' Envsubst ' out of the ' GetText ' can# is deleted completely, the N Move ' envsubst ' back.&& apk add--no-cache--virtual gettext gettext && mv/usr/bin/envsubst/tmp/& ;& rundeps= "$ (scanelf--needed--nobanner/usr/sbin/nginx/usr/liB/nginx/modules/*.so/tmp/envsubst | awk ' {gsub (/,/, "\nso:", $); Print "So:" | Sort-u | Xargs-r apk Info--installed | Sort-u) "&& apk add--no-cache--virtual. nginx-rundeps $runDeps && apk del. Build-deps && apk D El. GetText && mv/tmp/envsubst/usr/local/bin/# forward request and error logs to Docker log collector&& Ln-sf/dev/stdout/var/log/nginx/access.log && ln-sf/dev/stderr/var/log/nginx/error.log# Copy the files in the directory to the mirror copy nginx.conf/etc/nginx/nginx.confcopy nginx.vh.default.conf/etc/nginx/conf.d/default.conf# Launch command into a script start run echo "/usr/sbin/nginx" >>/etc/start.sh# Open 80 port expose 80STOPSIGNAL sigterm# start nginx command cmd ["/ Bin/sh ","/etc/start.sh "]
3.5. Create a mirror
[email protected] alpine_nginx]# Docker build-t Alpine:nginx.
3.6. Create a container
# do not host profile log files mount Docker run-ti--restart=always--name zjznginx -P 80:80-m 2048m --memory-swap=2048m -- cpu-shares=256 Alpine:nginx bash# Mount profile and log Docker Run-ti--restart=always--name Zjznginx- p 80:80-v/opt/webs/ngin X/nginx.conf:/etc/nginx/nginx.conf-v/opt/webs/nginx/logs/:/var/log/nginx-m 2048m --memory-swap=2048m -- cpu-shares=256 Alpine:nginx Bash
PS: Because of the way the script command started, there is no daemon, and finally use bash or SH into the container boot and then ctrl+p ctrl+q exit
Specific explanation:Why does Docker run Nginx use daemon off
Start Nginx
[[email protected] alpine_nginx]# Docker run-ti--restart=always--name Zjznginx- p 80:80-m 2048m --memory-swap =2048m
View Container
[[email protected] alpine_nginx]# Docker pscontainer ID IMAGE COMMAND CREATED STATUS PORTS NAMES0DA349AFEBFF alpine:nginx "bash" seconds ago up seconds 0.0.0.0:80->80/tcp Zjznginx
4. Testing
Nginx image created with dockerfile based on Alpine