1,创建目录并下载jdk上传到服务器中
首先需要jdk17的tar.gz 的包,没有的话,官网下载
从jdk官网下载jdk17镜像,再用xftp上传到服务器中(速度会快点)
jdk官网:Java Downloads | Oracle
创建文件夹
mkdir -p /usr/local/dockerjdk17
cd /usr/local/dockerjdk17
把下载下来的jdk丢到里面,(重要)
2、创建 Dockerfile 文件
FROM centos:7
MAINTAINER ALuo
WORKDIR /usr/local/java
ADD jdk-17_linux-x64_bin.tar.gz /usr/local/java/
ENV JAVA_HOME=/usr/local/java/jdk-17.0.10
ENV CLASSPATH=.:$JAVA_HOME/lib/jrt-fs.jar
ENV PATH=$PATH:$JAVA_HOME/bin
踩坑点:
1,这里的配置文件是对于新创建的docker镜像的,不是本地的地址,我第一遍就是把add 的路径当成了本地上传文件的路径,导致报错。
报错信息:
[root@localhost dockerjdk17]# docker build -t='centos7-java17' .
[+] Building 118.9s (7/7) FINISHED docker:default
=> [internal] load build definition from Dockerfile 0.0s
=> => transferring dockerfile: 255B 0.0s
=> [internal] load metadata for docker.io/library/centos:7 118.9s
=> [internal] load .dockerignore 0.0s
=> => transferring context: 2B 0.0s
=> CANCELED [1/3] FROM docker.io/library/centos:7@sha256:be65f488b7764ad3638f236b7b515b3678369a5124c47b8d32916d6487418ea4 0.0s
=> => resolve docker.io/library/centos:7@sha256:be65f488b7764ad3638f236b7b515b3678369a5124c47b8d32916d6487418ea4 0.0s
=> => sha256:eeb6ee3f44bd0b5103bb561b4c16bcb82328cfe5809ab675bb17ab3a16c517c9 2.75kB / 2.75kB 0.0s
=> => sha256:be65f488b7764ad3638f236b7b515b3678369a5124c47b8d32916d6487418ea4 1.20kB / 1.20kB 0.0s
=> => sha256:dead07b4d8ed7e29e98de0f4504d87e8880d4347859d839686a31da35a3b532f 529B / 529B 0.0s
=> [internal] load build context 0.0s
=> => transferring context: 2B 0.0s
=> CACHED [2/3] WORKDIR /usr/local/jdk 0.0s
=> ERROR [3/3] ADD jdk-17_linux-x64_bin.tar.gz /usr/local/jdk 0.0s
------
> [3/3] ADD jdk-17_linux-x64_bin.tar.gz /usr/local/jdk:
------
Dockerfile:4
--------------------
2 | MAINTAINER ALuo
3 | WORKDIR /usr/local/jdk
4 | >>> ADD jdk-17_linux-x64_bin.tar.gz /usr/local/jdk
5 | ENV JAVA_HOME=/usr/local/jdk/jdk-17.0.2
6 | ENV CLASSPATH=.:$JAVA_HOME/lib/jrt-fs.jar
--------------------
ERROR: failed to solve: failed to compute cache key: failed to calculate checksum of ref 740c625f-cd54-44ab-8969-c9101a1b35d8::td9lhc77z0mx8g4lqgc12jvvf: "/jdk-17_linux-x64_bin.tar.gz": not found
[root@localhost dockerjdk17]# ls
2,ENV JAVA_HOME=/usr/local/java/jdk-17.0.10 中的jdk-17.0.10不要替换成jdk-17_linux-x64_bin.tar.gz ,因为ADD jdk17.tar.gz命令中ADD自带解压缩的功能,而jdk-17_linux-x64_bin.tar.gz 解压缩后并不是直接去掉压缩后缀,所以最好先解压一遍看下解压之后的文件夹名是什么,再替换文件目录名
3,复制的时候注意一下,这里复制过去的FROM 莫名其妙的就成了 M 了,导致报错。
报错信息:
[root@localhost dockerjdk17]# docker build -t='centos7-java17' .
[+] Building 0.0s (1/1) FINISHED docker:default
=> [internal] load build definition from Dockerfile 0.0s
=> => transferring dockerfile: 252B 0.0s
Dockerfile:1
--------------------
1 | >>> M centos:7
2 | MAINTAINER ALuo
3 | WORKDIR /usr/local/jdk
--------------------
ERROR: failed to solve: dockerfile parse error on line 1: unknown instruction: M (did you mean CMD?)
3、构建镜像(注意后面有点,表示当前目录)
docker build -t='centos7-java17' .
出现下面信息,表示镜像打包成功
[root@localhost dockerjdk17]# docker build -t='centos7-java17' .
[+] Building 136.3s (8/8) FINISHED docker:default
=> [internal] load build definition from Dockerfile 0.0s
=> => transferring dockerfile: 255B 0.0s
=> [internal] load metadata for docker.io/library/centos:7 22.5s
=> [internal] load .dockerignore 0.0s
=> => transferring context: 2B 0.0s
=> [1/3] FROM docker.io/library/centos:7@sha256:be65f488b7764ad3638f236b7b515b3678369a5124c47b8d32916d6487418ea4 108.5s
=> => resolve docker.io/library/centos:7@sha256:be65f488b7764ad3638f236b7b515b3678369a5124c47b8d32916d6487418ea4 0.0s
=> => sha256:be65f488b7764ad3638f236b7b515b3678369a5124c47b8d32916d6487418ea4 1.20kB / 1.20kB 0.0s
=> => sha256:dead07b4d8ed7e29e98de0f4504d87e8880d4347859d839686a31da35a3b532f 529B / 529B 0.0s
=> => sha256:eeb6ee3f44bd0b5103bb561b4c16bcb82328cfe5809ab675bb17ab3a16c517c9 2.75kB / 2.75kB 0.0s
=> => sha256:2d473b07cdd5f0912cd6f1a703352c82b512407db6b05b43f2553732b55df3bc 76.10MB / 76.10MB 103.4s
=> => extracting sha256:2d473b07cdd5f0912cd6f1a703352c82b512407db6b05b43f2553732b55df3bc 4.8s
=> [internal] load build context 1.4s
=> => transferring context: 181.12MB 1.4s
=> [2/3] WORKDIR /usr/local/jdk 0.8s
=> [3/3] ADD jdk-17_linux-x64_bin.tar.gz /usr/local/jdk 3.1s
=> exporting to image 1.4s
=> => exporting layers 1.1s
=> => writing image sha256:da3c317efca440c8130e6781f61b7c51b1413e64f0fd238f2b5fbd5232c92138 0.3s
=> => naming to docker.io/library/centos7-java17
4、启动镜像
docker run -di --name centos7-java17 -d centos7-java17
5、进入容器
查询jdk17的容器信息
docker images
找到jdk17容器信息
REPOSITORY TAG IMAGE ID CREATED SIZE
centos7-java17 latest da3c317efca4 10 minutes ago 520MB
hello-world latest d2c94e258dcb 11 months ago 13.3kB
进入容器
docker exec -it centos7-java17 /bin/bash
6、查看版本
java -version
显示jdk信息
[root@6e71890c0c6e jdk]# java -version
java version "17.0.2" 2022-01-18 LTS
Java(TM) SE Runtime Environment (build 17.0.2+8-LTS-86)
Java HotSpot(TM) 64-Bit Server VM (build 17.0.2+8-LTS-86, mixed mode, sharing)
退出容器 输入 exit
即可
[root@6e71890c0c6e jdk]# exit
exit
[root@localhost dockerjdk17]#