频道澳门葡京手机版网址
登录注册
澳门葡京手机版网址 > 网络 > 其他 > 正文

Dockerfile使用

2018-11-30 10:28:03           
收藏   我要投稿

利用Dockerfile制作java运行环境的镜像

#安装docker
#卸载旧版本
yumremovedocker\
docker-client\
docker-client-latest\
docker-common\
docker-latest\
docker-latest-logrotate\
docker-logrotate\
docker-selinux\
docker-engine-selinux\
docker-engine
#安装所需的包
yuminstall-yyum-utils\
device-mapper-persistent-data\
lvm2
#使用以下命令设置稳定存储库
yum-config-manager\
--add-repo\
https://download.docker.com/linux/centos/docker-ce.repo
#安装最新版本的DockerCE
yuminstalldocker-ce-y
[root@i-8ew12yu3jenkins]#dockerversion
Client:
Version:18.06.1-ce
APIversion:1.38
Goversion:go1.10.3
Gitcommit:e68fc7a
Built:TueAug2117:23:032018
OS/Arch:linux/amd64
Experimental:false

Server:
Engine:
Version:18.06.1-ce
APIversion:1.38(minimumversion1.12)
Goversion:go1.10.3
Gitcommit:e68fc7a
Built:TueAug2117:25:292018
OS/Arch:linux/amd64
Experimental:false

[root@tokyo-***tomcat-base]#systemctlenabledocker.service
[root@tokyo-***tomcat-base]#systemctlstartdocker

当前用到的环境是:Centos 7.5

Docker version 18.06.1-ce, build e68fc7a

1、先用操作系统的基础镜像把一些运行需要的环境装好在/app目录下创建相对应的文件目录

[root@xxx/app/]#mkdirdockerfile/{web/{nginx,tomcat,jdk,apache},system/{centos,ubuntu,redhat}}-pv
[root@xxx/app]#cddockerfile/system/centos/
[root@xxx/app/dockerfile/system/centos]#mkdircentos-7.5-base
[root@xxx/app/dockerfile/system/centos]#cdcentos-7.5-base

创建一个Dockerfile文件

[root@tokyo-***centos-7.5-base]#catDockerfile
#NginxBaseImage
FROMcentos:7.5.1804

LABELmaintaier"jiangwenhui<290070744@qq.com>"

RUNyuminstall-yvimwgetpcrepcre-develzlibzlib-developensslopenssl-develiproutenet-toolsiotop

创建一个用于直接docker build脚本后续直接实行脚本即可

[root@tokyo-***centos-7.5-base]#catbuild-command.sh
#!/bin/bash
dockerbuild-t172.20.7.50/baseimages/centos-base:7.5.1804.
[root@tokyo-***centos-7.5-base]#

实行脚本创建新镜像

[root@node2/app/dockerfile/system/centos/centos-7.5-base]#bashbuild-command.sh
SendingbuildcontexttoDockerdaemon3.072kB
Step1/3:FROMcentos:7.5.1804
--->76d6bc25b8a5
Step2/3:LABELmaintaier'290070744@qq.com'
--->Usingcache
--->05ccd970d71d
Step3/3:RUNyuminstall-yvimwgetpcrepcre-develzlibzlib-developensslopenssl-develiproutenet-toolsiotop
--->Usingcache
--->73d683a54877
Successfullybuilt73d683a54877
Successfullytagged172.20.7.50/baseimages/centos-base:7.5.1804

2、利用制作好的基础环境镜像制作jdk的镜像从centos-7.5-base退出来在当前目录创建一个jdk1.8的目录.

[root@node2/app/dockerfile/system/centos/centos-7.5-base]#cd..
[root@node2/app/dockerfile/system/centos]#mkdirjdk1.8
[root@node2/app/dockerfile/system/centos]#cdjdk1.8/

创建Dockerfile

[root@tokyo-***jdk1.8]#catDockerfile
FROM172.20.7.50/baseimages/centos-base:7.5.1804
LABELmaintainer"jiangwenhui<290070744@qq.com>"

