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
Step 2 : MAINTAINER Jlive Liu "iliujun_live@163.com"
Removing intermediate container 119edb8e2e06
Step 3 : LABEL Description "jdk volume" Vendor "Jlive" Version "1.8"
Removing intermediate container deb99e7bd0bf
Step 4 : ADD . /opt
Removing intermediate container 0192f52ac659
Successfully built bf8026d1f466
root@router:docker#docker images
REPOSITORY
jdk1.8
2.tomcat镜像
rm -rf /docker_tmp/*
curl
mv
/docker_tmp/apache-tomcat-9.0.0.M8/
/docker_tmp
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
root@router:hello#docker build -t tomcat9 .
Sending build context to Docker daemon 13.54 MB
Step 1 : FROM busybox
Step 2 : MAINTAINER Jlive Liu "iliujun_live@163.com"
Step 3 : LABEL Description "tomcat image" Vendor "Jlive" Version "9"
Removing intermediate container ba6062d0c446
Step 4 : ADD ./tomcat /opt
Removing intermediate container 57df6d279975
Step 5 : ENV CATALINA_HOME /opt/tomcat
Removing intermediate container 6a7b7a72ae8c
Step 6 : ENV PATH $CATALINA_HOME/bin:$PATH
Removing intermediate container 378a772abd7c
Step 7 : EXPOSE 8080
Removing intermediate container 209a3dfcd502
Step 8 : VOLUME $CATALINA_HOME/conf $CATALINA_HOME/logs $CATALINA_HOME/webapps $CATALINA_HOME/work
Removing intermediate container ca0c8314da0f
Step 9 : ENTRYPOINT catalina.sh start
Removing intermediate container 3f5bf58e7a57
Successfully built 60da4a5c7203
root@router:hello#docker images
REPOSITORY
tomcat9
jdk1.8
busybox
3.运行tomcat
docker cp mytomcat:/opt/tomcat/conf /opt/tomcat/
docker rm -f mytomcat
为了最大程度的可定制性,可以将配置文件,log等目录映射到本地,当然如果应用比较固定统一则可以直接固化到image里
docker run -tid --restart=always
提示:还可以加上shell
docker run -tid --restart=always --name shell busybox
4.部署应用
以war包应用为例,测试这里用jenkins
i.代码打包为docker volume
rm -rf /docker_tmp/*
cd /docker_tmp
curl
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
Step 2 : MAINTAINER "Jlive Liu"
Step 3 : LABEL Description "jenkins" Vendor "Jlive" Version "2.7.1"
Step 4 : VOLUME /opt/tomcat/webapps/jenkins
Step 5 : RUN mkdir -p /opt/tomcat/webapps
Step 6 : ADD . /opt/tomcat/webapps
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
root@router:webapps#docker cp mytomcat:/root/.jenkins/secrets/initialAdminPassword .
root@router:webapps#cat
initialAdminPassword
895fff6eed77400fb5ba1aa4
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:3460468153aa55c93fba8cef
root@router:webapps#docker images
REPOSITORY
app_init
jenkins