docker-运维-共享宿主机的jdk
准备
mkdir dc-jdk
cd dc-jdk
创建dockerfile
在 dc-jdk中创建 Dockerfile 文件:
FROM ubuntu:latest
ENV LANG C.UTF-8
# 指定容器内jdk所在目录
ENV JAVA_HOME /opt/softinstall/jdk1.8.0_11
ENV CLASSPATH $JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
ENV PATH $PATH:$JAVA_HOME/bin:$JAVA_HOME/sbin
创建docker-compose.yml
在 dc-jdk中创建 docker-compose.yml 文件:
version: '3'
services:
myjdk18:
build:
context: .
dockerfile: Dockerfile
volumes:
# 将本地的jdk目录/opt/softinstall/jdk1.8.0_11挂载到容器中
# 本地jdk目录:容器jdk目录
- /opt/softinstall/jdk1.8.0_11:/opt/softinstall/jdk1.8.0_11
command: java -version
构建镜像
这一步很重要,每次修改了dockerfile 之后,都需要执行这个命令!!
$ docker-compose build
[root@bluesky dc-jdk]# docker-compose build
Building myjdk18
Step 1/5 : FROM ubuntu:latest
---> 775349758637
Step 2/5 : ENV LANG C.UTF-8
---> Using cache
---> 9d7b38a12294
Step 3/5 : ENV JAVA_HOME /opt/softinstall/jdk1.8.0_11
---> Using cache
---> 07da70072ae2
Step 4/5 : ENV CLASSPATH $JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
---> Using cache
---> f707d196f0fa
Step 5/5 : ENV PATH $PATH:$JAVA_HOME/bin:$JAVA_HOME/sbin
---> Using cache
---> 9d436adab128
Successfully built 9d436adab128
Successfully tagged dc-jdk_myjdk18:latestd
运行容器
$ docker-compose up
————————————————
[root@bluesky dc-jdk]# docker-compose up
Creating dc-jdk_myjdk18_1 ... done
Attaching to dc-jdk_myjdk18_1
myjdk18_1 | java version "1.8.0_11"
myjdk18_1 | Java(TM) SE Runtime Environment (build 1.8.0_11-b12)
myjdk18_1 | Java HotSpot(TM) 64-Bit Server VM (build 25.11-b03, mixed mode)
dc-jdk_myjdk18_1 exited with code 0
网友评论