基于官方提供的centos 7.2.1511 基础镜像构建JDK 和tomcat 镜像,先构建JDK镜像,然后在基于JDK镜像构建tomcat镜像
# 下载基础镜像JDK:
docker pull centos
# 执行构建JDK镜像:
mkdir -pv /opt/dockerfile/{web/{nginx,tomcat,jdk.apache},system/{centos,ubuntu,redhat}}
# 清除<none> 类型的镜像:
1 )确认: docker images | grep "<none>" | awk '{print $3}'
2 )删除: docker rmi `docker images | grep "<none>" | awk '{print $3}'`
# 打一个基础镜像:
docker tag centos:latest centos:7.6.1810
# 编写Dockerfile:
vim dockerfile
from centos:7.6.1810
RUN yum install epel-release -y && yum install vim iotop bc gcc gcc-c++ glibc glibc-devel pcre \
pcre-devel openssl openssl-devel zip unzip zlib-devel netols \
lrzsz tree ntpdate telnet lsof tcpdump wget libevent libevent-devel \
bc systemd-devel bash-completion traceroute -y
RUN useradd nginx -u 2019 && useradd tomcat -u 2020
RUN rm -rf /etc/localtime && ln -sv /usr/share/javazi/Asia/Shanghai /etc/localtime
# 构建一个基础镜像:
docker build -t centos-base:v1 .
# 查看一个镜像有几层:
docker images history 镜像ID
# 创建JDK版本images:
vim Dockerfile
# For JDK Base
FROM centos-base:v1 # 这个根据自己的镜像来决定
MAINTAINER 57674891@qq.com
ADD jdk-8u192-linux-x64.tar.gz /usr/local/src
RUN ln -sv /usr/local/src/jdk1.8.0_192 /usr/local/jdk
ADD profile /etc/profile
ENV JAVA_HOME /usr/local/jdk
ENV JRE_HOME $JAVA_HOME/jre
ENV CLASSPATH $JAVA_HOME/lib/:$JRE_HOME/LIB/
ENV PATH $PATH:$JAVA_HOME/bin
# 构建JDK镜像:
vim docker_build.sh
docker build -t jdk-base:1.8.0.192 .
# 创建tomcat-base镜像
cd /opt/dockerfile/web/tomcat
wget https://archive.apache.org/dist/tomcat/tomcat-8/v8.5.39/bin/apache-tomcat-8.5.39.tar.gz
# 创建dockfile文件:
vim Dockerfile
FROM jdk-base:1.8.0.192
MAINTAINER 57674891@qq.com
RUN test -d /data/soft || mkdir -p /data/soft
RUN test -d /data/www || mkdir -p /data/www
ADD apache-tomcat-8.5.39.tar.gz /data/soft
RUN ln -svf /data/soft/apache-tomcat-8.5.39 /data/soft/tomcat
RUN chown -R tomcat.tomcat /data/soft/tomcat /data/soft/apache-tomcat-8.5.39 /data/www/
# 构建JDK镜像:
vim docker_build.sh
docker build -t tomcat-base:8.5.39 .
# 根据镜像创建容器:
docker run -ti --rm tomcat-base:8.5.39 bash
# 创建tomcat-app1 镜像:
FROM centos-base:v1
ADD jdk-8u192-linux-x64.tar.gz /usr/local/src
RUN ln -sv /usr/local/src/jdk1.8.0_192 /usr/local/jdk
ENV JAVA_HOME /usr/local/jdk
ENV JRE_HOME $JAVA_HOME/jre/
ENV CLASSPATH $JAVA_HOME/lib/:$JRE_HOME/LIB/
ENV PATH $PATH:$JAVA_HOME/bin
ADD profile /etc/profile
RUN rm -rf /etc/localtime && ln -snf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && echo "Asia/Shanghai" > /etc/timezone
###################################################################
# 运行脚本 docker_build.sh,构建镜像:
#!/bin/bash
docker build -t jdk-base:1.8.0.192 .
###################################################################
# 测试构建的镜像是否可以正常运行:
[root@node-2 tomcat-app1]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
tomcat-app1 v1 642ebdce8a40 About a minute ago 964MB
tomcat-base 8.5.39 507bbd514fc5 2 minutes ago 950MB
## 前台交互的方式测试一下镜像:
[root@node-2 tomcat-app1]# docker run -ti --rm -p 8080:8080 tomcat-app1:v1
10.10.5.136 hostname is start
Using CATALINA_BASE: /data/soft/tomcat
Using CATALINA_HOME: /data/soft/tomcat
Using CATALINA_TMPDIR: /data/soft/tomcat/temp
Using JRE_HOME: /usr/local/jdk/jre
Using CLASSPATH: /data/soft/tomcat/bin/bootstrap.jar:/data/soft/tomcat/bin/tomcat-juli.jar
Tomcat started.
127.0.0.1 localhost
::1 localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
172.17.0.2 8ef11a979732
## 验证是否正常运行:
[root@node-2 tomcat-app1]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
41bc8227215b tomcat-app1:v1 "/data/soft/tomcat/b…" 2 minutes ago Up 2 minutes 0.0.0.0:8080->8080/tcp, 8443/tcp funny_lalande
## 访问一下页面:
[root@node-2 tomcat-app1]# curl -I 127.0.0.1:8080/app1
HTTP/1.1 302
Location: /app1/
Transfer-Encoding: chunked
Date: Mon, 20 Jul 2020 10:59:33 GMT
http://IP:8080/app1
cd /opt/dockerfile/web/tomcat
cp -r tomcat-app1 tomcat-app2
# 修改文件内容:
1 code.tar.gz 内容要最新的
2
vim docker_build.sh
docker build -t tomcat-app2:v1 .
3 修改dockerfile:
vim Dockerfile
FROM tomcat-base:8.5.39
maintainer 57674891@qqcom
RUN test -d /data/www/webapps/app2 || mkdir -p /data/www/webapps/app2
ADD code.tar.gz /data/www/webapps/app2
ADD server.xml /data/soft/tomcat/conf
ADD run_tomcat.sh /data/soft/tomcat/bin/
RUN chown -R tomcat.tomcat /data/soft/* /data/www/webapps/app2
EXPOSE 8080 8443
CMD ["/data/soft/tomcat/bin/run_tomcat.sh"]
4 打包文件:
tar -zcvf code.tar.gz index.html
5 构建docker镜像:
sh docker_build.sh
6 测试镜像:
docker run -ti -p 8080:8080 --rm 镜像ID bash
7 访问页面:
http://10.10.5.136:8080/app2/index.html
# 以上重新就构建出来了一套新的业务应用
# 1 ) Dockerfile 里面的环境变量很重要,一定要在tomcat-base 基础的镜像完成之后 测试一下 镜像是否可以正常运行:
ENV JAVA_HOME /usr/local/jdk
ENV JRE_HOME $JAVA_HOME/jre/
ENV CLASSPATH $JAVA_HOME/lib/:$JRE_HOME/LIB/
ENV PATH $PATH:$JAVA_HOME/bin
ADD profile /etc/profile
# 2 ) JDK-base 打镜像的就要 一起设置 2种变量 /etc/profile 和 Dockerfile 里面的 ENV 变量
......
vim profile
export JAVA_HOME=/usr/local/jdk
export JRE_HOME=/usr/local/jdk/jre
export TOMCAT_HOME=/data/soft/tomcat
export PATH=$JAVA_HOME/bin/:$JAVA_HOME/jre/bin:$TOMCAT_HOME/bin:$PATH
export CLASSPATH=.:$CLASSPATH:$JAVA_HOME/lib:$JAVA_HOME/jre/lib:$JAVA_HOME/lib/tools.jar
手机扫一扫
移动阅读更方便
你可能感兴趣的文章