包含jdk和nginx的基础镜像

目的

  制作一个基础镜像,包含jdk和nginx,这样要将java项目或一些前端页面做成容器,可以稍作修改引用该镜像。

Dockerfile  

FROM alpine:3.8

ENV 
    LANG=C.UTF-8 
    NGINX_VERSION=1.14.2 
    JAVA_ALPINE_VERSION=8.181.13-r0 
    JAVA_HOME=/usr/local/java 
    PATH=$PATH:/usr/local/java/jre/bin:/usr/local/java/bin
    
    
############## apk update repositories ###############

RUN 
  echo "" > /etc/apk/repositories && 
  echo "http://mirrors.aliyun.com/alpine/v3.8/main" >> /etc/apk/repositories &&
  echo "http://mirrors.aliyun.com/alpine/v3.8/community" >> /etc/apk/repositories && 
  apk update
 
############### apk update repositories ##############


############### add user ###########################
## use no root user
RUN 
  addgroup -S admin && adduser -S admin -G admin && 
  apk add --no-cache bash busybox-extras curl && 
  apk add --no-cache tzdata && cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && 
  echo ""

#################add user ###########################



################## install openjdk ###################

RUN 
  set -x && apk add --no-cache openjdk8="${JAVA_ALPINE_VERSION}" && 
  ln -s /usr/lib/jvm/java-1.8-openjdk /usr/local/java && 
  echo ""
  
################## install openjdk ####################


################## install nginx #####################
RUN 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 nginx 
    && apk add --no-cache --virtual .build-deps 
        gcc 
        libc-dev 
        make 
        openssl-dev 
        pcre-dev 
        zlib-dev 
        linux-headers 
        curl 
        gnupg1 
        libxslt-dev 
        gd-dev 
        geoip-dev 
    && curl -fSL https://nginx.org/download/nginx-$NGINX_VERSION.tar.gz -o nginx.tar.gz 
    && curl -fSL https://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 -rf "$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/ 
    && install -m644 html/50x.html /usr/share/nginx/html/ 
    && install -m755 objs/nginx-debug /usr/sbin/nginx-debug 
    && 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`, then throw
    # the rest away. To do this, we need to install `gettext`
    # then move `envsubst` out of the way so `gettext` can
    # be deleted completely, then move `envsubst` back.
    && apk add --no-cache --virtual .gettext gettext 
    && mv /usr/bin/envsubst /tmp/ 
    
    && runDeps="$( 
        scanelf --needed --nobanner --format '%n#p' /usr/sbin/nginx /usr/lib/nginx/modules/*.so /tmp/envsubst 
            | tr ',' '
' 
            | sort -u 
            | awk 'system("[ -e /usr/local/lib/" $1 " ]") == 0 { next } { print "so:" $1 }' 
    )" 
    && apk add --no-cache --virtual .nginx-rundeps $runDeps 
    && apk del .build-deps 
    && apk del .gettext 
    && mv /tmp/envsubst /usr/local/bin/ 
    
    # Bring in tzdata so users could set the timezones through the environment
    # variables
    && apk add --no-cache tzdata 
    
    # 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 nginx.conf /etc/nginx/nginx.conf
COPY nginx.vh.default.conf /etc/nginx/conf.d/default.conf

EXPOSE 80

################## install nginx #####################

CMD [ "nginx","-g","daemon off;" ]
Dockerfile
user  nginx;
worker_processes  1;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}


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  65;

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;
}
nginx.conf
server {
    listen       80;
    server_name  localhost;

    #charset koi8-r;
    #access_log  /var/log/nginx/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   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ .php$ {
    #    proxy_pass   http://127.0.0.1;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    #location ~ .php$ {
    #    root           html;
    #    fastcgi_pass   127.0.0.1:9000;
    #    fastcgi_index  index.php;
    #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
    #    include        fastcgi_params;
    #}

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /.ht {
    #    deny  all;
    #}
}
nginx.vh.default.conf

制作镜像

docker build -t docker-test/common:1.0.0 .

  

运行测试

docker run -itd -p 80:80 --name web-test docker-test/common:1.0.0

  访问正常:

  

  java测试:  

bash-4.4# java -version
openjdk version "1.8.0_181"
OpenJDK Runtime Environment (IcedTea 3.9.0) (Alpine 8.181.13-r0)
OpenJDK 64-Bit Server VM (build 25.181-b13, mixed mode)  

  

参考:

  https://github.com/docker-library/openjdk/blob/47a6539cd18023dafb45db9013455136cc0bca07/8/jdk/alpine/Dockerfile

  https://github.com/nginxinc/docker-nginx/blob/b71469ab815f580ba0ad658a32e91c86f8565ed4/stable/alpine/Dockerfile

原文地址:https://www.cnblogs.com/bigberg/p/10180027.html