docker 手工创建jdk镜像
1 Dockerfile
1.1 Dockerfile 定义
Dockerfile是由一系列命令和参数构成的脚本,这些命令应用于基础镜像并最终创建一个新的镜像。
1、对于开发人员:可以为开发团队提供一个完全一致的开发环境;
2、对于测试人员:可以直接拿开发时所构建的镜像或者通过Dockerfile文件构建一个新的镜像开始工作了;
3、对于运维人员:在部署时,可以实现应用的无缝移植。
1.2 常用命令
命令 | 作用 |
---|---|
FROM image_name:tag | 定义了使用哪个基础镜像启动构建流程 |
MAINTAINER user_name | 声明镜像的创建者 |
ENV key value | 设置环境变量 (可以写多条) RUN command |
ADD source_dir/file dest_dir/file | 将宿主机的文件复制到容器内,如果是一个压缩文件,将会在复制后自动解压 |
COPY source_dir/file dest_dir/file | 和ADD相似,但是如果有压缩文件并不能解压 |
WORKDIR path_dir | 设置工作目录 |
EXPOSE port1 prot2 | 用来指定端口,使容器内的应用可以通过端口和外界交互 |
CMD argument | 在构建容器时使用,会被docker run 后的argument覆盖 |
ENTRYPOINT argument | 和CMD相似,但是并不会被docker run指定的参数覆盖 |
VOLUME | 将本地文件夹或者其他容器的文件挂载到容器中 |
1.3 使用脚本创建镜像
1.3.1 创建JDK1.8镜像
(1) 创建目录
[root@localhost ~]# mkdir -p /usr/local/dockerjdk8
(2) 将jdk 拷到该目录下
[root@localhost ~]# cp jdk-8u171-linux-x64.tar.gz /usr/local/dockerjdk8/
(3)创建docker配置文件
**注意:**如果在windows平台中编写该配置文件,在上传到centos 服务系统之前,需要进行转码
#依赖镜像名称和ID
FROM 9f38484d220f
#指定镜像创建者信息
MAINTAINER liubijun
#切换工作目录
WORKDIR /usr
RUN mkdir /usr/local/java
#ADD 是相对路径jar,把java添加到容器中
ADD jdk-8u171-linux-x64.tar.gz /usr/local/java/
#配置java环境变量
ENV JAVA_HOME /usr/local/java/jdk1.8.0_171
ENV JRE_HOME $JAVA_HOME/jre
ENV CLASSPATH $JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar:$JRE_HOME/lib:$CLASSPATH
ENV PATH $JAVA_HOME/bin:$PATH
(4)使用docker的构建命令进行构建
[root@localhost ~]# docker build -t='jdk1.8' ./ //构建镜像
Sending build context to Docker daemon 2.325 GB
Step 1/9 : FROM 9f38484d220f
---> 9f38484d220f
Step 2/9 : MAINTAINER liubijun
---> Running in 90cb390ea092
---> 0f4519265a2c
Removing intermediate container 90cb390ea092
Step 3/9 : WORKDIR /usr
---> 0b6315992472
Removing intermediate container 55a413f2e835
Step 4/9 : RUN mkdir /usr/local/java
---> Running in c1d0d7331bb0
---> 34cd53b6d517
Removing intermediate container c1d0d7331bb0
Step 5/9 : ADD jdk-8u171-linux-x64.tar.gz /usr/local/java/
---> 40500f1cd935
Removing intermediate container 3fc39a4719e6
Step 6/9 : ENV JAVA_HOME /usr/local/java/jdk1.8.0_171
---> Running in 93dd96865d97
---> a662d7ee71d1
Removing intermediate container 93dd96865d97
Step 7/9 : ENV JRE_HOME $JAVA_HOME/jre
---> Running in 853ea1bc2911
---> 9879c2f94824
Removing intermediate container 853ea1bc2911
Step 8/9 : ENV CLASSPATH $JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar:$JRE_HOME/lib:$CLASSPATH
---> Running in 513dfa6bb4e5
---> 622b87b0c066
Removing intermediate container 513dfa6bb4e5
Step 9/9 : ENV PATH $JAVA_HOME/bin:$PATH
---> Running in 1cbc7ff7f713
---> 88a34dbcd5f0
Removing intermediate container 1cbc7ff7f713
Successfully built 88a34dbcd5f0
[root@localhost ~]# docker images // 查看构建好的镜像
REPOSITORY TAG IMAGE ID CREATED SIZE
jdk1.8 latest 88a34dbcd5f0 22 seconds ago 589 MB
mynginx_1.0 latest 242f3c6e0f81 4 months ago 109 MB
docker.io/nginx latest 27a188018e18 4 months ago 109 MB
docker.io/tomcat latest 5a069ba3df4d 5 months ago 465 MB
docker.io/mongo latest b1c8142b3efc 5 months ago 410 MB
docker.io/zookeeper latest 2a7f6fc5c8a1 5 months ago 150 MB
docker.io/redis latest a55fbf438dfd 5 months ago 95 MB
docker.io/mysql latest 7bb2586065cd 5 months ago 477 MB
docker.io/centos latest 9f38484d220f 6 months ago 202 MB
docker.io/lwieske/java-8 latest ac36fa5aed45 7 months ago 163 MB
rabbitmq management df80af9ca0c9 13 months ago 149 MB
elasticsearch 5.6.8 6c0bdf761f3b 18 months ago 570 MB
mobz/elasticsearch-head 5 b19a5c98e43b 2 years ago
(5 )创建容器与验证
[root@localhost ~]# docker run -it --name=myjdk8 jdk1.8 /bin/bash
[root@8bec0ca04062 usr]# javac
Usage: javac <options> <source files>
where possible options include:
-g Generate all debugging info
-g:none Generate no debugging info
-g:{lines,vars,source} Generate only some debugging info
-nowarn Generate no warnings
-verbose Output messages about what the compiler is doing
-deprecation Output source locations where deprecated APIs are used
-classpath <path> Specify where to find user class files and annotation processors
-cp <path> Specify where to find user class files and annotation processors
-sourcepath <path> Specify where to find input source files
-bootclasspath <path> Override location of bootstrap class files
-extdirs <dirs> Override location of installed extensions
-endorseddirs <dirs> Override location of endorsed standards path
-proc:{none,only} Control whether annotation processing and/or compilation is done.
-processor <class1>[,<class2>,<class3>...] Names of the annotation processors to run; bypasses default discovery process
-processorpath <path> Specify where to find annotation processors
-parameters Generate metadata for reflection on method parameters
-d <directory> Specify where to place generated class files
-s <directory> Specify where to place generated source files
-h <directory> Specify where to place generated native header files
-implicit:{none,class} Specify whether or not to generate class files for implicitly referenced files
-encoding <encoding> Specify character encoding used by source files
-source <release> Provide source compatibility with specified release
-target <release> Generate class files for specific VM version
-profile <profile> Check that API used is available in the specified profile
-version Version information
-help Print a synopsis of standard options
-Akey[=value] Options to pass to annotation processors
-X Print a synopsis of nonstandard options
-J<flag> Pass <flag> directly to the runtime system
-Werror Terminate compilation if warnings occur
@<filename> Read options and filenames from file
[root@8bec0ca04062 usr]#