需要在docker里跑Spring Boot框架,搭建个基于ubuntu:18.04的java环境。
1. 拉取镜像及下载jdk压缩包。$sudo docker pull ubuntu:18.04,到存储服务器上找了个java包jdk-8u141-linux-x64.tar.gz
2. 使用Dockerfile来build新的镜像。
$ cd java_api
$ vim Dockerfile ,内容如下:
FROM ubuntu:18.04
ADD jdk-8u141-linux-x64.tar.gz /usr/local/java
ENV JAVA_HOME /usr/local/java/jdk1.8.0_141
ENV JRE_HOME /usr/local/java/jdk1.8.0_141/jre
ENV CLASSPATH $JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
ENV PATH $PATH:$JAVA_HOME/bin
3. 构建镜像
$ docker build -t ubuntu-jdk8:18.04 .
Sending build context to Docker daemon 425.6MB
Step 1/6 : FROM ubuntu:18.04
---> 775349758637
Step 2/6 : ADD jdk-8u141-linux-x64.tar.gz /usr/local/java
---> 9a7c38f07159
Step 3/6 : ENV JAVA_HOME /usr/local/java/jdk1.8.0_141
---> Running in 9469e2781002
Removing intermediate container 9469e2781002
---> 652feae84944
Step 4/6 : ENV JRE_HOME /usr/local/java/jdk1.8.0_141/jre
---> Running in 9f8a3dcdf7db
Removing intermediate container 9f8a3dcdf7db
---> 6476d0827f65
Step 5/6 : ENV CLASSPATH $JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
---> Running in 92991b289ec8
Removing intermediate container 92991b289ec8
---> e573a21578d1
Step 6/6 : ENV PATH $PATH:$JAVA_HOME/bin
---> Running in c4bebc8e2c72
Removing intermediate container c4bebc8e2c72
---> 7cc092532b01
Successfully built 7cc092532b01
Successfully tagged ubuntu-jdk8:18.04
4. 验证
$ docker run -it --name test ubuntu-jdk8:18.04
root@7dbbb1ee96b1:/# which java
/usr/local/java/jdk1.8.0_141/bin/java
root@7dbbb1ee96b1:/# java -version
java version "1.8.0_141"
Java(TM) SE Runtime Environment (build 1.8.0_141-b15)
Java HotSpot(TM) 64-Bit Server VM (build 25.141-b15, mixed mode)
结束。