Docker实践笔记03:JAVA环境DockerFile制作

镜像库

https://hub.docker.com/

系统仓库
centos7iongdoc/centos7
java8iongdoc/java8
java11iongdoc/java11
tomcat10iongdoc/tomcat

镜像仓库DockerFIle

https://github.com/CiaraLi/dockerFiles

Centos7 镜像

  • 基础环境 DockerFile
    包含的默认的开机配置环境配置,基础通用组件安装
FROM centos:7
ENV container docker
RUN (cd /lib/systemd/system/sysinit.target.wants/; for i in *; do [ $i == \
systemd-tmpfiles-setup.service ] || rm -f $i; done); \
rm -f /lib/systemd/system/multi-user.target.wants/*;\
rm -f /etc/systemd/system/*.wants/*;\
rm -f /lib/systemd/system/local-fs.target.wants/*; \
rm -f /lib/systemd/system/sockets.target.wants/*udev*; \
rm -f /lib/systemd/system/sockets.target.wants/*initctl*; \
rm -f /lib/systemd/system/basic.target.wants/*;\
rm -f /lib/systemd/system/anaconda.target.wants/*;\
rm -f /etc/localtime && ln -sf  /usr/share/zoneinfo/Asia/Shanghai /etc/localtime ;\
echo "Hello  Centos --Ciara--"  > /etc/motd ; \
echo "set encoding=utf-8" > ~/.vimrc;\
mkdir -p /var/data; 
WORKDIR /var/data
COPY  ./*   ./ 
VOLUME [ "/sys/fs/cgroup" ,"var/data"]
CMD ["/usr/sbin/init"]
  • 其中 自定义环境配置如下
    1、基础环境包含的一些设置时区 、开机问候语、字符集设置
rm -f /etc/localtime && ln -sf  /usr/share/zoneinfo/Asia/Shanghai /etc/localtime ;\
echo "Hello  Centos --Ciara--"  > /etc/motd ; \
echo "set encoding=utf-8" > ~/.vimrc; 

Java8环境镜像 在线安装

  • 创建DOckerFile
#镜像源
FROM iongdoc/centos7
#安装JAVA
WORKDIR /var/data 
COPY  ./src/* ./src/
RUN ./src/java8_install.sh;\
    yum clean all;
#数据卷
VOLUME ["/var/logs/"]
CMD [""]
  • 创建Java8安装脚本
#!/bin/bash
# 路径设置
homeDir="/var/data"
file="jdk-8u141-linux-x64.tar.gz"
jdk="jdk1.8.0_141"
#卸载旧版本
check= `rpm -qa|grep java`
if [ "$result" != "" ]
then
       echo "卸载旧版本   doing..."
       rpm -qa|grep java | xargs   yum -y remove
       echo "OK..."
fi
mkdir -p /usr/local/java
#安装新版本
yum install -y  wget
echo "开始安装Java8  doing..."
cd  $homeDir/src
wget --no-cookies --no-check-certificate --header "Cookie: gpw_e24=http%3A%2F%2Fwww.oracle.com%2F; oraclelicense=accept-securebackup-cookie" "http://download.oracle.com/otn-pub/java/jdk/8u141-b15/336fa29ff2bb4ef291e347e091f7f4a7/jdk-8u141-linux-x64.tar.gz"
# 复制准备好的文件
tar -zxvf  $homeDir/src/$file
mv ./$jdk  /usr/local/java/jdk8
# 备份
cp /etc/profile /etc/profile.bk.$(date +%F'-'%H'-'%M'-'%S)
# 环境变量
echo "export JAVA_HOME=/usr/local/java/jdk8"   >> /etc/profile
echo "export JRE_HOME=$JAVA_HOME/jre "   >> /etc/profile
echo "export PATH=$PATH:$JAVA_HOME/bin "   >> /etc/profile
echo "export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar"   >> /etc/profile
source /etc/profile
ln -s /usr/local/java/jdk8/bin/java /usr/bin/java
echo "Javay已成功安装至  /usr/local/java/jdk8 目录  OK ..."
# 测试
java -version
# 删除源文件
rm -rf /var/data/src/*
yum remove -y wget

Java11 环境镜像 离线包版本

  • 需要在官网下载jdk源码包 https://www.oracle.com/java/technologies/javase-jdk11-downloads.html
  • 创建DOckerFile
#镜像源
FROM iongdoc/centos7
#安装JAVA
WORKDIR /var/data 
COPY  ./src/* ./src/
RUN ./src/java11_install.sh;\
    yum clean all;
#数据卷
VOLUME ["/var/logs/"]
CMD ["date"]
  • 创建Java11安装脚本java11_install.sh
 #!/bin/bash
#路径设置
homeDir="/var/data"
file="jdk-11.0.12_linux-x64_bin.tar.gz"
jdk="jdk-11.0.12"
#卸载旧版本
check= `rpm -qa|grep java`
 if [ "$result" != "" ]
then
        echo "卸载旧版本   doing..."
        rpm -qa|grep java | xargs   yum -y remove
        echo "OK..."
fi
mkdir -p /usr/local/java
#安装新版本
yum install -y  wget
echo "开始安装Java8  doing..."
cd  $homeDir/src
# 复制准备好的文件
tar -zxvf  $homeDir/src/$file
mv ./$jdk  /usr/local/java/jdk11
# 备份
cp /etc/profile /etc/profile.bk.$(date +%F'-'%H'-'%M'-'%S)
# 环境变量
echo "export JAVA_HOME=/usr/local/java/jdk11"   >> /etc/profile
echo "export JRE_HOME=$JAVA_HOME/jre "   >> /etc/profile
echo "export PATH=$PATH:$JAVA_HOME/bin "   >> /etc/profile
echo "export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar"   >> /etc/profile
source /etc/profile
ln -s /usr/local/java/jdk11/bin/java /usr/bin/java
echo "Javay已成功安装至  /usr/local/java/jdk11 目录  OK ..."
#测试
java -version
#删除源文件
rm -rf /var/data/src/*

使用方式

1、下载Git仓库
2、在自己想要的环境下面创建容器。如:

cd java8
docker build -t java8 ./
docker run -it --name java java8  /bin/bash

tomcat 环境

  • DockerFile
#镜像源
FROM iongdoc/centos7
#安装JAVA
WORKDIR /var/data 
COPY  ./src/* ./src/
RUN /bin/chmod +x ./src/tomcat_install.sh;\
    ./src/tomcat_install.sh;\
    yum clean all;
#数据卷 默认,可以在run时重新指定目录
VOLUME ["/var/data/logs/"]
#声明开放端口,需要在run时做映射
EXPOSE 8080 
CMD ["/usr/local/tomcat/bin/catalina.sh","run"]
  • tomcat安装脚本
#!/bin/bash
homeDir="/var/data" 
jdk="java8"
tomcat="tomcat"
webapp="webapp"
check= `rpm -qa|grep java`
#-----安装JDK  ------
# 卸载旧版本  
 if [ "$result" != "" ]
 then
	echo "卸载旧版本   doing..." 
	rpm -qa|grep java | xargs   yum -y remove
	echo "OK..." 
fi
cd  $homeDir/src
yum install -y  wget
# 安装新版本  then
mkdir -p /usr/local/java  
echo "开始安装Java8  doing..." 
# 复制准备好src 的文件jdk-8u141-linux-x64.tar.gz  或者在线下载
if [ !  -f  "$homeDir/src/jdk-8u141-linux-x64.tar.gz" ] 
then
	 wget --no-cookies --no-check-certificate --header "Cookie: gpw_e24=http%3A%2F%2Fwww.oracle.com%2F; oraclelicense=accept-securebackup-cookie" "http://download.oracle.com/otn-pub/java/jdk/8u141-b15/336fa29ff2bb4ef291e347e091f7f4a7/jdk-8u141-linux-x64.tar.gz"   
fi
# 复制准备好的文件 
tar -zxvf  $homeDir/src/jdk-8u141-linux-x64.tar.gz  
mv $homeDir/src/jdk1.8.0_141  /usr/local/java/$jdk
# 备份
cp /etc/profile /etc/profile.bk.$(date +%F'-'%H'-'%M'-'%S)
# 环境变量
echo "export JAVA_HOME=/usr/local/java/$jdk"   >> /etc/profile
echo "export JRE_HOME=$JAVA_HOME/jre "   >> /etc/profile
echo "export PATH=$PATH:$JAVA_HOME/bin "   >> /etc/profile
echo "export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar"   >> /etc/profile
source /etc/profile
ln -s /usr/local/java/$jdk/bin/java /usr/bin/java
echo "Java已成功安装至  /usr/local/java/$jdk 目录  OK ..." 
java -version
#----安装tomcat ------
# 复制准备好的文件apache-tomcat-10.0.10.tar.gz  或者在线下载 
if [ !   -f  "$homeDir/src/apache-tomcat-10.0.10.tar.gz" ]  
then
  wget https://dlcdn.apache.org/tomcat/tomcat-10/v10.0.10/bin/apache-tomcat-10.0.10.tar.gz
fi
tar -zxvf  $homeDir/src/apache-tomcat-10.0.10.tar.gz 
mv $homeDir/src/apache-tomcat-10.0.10  /usr/local/$tomcat 
chmod +x /usr/local/$tomcat/bin/* 
#设置目录
mv /usr/local/$tomcat/webapps  $homeDir/webapps
ln -s $homeDir/webapps /usr/local/$tomcat/webapps
ln -s $homeDir/logs  /usr/local/tomcat/logs
echo "Tomcat已成功安装至:/usr/local/$tomcat 目录  
	  WebApp路径:$homeDir/webapp
	  前台运行: /usr/local/tomcat/bin/catalina.sh run
	  启动命令: /usr/local/tomcat/bin/catalina.sh start
	  停止命令: /usr/local/tomcat/bin/catalina.sh  stop
      OK ..."  
#删除安装包 
rm -rf  $homeDir/src/*
yum remove -y wget
  • 启动命令
docker build -t tomcat ./
 docker run -itd --name tomcat -p 8080:8080  tomcat 
  • 查看端口映射
[root@bogon ~]# docker port 971bcc4bd063 8080
 0.0.0.0:8080
 :::8080

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值