Use dockerfile to manually build the JDK11 image, run the container and verify

Docker Hub, the public warehouse website where Docker officially maintains images , is no longer accessible in China. Most images cannot be downloaded. We are preparing to gradually build our own image library. [Reprinted from aliyun official-Container Image Service ACR] Docker FAQs

Alibaba Cloud Container Registry (ACR) is a secure hosting and efficient distribution platform for OCI-compliant cloud native products such as container images and Helm Charts. ACR Enterprise Edition supports full-link acceleration capabilities such as global synchronization acceleration, large-scale and large image distribution acceleration, and multi-code source construction acceleration. It is seamlessly integrated with container service ACK to help enterprises reduce delivery complexity and create a one-stop solution for cloud native applications. solution.

1. Download the JDK11 package under Linux

1.1 Download jdk-11.0.19_linux-x64_bin.tar.gz

Java SE Development Kit 11.0.19
requires an Oracle account to be logged in before downloading.
Here is an Oracle public account and password disclosed by other bloggers

Insert image description here

1.2 Upload the downloaded JDK11 package to the new folder mydockerfile in CentOS

Suggestion: Put the dockerfile file and the jdk package in the same folder, so you don't need to use the -f parameter to specify the location of the dockerfile when building.
Insert image description here
The name of the folder here is written as mydockfile , which should be mydockerfile . It has been renamed and corrected later:
Insert image description here

2. Write the dockerfile file and execute the docker build command to build the image

Prerequisite : Docker is installed and configured. Please refer to my article on installing and configuring docker and docker-compose based on CentOS7 .

2.1 Write and upload dockerfile

We use the command docker build to create a new image from scratch. To do this, we need to create a dockerfile that contains a set of instructions to tell Docker how to build our image. Each command creates a new layer on the image, and the prefix of each command must be uppercase.
The first FROM specifies which image source to use.
The RUN command tells docker to execute commands in the image and install what. . .
Then, we use the dockerfile file to build an image through the docker build command.

Note: The dockerfile file must be written with the Linux system version. I am using the CentOS system here. To query the Linux system version, you can refer to my article to
check the CentOS version and system digits and set the CentOS 7.9 2009 firewall configuration to open the port command and process

dockerfile file content:

# 建立一个新的镜像文件,配置模板:新建立的镜像是以centos为基础模板
# 因为jdk必须运行在操作系统之上
#每一个指令都会在镜像上创建一个新的层,每一个指令的前缀都必须是大写的。
#第一条FROM,指定使用哪个镜像源

FROM centos:7.9.2009

#维护者 作者 邮箱
MAINTAINER djcking <djc**4*****@qq.com>

#RUN 指令告诉docker 在镜像内执行命令,安装了什么
#创建一个新目录来存储jdk文件
RUN  mkdir "/usr/local/java"

#将jdk压缩文件复制到镜像中,它将自动解压缩tar文件
ADD jdk-11.0.19_linux-x64_bin.tar.gz   /usr/local/java

# 设置时区
RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime


# 设置环境变量
ENV JAVA_HOME /usr/local/java/jdk-11.0.19
ENV PATH $JAVA_HOME/bin:$PATH


# VOLUME 指定了临时文件目录为/tmp
# 其效果是在主机 /var/lib/docker 目录下创建了一个临时文件,并链接到容器的/tmp
VOLUME /tmp

Upload the dockerfile file:
It is recommended that the dockerfile file and the jdk package be placed in the same folder, so that you do not need to use the -f parameter to specify the location of the dockerfile when building.

Insert image description here

2.2 Execute the docker build command to build the image

The relationship between dockerfile, image-IMAGE, and container-CONTAINER (personal learning and understanding):

1、docker build命令使用dockerfile文件中的配置来构建镜像-IMAGE2、docker run命令运行构建好的镜像-IMAGE来启动容器-CONTAINER;

3、镜像-IMAGE是死的,因为它本质上就是为了生成容器而存在的,容器-CONTAINER是活的,因为最终运行服务时使用的是容器-CONTAINER

Build image command-docker build:

参数说明:
-t :指定要创建的目标镜像名
.Dockerfile 文件所在目录,可以指定Dockerfile 的绝对路径
//进入存放jdk包与dockerfile的目录mydockerfile
cd mydockerfile

//-t 给新构建的镜像取名为java, 并设定版本为11
//注意最后有个点,代表使用当前路径的 Dockerfile 进行构建
//成功执行下面的docker build命令后,docker images 命令中,TAG就是11(镜像标签或者版本),Repository就是java,为镜像仓库源名。
docker build -t java:11 .

Insert image description here
Use the docker images command in the above figure to see that the created jdk image already exists in the list, and the IMAGE ID has been successfully generated.

docker images 各参数说明:

REPOSITORY:表示镜像的仓库源名

