构建 JDK8 镜像

下载 JDK8

https://www.oracle.com/java/technologies/javase/javase-jdk8-downloads.html

image-20201027185030923

创建镜像的构建目录 base_jdk8,并且将 JDK8 上传到服务器

image-20201027185401601

编辑创建 Dockerfile

# Using Centos for base image
FROM centos:7

# author label
LABEL maintainer="Aron.li"

# install timezone gcc
ENV ENVIRONMENT DOCKER_PROD
RUN cd / && ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \
  && yum makecache \
  && yum install -y wget aclocal automake autoconf make gcc gcc-c++ python-devel mysql-devel bzip2 libffi-devel epel-release \
  && yum clean all

# cd /opt
WORKDIR /opt

# copy jdk8 to /opt, and decompression
ADD jdk-8u271-linux-x64.tar.gz /opt

# set jdk8 env
ENV JAVA_HOME=/opt/jdk1.8.0_271
ENV CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
ENV PATH=$JAVA_HOME/bin:$PATH

# exec java -version
CMD ["java","-version"]

构建镜像

docker build -f Dockerfile -t base_jdk8:v1.0 .

执行如下:

image-20201027191035881

生成镜像如下:

image-20201027191212996

启动镜像,测试java环境

编写一个启动镜像的脚本 restart_service.sh

basedir=`pwd`

name='base_jdk8'
image='base_jdk8:v1.0'
docker stop $name
docker rm $name
docker run -it --privileged=true --name $name $image /bin/bash

测试如下:

image-20201027191259441

基于 JDK8 的镜像,构建SpringBoot服务

上传 SpringBoot 的 jar 包

image-20201027193505484

编辑创建 Dockerfile

# Using JDK8 for base image
FROM base_jdk8:v1.0

# author label
LABEL maintainer="Aron.li"

# install timezone gcc
ENV ENVIRONMENT DOCKER_PROD
RUN cd / && ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

# cd /opt
WORKDIR /opt

# copy jar to /opt, and decompression
ADD autotest-0.0.1-SNAPSHOT.jar /opt

# exec java -jar autotest-0.0.1-SNAPSHOT.jar
CMD ["java","-jar","autotest-0.0.1-SNAPSHOT.jar"]

构建镜像

docker build -f Dockerfile -t autotest:v1.0 .

启动镜像服务

编写一个启动镜像的脚本 restart_service.sh

#!/bin/bash
basedir=$(cd `dirname $0`;pwd)
host_ip=`python -c "import socket;print([(s.connect(('8.8.8.8', 53)), s.getsockname()[0], s.close()) for s in [socket.socket(socket.AF_INET, socket.SOCK_DGRAM)]][0][1])"`


name='autotest'
image='autotest:v1.0'
docker stop $name
docker rm $name
#docker run -it --name $name $image /bin/bash

docker run -d --name $name -p 8080:8080 -h $name \
           --add-host=mysql_host:$host_ip \
           --add-host=mongo_host:$host_ip \
           --add-host=redis_host:$host_ip \
           $image
Logo

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

更多推荐