ADDjdk-8u191-linux-x64.tar.gz/usr/local/src/
RUNln-s/usr/local/src/jdk1.8.0_191//usr/local/jdk

ADDprofile/etc/profile

ENVJAVA_HOME/usr/local/jdk
ENVJRE_HOME$JAVA_HOME/jre
ENVCLASSPATH$JAVA_HOME/lib/:$JRE_HOME/lib/
ENVPATH$PATH:$JAVA_HOME/bin

RUNrm-rf/etc/localtime&&ln-snf/usr/share/zoneinfo/Asia/Shanghai/etc/localtime&&echo"Asia/Shanghai">/etc/timezone

上传jdk包到当前目录下:

复制/etc/profile文件到当前目录

[root@tokyo-***jdk1.8]#cp/etc/profile.

在profile最后添加jdk的环境变量

[root@tokyo-***jdk1.8]#tail-10profile

TZ='Asia/Shanghai'
exportTZ

#java

exportJAVA_HOME=/usr/local/jdk
exportTOMCAT_HOME=/apps/tomcat
exportPATH=$JAVA_HOME/bin:$JAVA_HOME/jre/bin:$TOMCAT_HOME/bin:$PATH
exportCLASSPATH=.$CLASSPATH:$JAVA_HOME/lib:$JAVA_HOME/jre/lib:$JAVA_HOME/lib/tools.jar

创建docker build的shell脚本

[root@tokyo-***jdk1.8]#catbuild-command.sh
#!/bin/bash
#
dockerbuild-t172.20.7.50/baseimages/centos7.5-jdk:8.162.

开始制作镜像

[root@node2/app/dockerfile/system/centos/jdk1.8]#bashbuild-command.sh
SendingbuildcontexttoDockerdaemon189.8MB
Step1/10:FROM172.20.7.50/baseimages/centos-base:7.5.1804
--->73d683a54877
Step2/10:LABELmaintainer"jwh<290070744@qq.com>"
--->Runningin65604dd1f392
Removingintermediatecontainer65604dd1f392
--->c4720603ce38
Step3/10:ADDjdk-8u162-linux-x64.tar.gz/usr/local/src/
--->bc98adffe1b4
Step4/10:RUNln-s/usr/local/src/jdk1.8.0_162//usr/local/jdk
--->Runningindf5a6f67f9fd
Removingintermediatecontainerdf5a6f67f9fd
--->0ae1af0416c6
Step5/10:ADDprofile/etc/profile
--->eee23a69c0c8
Step6/10:ENVJAVA_HOME/usr/local/jdk
--->Runninginedbef8563e83
Removingintermediatecontaineredbef8563e83
--->5f783f642054
Step7/10:ENVJRE_HOME$JAVA_HOME/jre
--->Runninginfa0e5f08e732
Removingintermediatecontainerfa0e5f08e732
--->28a4d31463d4
Step8/10:ENVCLASSPATH$JAVA_HOME/lib/:$JRE_HOME/lib/
--->Runningin3c4ebb04ac62
Removingintermediatecontainer3c4ebb04ac62
--->245f2dd82d52
Step9/10:ENVPATH$PATH:$JAVA_HOME/bin
--->Runningin4f5e6093f0a9
Removingintermediatecontainer4f5e6093f0a9
--->5be0e6261eea
Step10/10:RUNrm-rf/etc/localtime&&ln-snf/usr/share/zoneinfo/Asia/Shanghai/etc/localtime&&echo"Asia/Shanghai">/etc/timezone
--->Runningin52d8cb8463a8
Removingintermediatecontainer52d8cb8463a8
--->9fb867ae8c39
Successfullybuilt9fb867ae8c39
Successfullytagged172.20.7.50/baseimages/centos7.5-jdk:8.162

查看当前目录下的文件

[root@tokyo-***jdk1.8]#ls
build-command.shDockerfilejdk-8u191-linux-x64.tar.gzprofile

检查制作好的镜像是否能正常使用

