环境信息

环境版本(信息)
基础镜像操作系统CentOS7.6 (CentOS-7-x86_64-1810)
JDKOracle JDK-8u191

前置条件:

​ 实验环境需要能够联网

安装步骤

  1. 下载centos7.6

    docker pull centos:7.6.1810

下载完成后,通过docker images查看镜像列表

[root@MiWiFi-R3-srv cds-config-server]# docker images
REPOSITORY              TAG                 IMAGE ID            CREATED             
centos                  7.6.1810            d5f224905a66        3 weeks ago         202MB
  1. JDK的安装和配置

新建Dockerfile文件,其内容如下

FROM centos:7.6.1810
MAINTAINER kongfan:2018/12/31
ENV JAVA_VERSION=8 \
    JAVA_UPDATE=191 \
    JAVA_BUILD=12 \
    JAVA_PATH=2787e4a523244c269598db4e85c51e0c \
    JAVA_HOME="/usr/lib/jvm/default-jvm"
 
RUN yum install wget ca-certificates unzip -y && \
    cd "/tmp" && \
    wget --header "Cookie: oraclelicense=accept-securebackup-cookie;" \
        "http://download.oracle.com/otn-pub/java/jdk/${JAVA_VERSION}u${JAVA_UPDATE}-b${JAVA_BUILD}/${JAVA_PATH}/jdk-${JAVA_VERSION}u${JAVA_UPDATE}-linux-x64.tar.gz" && \
    tar -xzf "jdk-${JAVA_VERSION}u${JAVA_UPDATE}-linux-x64.tar.gz" && \
    mkdir -p "/usr/lib/jvm" && \
    mv "/tmp/jdk1.${JAVA_VERSION}.0_${JAVA_UPDATE}" "/usr/lib/jvm/java-${JAVA_VERSION}-oracle" && \
    ln -s "java-${JAVA_VERSION}-oracle" "$JAVA_HOME" && \
    ln -s "$JAVA_HOME/bin/"* "/usr/bin/" && \
    rm -rf "$JAVA_HOME/"*src.zip && \
    rm -rf "$JAVA_HOME/lib/missioncontrol" \
           "$JAVA_HOME/lib/visualvm" \
           "$JAVA_HOME/lib/"*javafx* \
           "$JAVA_HOME/jre/lib/plugin.jar" \
           "$JAVA_HOME/jre/lib/ext/jfxrt.jar" \
           "$JAVA_HOME/jre/bin/javaws" \
           "$JAVA_HOME/jre/lib/javaws.jar" \
           "$JAVA_HOME/jre/lib/desktop" \
           "$JAVA_HOME/jre/plugin" \
           "$JAVA_HOME/jre/lib/"deploy* \
           "$JAVA_HOME/jre/lib/"*javafx* \
           "$JAVA_HOME/jre/lib/"*jfx* \
           "$JAVA_HOME/jre/lib/amd64/libdecora_sse.so" \
           "$JAVA_HOME/jre/lib/amd64/"libprism_*.so \
           "$JAVA_HOME/jre/lib/amd64/libfxplugins.so" \
           "$JAVA_HOME/jre/lib/amd64/libglass.so" \
           "$JAVA_HOME/jre/lib/amd64/libgstreamer-lite.so" \
           "$JAVA_HOME/jre/lib/amd64/"libjavafx*.so \
           "$JAVA_HOME/jre/lib/amd64/"libjfx*.so && \
    rm -rf "$JAVA_HOME/jre/bin/jjs" \
           "$JAVA_HOME/jre/bin/keytool" \
           "$JAVA_HOME/jre/bin/orbd" \
           "$JAVA_HOME/jre/bin/pack200" \
           "$JAVA_HOME/jre/bin/policytool" \
           "$JAVA_HOME/jre/bin/rmid" \
           "$JAVA_HOME/jre/bin/rmiregistry" \
           "$JAVA_HOME/jre/bin/servertool" \
           "$JAVA_HOME/jre/bin/tnameserv" \
           "$JAVA_HOME/jre/bin/unpack200" \
           "$JAVA_HOME/jre/lib/ext/nashorn.jar" \
           "$JAVA_HOME/jre/lib/jfr.jar" \
           "$JAVA_HOME/jre/lib/jfr" \
           "$JAVA_HOME/jre/lib/oblique-fonts" && \
    wget --header "Cookie: oraclelicense=accept-securebackup-cookie;" \
        "http://download.oracle.com/otn-pub/java/jce/${JAVA_VERSION}/jce_policy-${JAVA_VERSION}.zip" && \
    unzip -jo -d "${JAVA_HOME}/jre/lib/security" "jce_policy-${JAVA_VERSION}.zip" && \
    rm "${JAVA_HOME}/jre/lib/security/README.txt" && \
    yum clean all && \
    rm "/tmp/"*

Dockerfile主要做如下事情:

  • 去Oracle官网下载jdk包,

    注意:Dockerfile中EVN的各个变量,主要是用来组装官网JDK的具体下载地址

  • 配置jdk环境变量

  • 删除不需要的文件内容,降低docker镜像的文件大小

  • 安装jdk security包

  1. 构建Docker镜像

    新构建的镜像名称为centos7.6-jdk8

# 注意后边的 . 不能忘了
docker build -t="centos7-jdk8" .
  1. docker images 查看构建结果
[root@MiWiFi-R3-srv home]# docker images
REPOSITORY              TAG                 IMAGE ID            CREATED             SIZE
centos7-jdk8            latest              1480ca58186b        20 minutes ago      379MB
centos                  7.6.1810            d5f224905a66        3 weeks ago         202MB
centos                  latest              1e1148e4cc2c        3 weeks ago         202MB
  1. 验证
    采用新构建的镜像,启动一个容器,进入容器里面查看JAVA配置信息。看到如下信息,祝贺你,大功告成。
[root@MiWiFi-R3-srv home]# docker run -t centos7-jdk8

[root@MiWiFi-R3-srv home]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
31518a2d7e39        centos7-jdk8        "/bin/bash"         25 seconds ago      Up 24 seconds                           clever_noether
[root@MiWiFi-R3-srv home]# docker exec -it 31518a2d7e39 bash
[root@31518a2d7e39 /]# java -version
java version "1.8.0_191"
Java(TM) SE Runtime Environment (build 1.8.0_191-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.191-b12, mixed mode)
[root@31518a2d7e39 /]# echo $JAVA_HOME
/usr/lib/jvm/default-jvm
Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