FROM busybox
MAINTAINER Jlive Liu "iliujun_live@163.com"
LABEL Description="jdk volume" Vendor="Jlive" Version="1.8"
VOLUME ["/opt/jdk"]
ADD . /opt
root@router:docker#docker build -t jdk1.8 .
Sending build context to Docker daemon 366.3 MB
Step 1 : FROM busybox
---> 2b8fd9751c4c
Step 2 : MAINTAINER Jlive Liu "iliujun_live@163.com"
---> Running in 119edb8e2e06
---> cf23614e0267
Removing intermediate container 119edb8e2e06
Step 3 : LABEL Description "jdk volume" Vendor "Jlive" Version "1.8"
---> Running in deb99e7bd0bf
---> 95a32896c940
Removing intermediate container deb99e7bd0bf
Step 4 : ADD . /opt
---> bf8026d1f466
Removing intermediate container 0192f52ac659
Successfully built bf8026d1f466
root@router:docker#docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
jdk1.8 latest bf8026d1f466 3 seconds ago 366.1 MB
2.tomcat镜像
rm -rf /docker_tmp/*
curl http://apache.fayea.com/tomcat/tomcat-9/v9.0.0.M8/bin/apache-tomcat-9.0.0.M8.tar.gz |tar -zxvf - -C /docker_tmp
mv /docker_tmp/apache-tomcat-9.0.0.M8/ /docker_tmp /tomcat
cd /docker_tmp
FROM ubuntu:14.04
MAINTAINER "Jlive Liu"
LABEL Description="Apache tomcat" Vendor="Jlive" Version="9"
ADD . /opt
# Define ENV
ENV LANG=C.UTF-8
ENV CATALINA_HOME=/opt/tomcat
ENV PATH=$CATALINA_HOME/bin:$PATH
# Exporse ports
EXPOSE 8080
# Volume
VOLUME ["$CATALINA_HOME/conf","$CATALINA_HOME/logs","$CATALINA_HOME/webapps","$CATALINA_HOME/work"]
# Define entrypoint
ENTRYPOINT ["catalina.sh", "run"]
root@router:hello#ls
Dockerfile tomcat/
root@router:hello#docker build -t tomcat9 .
Sending build context to Docker daemon 13.54 MB
Step 1 : FROM busybox
---> 2b8fd9751c4c
Step 2 : MAINTAINER Jlive Liu "iliujun_live@163.com"
---> Using cache
---> cf23614e0267
Step 3 : LABEL Description "tomcat image" Vendor "Jlive" Version "9"
---> Running in ba6062d0c446
---> c48539035ef3
Removing intermediate container ba6062d0c446
Step 4 : ADD ./tomcat /opt
---> 00c17f172892
Removing intermediate container 57df6d279975
Step 5 : ENV CATALINA_HOME /opt/tomcat
---> Running in 6a7b7a72ae8c
---> 5a7985a3d994
Removing intermediate container 6a7b7a72ae8c
Step 6 : ENV PATH $CATALINA_HOME/bin:$PATH
---> Running in 378a772abd7c
---> e2c75bbc7079
Removing intermediate container 378a772abd7c
Step 7 : EXPOSE 8080
---> Running in 209a3dfcd502
---> cd643ba3f4ed
Removing intermediate container 209a3dfcd502
Step 8 : VOLUME $CATALINA_HOME/conf $CATALINA_HOME/logs $CATALINA_HOME/webapps $CATALINA_HOME/work
---> Running in ca0c8314da0f
---> fea57546a257
Removing intermediate container ca0c8314da0f
Step 9 : ENTRYPOINT catalina.sh start
---> Running in 3f5bf58e7a57
---> 60da4a5c7203
Removing intermediate container 3f5bf58e7a57
Successfully built 60da4a5c7203
root@router:hello#docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
tomcat9 latest fa1a3de66f7c About a minute ago 228.4 MB
jdk1.8 latest bf8026d1f466 47 minutes ago 366.1 MB
busybox latest 2b8fd9751c4c 2 weeks ago 1.093 MB
3.运行tomcat
docker cp mytomcat:/opt/tomcat/conf /opt/tomcat/
docker rm -f mytomcat
为了最大程度的可定制性,可以将配置文件,log等目录映射到本地,当然如果应用比较固定统一则可以直接固化到image里
docker run -tid --restart=always \
-e JAVA_HOME=/opt/jdk \
-e JAVA_OPTS="-Xms1536m -Xmx1536m -XX:MaxPermSize=512m" \
-p 8080:8080 \
--memory-swappiness=1 \
--ulimit nofile=20480:40960 \
--volumes-from jdk1.8 \
-v /opt/tomcat/conf:/opt/tomcat/conf \
-v /opt/tomcat/logs:/opt/tomcat/logs \
--name mytomcat \
tomcat9
提示:还可以加上shell
docker run -tid --restart=always --name shell busybox
--volumes-from shell \
4.部署应用
以war包应用为例,测试这里用jenkins
i.代码打包为docker volume
rm -rf /docker_tmp/*
cd /docker_tmp
curl http://ftp.tsukuba.wide.ad.jp/software/jenkins/war-stable/2.7.1/jenkins.war -o /tmp/jenkins.war
mkdir jenkins
unzip /tmp/jenkins.war -d jenkins
cat >Dockerfile <<HERE
FROM busybox
MAINTAINER "Jlive Liu"
LABEL Description="jenkins" Vendor="Jlive" Version="2.7.1"
VOLUME ["/opt/tomcat/webapps/jenkins"]
RUN mkdir -p /opt/tomcat/webapps
ADD . /opt/tomcat/webapps
HERE
docker build -t jenkins .
注意:jenkins只是tomcat中的一个应用,打包时是精细打包,所以挂载卷的时只会把/opt/tomcat/webapps/jenkins挂到/opt/tomcat/webapps下,tomcat默认的demo是不会覆盖的
正常环境下,应该是通过ant脚本等自动编译打包工具将代码自动打成war包,再将war包打成docker volume发布
root@router:webapps#docker build -t jenkins .
Sending build context to Docker daemon 81.66 MB
Step 1 : FROM busybox
---> 2b8fd9751c4c
Step 2 : MAINTAINER "Jlive Liu"
---> Using cache
---> b967b67fc736
Step 3 : LABEL Description "jenkins" Vendor "Jlive" Version "2.7.1"
---> Using cache
---> 1bae96ee2100
Step 4 : VOLUME /opt/tomcat/webapps/jenkins
---> Using cache
---> 8dd4d9ab79d2
Step 5 : RUN mkdir -p /opt/tomcat/webapps
---> Using cache
---> d06a04c638c2
Step 6 : ADD . /opt/tomcat/webapps
---> 573ad293c3d5
Removing intermediate container 4dd659de46c6
Successfully built 573ad293c3d5
提示:便于批量部署,建议将打包好的代码上传到docker-distribution
ii.发布代码
docker run -tid --restart=always --name app jenkins
docker rm -f mytomcat
docker run -tid --restart=always \
-e JAVA_HOME=/opt/jdk \
-e JAVA_OPTS="-Xms1536m -Xmx1536m -XX:MaxPermSize=512m" \
-p 8080:8080 \
--memory-swappiness=1 \
--ulimit nofile=20480:40960 \
--volumes-from jdk1.8 \
--volumes-from app \
-v /opt/tomcat/conf:/opt/tomcat/conf \
-v /opt/tomcat/logs:/opt/tomcat/logs \
--name mytomcat \
tomcat9
root@router:webapps#docker cp mytomcat:/root/.jenkins/secrets/initialAdminPassword .
root@router:webapps#cat initialAdminPassword
895fff6eed77400fb5ba1aa45bc22dca
jenkins-2.7.1更新比较大,UI方面更加清爽,和最新的zabbix3.x的感觉类似,初始化时是随机密码,并且生成在docker里,所以需要进入docker或cp出来查看
国内安装jenkins插件不容易,可以将容器commit为image
root@router:webapps#docker commit -m "init ok" app app_init
sha256:3460468153aa55c93fba8cefe318bedd08fd8b1a52e7738ee3b6680aed74a5ac
root@router:webapps#docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
app_init latest 3460468153aa 4 seconds ago 69.94 MB
jenkins latest 573ad293c3d5 46 minutes ago 81.77 MB