[root@tokyo-***jdk1.8]#dockerrun-it--rm172.20.7.50/baseimages/centos7.5-jdk:8.162bash
[root@efaa94bf800c/]#java-version
javaversion"1.8.0_191"
Java(TM)SERuntimeEnvironment(build1.8.0_191-b12)
JavaHotSpot(TM)64-BitServerVM(build25.191-b12,mixedmode)
[root@efaa94bf800c/]#date+'%F%T'
2018-11-2910:42:57
[root@efaa94bf800c/]#exit
exit
[root@tokyo-***jdk1.8]#

3、做Tomcat的镜像进入之前建好的/app/dockerfile/web/tomcat下,创建一个tomcat-base目录

[root@node2~]#cd/app/dockerfile/web/tomcat
[root@node2/app/dockerfile/web/tomcat]#mkdirtomcat-base
[root@tokyo-***tomcat]#cdtomcat-base/

创建Dockerfile

[root@tokyo-***tomcat-base]#catDockerfile
FROM172.20.7.50/baseimages/centos7.5-jdk:8.162

LABELmaintainer"jiangwenhui<290070744@qq.com>"

RUNmkdir/apps
ADDapache-tomcat-8.5.35.tar.gz/apps
RUNln-s/apps/apache-tomcat-8.5.35/apps/tomcat

创建docker build脚本

[root@tokyo-***tomcat-base]#catbuild-command.sh
#!/bin/bash

dockerbuild-t172.20.7.50/baseimages/centos-tomcat:8.5.35.

实行创建镜像文件:

[root@node2/app/dockerfile/web/tomcat/tomcat-base]#bashbuild-command.sh
SendingbuildcontexttoDockerdaemon9.625MB
Step1/5:FROM172.20.7.50/baseimages/centos7.5-jdk:8.162
--->9fb867ae8c39
Step2/5:LABELmaintainer"jiangwenhui<290070744@qq.com>"
--->Runningin9ce6fc4d2850
Removingintermediatecontainer9ce6fc4d2850
--->b68755061b28
Step3/5:RUNmkdir/apps
--->Runninginb483c6b127f2
Removingintermediatecontainerb483c6b127f2
--->605c1a048a5f
Step4/5:ADDapache-tomcat-8.5.35.tar.gz/apps
--->3c44f96ed41c
Step5/5:RUNln-s/apps/apache-tomcat-8.5.35/apps/tomcat
--->Runningin4c1aa39a6c92
Removingintermediatecontainer4c1aa39a6c92
--->9b3bc4f58cc9
Successfullybuilt9b3bc4f58cc9
Successfullytagged172.20.7.50/baseimages/centos-tomcat:8.5.35

利用创建好的镜像起一个容器检查下是否成功制作好,启动时加上-p暴露端口,在物理机上测试下

[root@node2/app/dockerfile/web/tomcat/tomcat-base]#dockerrun-it-p8802:8080172.20.7.50/baseimages/centos-tomcat:8.5.35bash
[root@917b2c2262a3/]#cd/apps/
[root@917b2c2262a3apps]#ll
total0
drwxr-xr-x9rootroot220Nov2222:08apache-tomcat-8.5.35
lrwxrwxrwx1rootroot26Nov2222:08tomcat->/apps/apache-tomcat-8.5.35
[root@917b2c2262a3apps]#./tomcat/bin/catalina.shstart
UsingCATALINA_BASE:/apps/tomcat
UsingCATALINA_HOME:/apps/tomcat
UsingCATALINA_TMPDIR:/apps/tomcat/temp
UsingJRE_HOME:/usr/local/jdk/jre
UsingCLASSPATH:/apps/tomcat/bin/bootstrap.jar:/apps/tomcat/bin/tomcat-juli.jar
Tomcatstarted.
相关TAG标签 Dockerfile 使用
上一篇:Ubuntu中IPFS私有多结点部署
下一篇:如何加快win10开机速度
相关文章
图文推荐

关于大家 | 联系大家 | 广告服务 | 投资合作 | 版权申明 | 在线帮助 | 网站地图 | 作品发布 | Vip技术培训 | 举报中心

版权所有: 澳门葡京手机版网址_澳门新莆京娱乐_www.88807.com - 点此进入--致力于做实用的IT技术学习网站

XML 地图 | Sitemap 地图