docker实例之jenkins
说明:使用Dockerfile构建Jenkins实战记录
环境:Ubuntu16.04
1.部署包准备
将如下部署包上传到/home/www/jenkins目录下并解压后重命名
jdk-8u91-linux-x64.tar.gz
apache-tomcat-9.0.8.tar.gz
apache-maven-3.5.3-bin.tar.gz
jenkins.war
tar -zxvf jdk-8u91-linux-x64.tar.gz && mv jdk-8u91 jdk1.8
tar -zxvf apache-tomcat-9.0.8.tar.gz && mv apache-tomcat-9.0.8 tomcat9
tar -zxvf apache-maven-3.5.3-bin.tar.gz && mv apache-maven-3.5.3 maven3
2.编写Dockerfile
#cat Dockerfile
FROM ubuntu
MAINTAINER test@test.com
COPY jdk1.8 /usr/local/jdk1.8
COPY maven3 /usr/local/maven3
COPY tomcat9 /usr/local/tomcat9
ENV JAVA_HOME /usr/local/jdk1.8
ENV PATH $JAVA_HOME/bin:$PATH
ENV MAVEN_HOME /usr/local/maven3
ENV PATH $PATH:$MAVEN_HOME/bin
EXPOSE 8080
CMD /usr/local/tomcat9/bin/startup.sh
注意:这里最好使用拷贝的方式,不要使用ADD自动解压的方式,自动解压有时会出错,而且拷贝的是该目录下的文件,并不拷贝该目录,所以后面要加上目录
提醒:如果这里遇到环境变量不生效,可以将其添加到/root/.bashrc
3.构建镜像
docker build -t jenkins:1.0 . 最后这个.不能忘记和省略
构建实际过程如下
# docker build -t jenkins:1.0 .
Sending build context to Docker daemon 462.1MB
Step 1/11 : FROM ubuntu
---> 735f80812f90
Step 2/11 : MAINTAINER test@test.com
---> Running in 62a7dbef3184
Removing intermediate container 62a7dbef3184
---> 82568cd558f0
Step 3/11 : COPY jdk1.8 /usr/local/jdk1.8
---> 945586a8dcff
Step 4/11 : COPY maven3 /usr/local/maven3
---> bc184ee94173
Step 5/11 : COPY tomcat9 /usr/local/tomcat9
---> fc6d582bdb64
Step 6/11 : ENV JAVA_HOME /usr/local/jdk1.8
---> Running in f5cad0490d63
Removing intermediate container f5cad0490d63
---> 1f6e56c8bb71
Step 7/11 : ENV PATH $JAVA_HOME/bin:$PATH
---> Running in 6b060ca76756
Removing intermediate container 6b060ca76756
---> 1e170137b622
Step 8/11 : ENV MAVEN_HOME /usr/local/maven3
---> Running in d917c5017e3f
Removing intermediate container d917c5017e3f
---> 3acf26942c00
Step 9/11 : ENV PATH $PATH:$MAVEN_HOME/bin
---> Running in 73307a90b9b0
Removing intermediate container 73307a90b9b0
---> 3f6aaf6bddb6
Step 10/11 : EXPOSE 8080
---> Running in b9e654185772
Removing intermediate container b9e654185772
---> 4ed3514052be
Step 11/11 : CMD /usr/local/tomcat9/bin/startup.sh
---> Running in b8266f7f5e8e
Removing intermediate container b8266f7f5e8e
---> e9095cf5bd08
Successfully built e9095cf5bd08
Successfully tagged jenkins:1.0
确认上面构建的容器
# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
jenkins 1.0 2efe6942d56c About a minute ago 544MB
4.创建容器
docker run --privileged=true --net openwallet -d -it -p 10000:8080 -v /usr/local/tomcat9/webapps:/usr/local/tomcat9/webapps --name openwallet_jenkins jenkins:1.0 /bin/bash
实际运行效果如下:
# docker run --privileged=true --net openwallet -d -it -p 10000:8080 -v /usr/local/tomcat9/webapps:/usr/local/tomcat9/webapps --name openwallet_jenkins jenkins:1.0 /bin/bash
4febf54c0fbfa3b067fda26308eb06b585bc296c378448ff40241a5dac965603
5.防火墙策略
在宿主机上安装ufw防火墙并添加规则
宿主机的ip地址是192.168.30.36
安装ufw防火墙工具#sudo apt-get install ufw
#ufw enable
#ufw default deny
#ufw status
Status: active
添加策略
ufw allow from 192.168.30.36 to any port 8080
6.启动容器
docker start openwallet_jenkins 正常不用执行这个,上面执行之后会自动启动容器以及容器中的程序
7.确认容器中的程序运行是否正常
docker exec -it openwallet_jenkins /bin/bash
mvn -version
java -version
ps -ef |grep tomcat
如果Tomcat没启动,则需要手动启动下cd /usr/local/tomcat9/bin/startup.sh
确认tomcat9/webapps目录下是否有jenkins.war文件,如果没有则需要拷贝
docker cp jenkins.war openwallet_jenkins:/usr/local/tomcat9/weapps/
8.登陆Jenkins并做对应设置
http://192.168.30.36:10000/jenkins访问出现如下图片,显示为离线状态,解决方法如下:
http://192.168.30.36:10000/jenkins/pluginManager/advanced 进入之后拉到最下面将https改成http提交之后重启Tomcat
再次打开http://192.168.30.36:10000/jenkins正常按照提示到docker容器中执行命令获取密码
# cat /root/.jenkins/secrets/initialAdminPassword
8391c66d057244fca2a775de532050b3
输入上面获取的密码按要求往下进行即可
安装插件的时候选择“安装推荐的插件”----这一步持续差不多五分钟后就自动跳转到设置账号密码的地方了
之后按照提示操作即可