TAG:镜像的标签或版本

IMAGE ID:镜像ID

CREATED:镜像创建时间

SIZE:镜像大小

同一镜像仓库源可以有多个 TAG,代表这个仓库源的不同个版本,如 jdk仓库源里,有 111213....20 等多个不同的版本,我们使用 REPOSITORY:TAG 来定义并区分不同的镜像,例如jdk:11,jdk:12,jdk:13,jdk:20等。

3. Start the container through image creation and verify

3.1 Create a startup container through image creation


//运行该镜像 
//此命令使用镜像java:11,创建并启动容器,执行成功会根据算法返回一个字符串,是CONTAINER ID,即辨别容器的ID。
// --name djcjdk11 代表一个别名。
docker run -id  --name djcjdk11 java:11

//docker run 参数说明:
-i:交互式操作

-d:后台运行

-t:终端。

The docker ps command displays the running container (only the first 12 digits of the CONTAINER ID are displayed by default . The complete container ID has 64 digits, but it is enough to distinguish different containers):
Insert image description here

3.2 Verification

Because the source package of the djcjdk11 container we created is jdk-11.0.19_linux-x64_bin.tar.gz , the verification checks the JDK version.

//docker exec:推荐大家使用 docker exec 命令,因为此命令会退出容器终端,但不会导致容器的停止。
//可以使用NAMES即djcjdk11或者CONTAINER ID的前四位或完整的CONTAINER ID
//以下6个命令任意一个皆可
docker exec -it NAMES  bash 
docker exec -it CONTAINER ID  bash
docker exec -it CONTAINER ID前几位 bash
即
docker exec -it djcjdk11 bash 
docker exec -it 4001866f057c  bash
docker exec -it CONTAINER ID前几位 bash ,如 docker exec -it 4001  bash

或者

docker exec -it NAMES  /bin/bash 
docker exec -it CONTAINER ID  /bin/bash
docker exec -it CONTAINER ID前几位 /bin/bash
即
docker exec -it djcjdk11 /bin/bash
docker exec -it 4001866f057c /bin/bash
docker exec -it CONTAINER ID前几位 /bin/bash,如 docker exec -it 4001  /bin/bash



//检测Java版本  java --version 或java -version
java --version 或java -version

Insert image description here

4. List of command screenshots

4.1 Commands

//1-进入文件夹
cd /home/mydockerfile

//2-构建镜像命令
//-t 给新构建的镜像仓库源取名为java, 并设定版本或者标签为为11  即TAG
docker build -t java:11 .

//3-查看images
 docker images


//4-启动运行镜像
//此命令使用镜像java:11,创建并启动容器,执行成功会根据算法返回一个字符串,是CONTAINER ID,即辨别容器的ID。
docker run -id  --name djcjdk11 java:11


//5-查看正在运行的容器
docker ps


//6-进入容器
//docker exec:推荐大家使用 docker exec 命令,因为此命令会退出容器终端,但不会导致容器的停止。
docker exec -it djcjdk11 bash 
或
docker exec -it 4001 /bin/bash 

//7-检测Java版本  java --version 或java -version
java --version 或java -version



--------其他命令---------

//关闭容器 
//这里使用CONTAINER ID的前四位
docker stop 4001

//docker logs  CONTAINER ID或者NAMES 可以查看容器内部的标准输出。
//-f: 让 docker logs 像使用 tail -f 一样来输出容器内部的标准输出。
//补充:tail 命令可用于查看文件的内容,有一个常用的参数 -f 常用于查阅正在改变的日志文件。
//tail -f filename 会把 filename 文件里的最尾部的内容显示在屏幕上,并且不断刷新,只要 filename 更新就可以看到最新的文件内容。

docker logs 4001或者 docker logs djcjdk11
docker logs -f 4001或者docker logs -f djcjdk11

For more operations on images and containers, please refer to these two articles of mine:
Basic Docker Operations: Deleting Container Container and Deleting Image IMAGE
Basic Docker Container Operations: Start-Stop-Restart

4.2 Screenshots

Insert image description here
Insert image description here

5. Reference materials

Docker image uses
Docker container uses
Docker Dockerfile
Docker Compose
Docker warehouse management
Linux tail command
to view the CentOS version and system bit number and set CentOS 7.9 2009 firewall configuration to open the port command and process
practice: use dockerfile to create a mirror to implement the containerization of the springboot project
practice: Use docker-compose to containerize the springboot project
[Docker] to manually build the OracleJDK11 image
[SpringBoot + Docker] write a Dockerfile to install jdk11.0.3, and deploy the SpringBoot project
Docker - install openjdk11 and start running
Docker's dockerfile to create a jdk image
docker file writing, and jdk11 Run java Dockerfile
Linux rm command to delete folder

Guess you like

Origin blog.csdn.net/qyfx123456/article/details/132345940