Docker镜像分层构建
1.构建基础镜像
mkdir /opt/dockerfile/system/ubuntu
vim /opt/dockerfile/system/ubuntu/Dockerfile
#Ubuntu Base Image
FROM ubuntu:20.04
MAINTAINER shitouweb
RUN apt-get update && apt-get install -y wget libpcre3 libpcre3-dev openssl libssl-dev zlib1g-dev iproute2 net-tools iotop gcc g++ automake build-essential unzip tzdata
RUN groupadd www -g 2020 && useradd www -u 2020 -g www
vim /opt/dockerfile/system/ubuntu/build-command.sh
#!/bin/bash
sudo docker build -t ubuntu-base:20.04 .
2.构建JDK镜像
mkdir /opt/dockerfile/web/jdk/jdk-8U251
vim /opt/dockerfile/web/jdk/jdk-8U251/Dockerfile
#JDK-8U251
FROM ubuntu-base:20.04 MAINTAINER shitouweb "" ADD jdk-8u251-linux-x64.tar.gz /usr/local/src/ RUN ln -sv /usr/local/src/jdk1.8.0_251 /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 RUN rm -rf /etc/localtime && ln -snf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
vim /opt/dockerfile/web/jdk/jdk-8U251/profile #可以在虚拟机中先搭建java环境变量然后复制profile文件即可,添加以下
export JAVA_HOME=/usr/local/jdk
export CLASSPATH=$JAVA_HOME/jre/lib/rt.jar:$JRE_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
export PATH=$PATH:$JAVA_HOME/bin
vim /opt/dockerfile/web/jdk/jdk-8U251/build-command.sh
#!/bin/bash
sudo docker build -t ubuntu20.04-jkd:8U251 .
3.构建Tomcat镜像
mkdir /opt/dockerfile/web/tomcat/tomcat-9.0.35
vim /opt/dockerfile/web/tomcat/tomcat-9.0.35/Dockerfile
FROM ubuntu20.04-jkd:8U251
LABEL shitouweb
ADD apache-tomcat-9.0.35.tar.gz /apps
RUN ln -sv /apps/apache-tomcat-9.0.35 /apps/tomcat
vim /opt/dockerfile/web/tomcat/tomcat-9.0.35/build-command.sh
#!/bin/bash
sudo docker build -t ubuntu20.04-tomcat:9.0.35 .
4.构建业务镜像
mkdir /opt/dockerfile/web/tomcat/tomcat-app1
vim /opt/dockerfile/web/tomcat/tomcat-app1/Dockerfile
FROM ubuntu20.04-tomcat:9.0.35
LABEL shitouweb ADD server.xml /apps/tomcat/conf/server.xml ADD docker-1.tar.gz /data/tomcat/webapps ADD run_tomcat.sh /apps/tomcat/bin/run_tomcat.sh RUN chown -R www:www /apps/ EXPOSE 8080 8005 CMD ["/apps/tomcat/bin/run_tomcat.sh"]
vim /opt/dockerfile/web/tomcat/tomcat-app1/buile-command.sh
#!/bin/bash
sudo docker build -t ubuntu20.04-tomcat0.0.35-webapps:1 .
vim /opt/dockerfile/web/tomcat/tomcat-app1/run_tomcat.sh可执行文件必须增加执行权限,否则在容器中不能执行
su -c ‘/apps/tomcat/bin/catalina.sh start‘ - www
tail -f /etc/hosts
修改server.xml
<Host name="localhost" appBase="/data/tomcat/webapps"
unpackWARs="false" autoDeploy="false">
注意:在DockerFile中写入的CMD后面的命令不执行主要是因为启动的时候指定了shell