下载镜像并初始化系统
docker pull centos:7.2.1511
测试启动容器
docker run -it docker.io/centos:7.2.1511 /bin/bash
创建目录环境,宿主机下
mkdir /opt/dockerfile/{web/{nginx,tomcat,jdk,apache},system/{centos,ubuntu,redhat}} -pv
cd /opt/dockerfile/web/jdk/
编写 Dockerfile
vim ./Dockerfile
#"#"为注释, 等于 shell 脚本的中# #除了注释行之外的第一行,必须是 From xxx (xxx 是基础镜像)
#第一行先定义基础镜像,后面的本地有效的镜像名,如果本地没 有会从远程仓库下载,第一行很重要
#JDK Base Image
From centos:7.2.1511
MAINTAINER silence 2178937492@qq.com
ADD jdk-8u241-linux-x64.tar.gz /usr/local/src/
RUN ln -sv /usr/local/src/jdk1.8.0_241 /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 && echo "Asia/Shanghai" > /etc/timezone
上传安装包及profile文件
cp -a /etc/profile /opt/dockerfile/web/jdk
vim /profile
export JAVA_HOME=/usr/local/jdk
export JRE_HOME=$JAVA_HOME/jre
执行构建镜像
vim jdk-bulid.sh
#!/bin/bash
docker build -t centos-jdk:v1 .
bash jdk-bulid.sh
查看是否生成本地镜像
docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos-jdk v1 4e7cef60455a 13 hours ago 598MB
从镜像启动容器
docker run -it centos-jdk:v1 bash
[root@4eebcd4459a0 /]# java -version
Java(TM) SE Runtime Environment (build 1.8.0_241-b07)
Java HotSpot(TM) 64-Bit Server VM (build 25.241-b07, mixed mode)
上传镜像到hub和harbor
docker login -u silencegan
docker push 172.24.77.241/centos/centos-7.2.1511-jdk1.8.0.241
从 JDK 镜像构建 tomcat 8 Base 镜像
编辑 Dockerfile
cd /opt/dockerfile/web/tomcat
vim Dockerfile
#Tomcat Base Image
FROM centos-jdk:v1
RUN useradd www -u 2000
#ADD del_tomcatlog.sh /root/script
#RUN echo "0 2 * * * /bin/bash /root/script/del_tomcatlog.sh &> /dev/null" >>
/var/spool/cron/root
#env settinf
ENV TZ "Asia/Shanghai"
ENV LANG en_US.UTF-8
ENV TERM xterm
ENV TOMCAT_MAJOR_VERSION 8ENV TOMCAT_MINOR_VERSION 8.5.53
ENV CATALINA_HOME /apps/tomcat
ENV APP_DIR ${CATALINA_HOME}/webapps
#tomcat settinf
RUN mkdir /apps
ADD apache-tomcat-8.5.53.tar.gz /apps
RUN ln -sv /apps/apache-tomcat-8.5.53 /apps/tomcat
上传 tomcat 压缩包
通过脚本构建 tomcat 基础镜像
vim tomcat-build.sh
#!/bin/bash
docker build -t tomcat-base:v1 .
执行构建
bash tomcat-build.sh
查看
REPOSITORY TAG IMAGE ID CREATED SIZE
tomcat-base v1 7c9c929e9b42 8 seconds ago 613MB
构建业务镜像 1 :创建 tomcat-app1 和 tomcat-app2 两个目录,代表不同的两个基于 tomcat 的业务
mkdir -pv /opt/dockerfile/web/tomcat/{tomcat-app1,tomcat-app2}
cd /opt/dockerfile/web/tomcat/tomcat-app1
vim Dockerfile
#Tomcat Web Image
FROM tomcat-base:v1
ADD run_tomcat.sh /apps/tomcat/bin/run_tomcat.sh
ADD myapp/* /apps/tomcat/webapps/myapp/
RUN chown www.www /apps/ -R
CMD ["/apps/tomcat/bin/run_tomcat.sh"]
EXPOSE 8080 8009
准备自定义 myapp 页面
mkdir myapp
echo "Tomcat Web Page1" > myapp/index.html
cat myapp/index.html
准备容器启动执行脚本
vim run_tomcat.sh
chmod +x run_tomcat.sh
#!/bin/bash
echo "172.24.77.242 tomcat.silence.com" >> /etc/hosts
echo "nameserver 223.5.5.5" > /etc/resolv.conf
su - root -c "/apps/tomcat/bin/catalina.sh start"
su - root -c "tail -f /etc/hosts"
准备构建脚本
vim build-command.sh
#!/bin/bash
docker build -t tomcat-web:app1 .
执行构建
bash build-command.sh
从镜像启动容器测试
docker run -it -d -p 8888:8080 tomcat-web:app1
测试页面
http://172.24.77.242:8888/myapp/
构建业务镜像 2
cd /opt/dockerfile/web/tomcat/tomcat-app2
vim Dockerfile
#Tomcat Web2 Image
FROM tomcat-base:v1
ADD run_tomcat.sh /apps/tomcat/bin/run_tomcat.sh
ADD myapp/* /apps/tomcat/webapps/myapp/
RUN chown www.www /apps/ -R
CMD ["/apps/tomcat/bin/run_tomcat.sh"]
EXPOSE 8080 8009
准备自定义页面
mkdir myapp
echo "Tomcat Web Page2" > myapp/index.html
cat myapp/index.html
准备容器启动执行脚本
vim run_tomcat.sh
chmod +x run_tomcat.sh
#!/bin/bash
echo "172.24.77.242 tomcat.silence.com" >> /etc/hosts
echo "nameserver 223.5.5.5" > /etc/resolv.conf
su - root -c "/apps/tomcat/bin/catalina.sh start"
su - root -c "tail -f /etc/hosts"
准备容器启动执行脚本
vim build-command.sh
#!/bin/bash
docker build -t tomcat-web:app2 .
执行构建
bash build-command.sh
从镜像启动容器测试
docker run -it -d -p 8889:8080 tomcat-web:app2
测试页面
http://172.24.77.242:8889/myapp/