准备了2台服务器
192.168.202.130: 安装有 nexus 私有仓库,jenkins
192.168.202.128: 拉取docker镜像,启动
1. 安装jenkins
2. 创建springboot项目
pom配置
docker.host docker 远程访问地址
docker.registry docker私有仓库
docker.username 私有仓库nexus账号
docker.username 私有仓库nexus密码
<properties>
<java.version>1.8</java.version>
<docker.plugin.version>0.32.0</docker.plugin.version>
<docker.host>http://192.168.202.130:2375</docker.host>
<docker.registry>192.168.202.130:8082</docker.registry>
<docker.namespace>library</docker.namespace>
<docker.username>admin</docker.username>
<docker.password>admin</docker.password>
</properties>
spring-boot-maven-plugin 插件打包docker
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<!-- 发现io.fabric8更好用,换了一个 -->
<configuration>
<image>
<!--配置镜像名称-->
<name>${docker.registry}/boot/${project.name}:${project.version}</name>
<!--镜像打包完成后自动推送到镜像仓库-->
<publish>true</publish>
</image>
<docker>
<!--Docker远程管理地址-->
<host>${docker.host}</host>
<!--不使用TLS访问-->
<tlsVerify>false</tlsVerify>
<!--Docker推送镜像仓库配置-->
<publishRegistry>
<!--推送镜像仓库用户名-->
<username>${docker.username}</username>
<!--推送镜像仓库密码-->
<password>${docker.password}</password>
<!--推送镜像仓库地址-->
<url>${docker.registry}</url>
</publishRegistry>
</docker>
</configuration>
<executions>
<execution>
<id>docker-exec</id>
<phase>install</phase>
<goals>
<goal>build-image</goal>
</goals>
</execution>
</executions>
</plugin>
io.fabric8 -> docker-maven-plugin 插件打包docker
<plugin>
<groupId>io.fabric8</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>${docker.plugin.version}</version>
<configuration>
<!-- Docker Remote Api-->
<dockerHost>${docker.host}</dockerHost>
<!-- Docker 镜像私服-->
<registry>${docker.registry}</registry>
<!-- 认证信息-->
<authConfig>
<push>
<username>${docker.username}</username>
<password>${docker.password}</password>
</push>
</authConfig>
<images>
<image>
<!-- 镜像名称: 192.168.202.130:8082/fabric8/web:8.0.0-SNAPSHOT-->
<name>${docker.registry}/fabric8/${project.name}:${project.version}</name>
<build>
<dockerFile>${project.basedir}/Dockerfile</dockerFile>
</build>
</image>
<image>
<!-- 镜像名称: 192.168.202.130:8082/fabric8/web:latest-->
<name>${docker.registry}/fabric8/${project.name}</name>
<build>
<dockerFile>${project.basedir}/Dockerfile</dockerFile>
</build>
</image>
</images>
</configuration>
<executions>
<execution>
<id>docker-exec</id>
<!-- 绑定install命令,执行instal后,会去执行docker build,push -->
<phase>install</phase>
<goals>
<goal>build</goal>
<goal>push</goal>
</goals>
</execution>
</executions>
</plugin>
Dockerfile
打包镜像时,moxm/java:1.8-full 拉取不到镜像会报错,去仓库服务器执行 192.168.202.130
docker pull moxm/java:1.8-full
FROM moxm/java:1.8-full #需要的环境
RUN mkdir -p /java # 创建目录
WORKDIR /java #工作空间
COPY target/*.jar app.jar # 复制jar
EXPOSE 8080
ENV TZ=Asia/Shanghai JAVA_OPTS="-Xms128m -Xmx256m -Djava.security.egd=file:/dev/./urandom"
CMD sleep 5; java -jar app.jar $JAVA_OPTS
spring-boot 和 io.fabric8 打包docker镜像,使用了2个后,发现io.fabric8更好用,可以打包多个镜像。更主要是打包速度io.fabric8快很多。
3. ssh服务器 192.168.202.128
配置docker私有库
修改 vim /etc/docker/daemon.json
{
"registry-mirrors": ["https://registry.docker-cn.com"],
"insecure-registries": [
"192.168.202.130:8082",
"192.168.202.130:8083"
]
}
重启docker
systemctl daemon-reload
systemctl restart docker
登录nexus
# 修改下自己密码
docker login -u admin -p admin 192.168.202.130:8082
docker login -u admin -p admin 192.168.202.130:8083
Login Succeeded
代表成功了
#创建目录
mkdir -p /opt/webapps/ && chmod 777 /opt/webapps/ && cd /opt/webapps/
配置 docker-compose.yml
vim docker-compose.yml
version: '3'
services:
myweb:
image: "192.168.202.130:8082/fabric8/web"
container_name: web
ports:
- "7654:8080"
4. jenkins配置
新建maven任务
源码管理
这项目开源的,不需要配置密码。要密码的配置 Credentials
build
# -P sit 指定环境
# -U 强制让Maven检查所有SNAPSHOT依赖更新,确保集成基于最新的状态,如果没有该参数,Maven默认以天为单位检查更新,而持续集成的频率应该比这高很多
# -Dmaven.test.skip=true 跳过单元测试
clean install -P sit -U -Dmaven.test.skip=true
构建后操作
我这里配置服务器在远程的
SSH Server:系统管理--> 系统配置--> SSH Servers。 配置服务器
Transfer Set Source files:需要复制到远程服务器的文件,docker镜像打包了到私有库了,这里可以不用复制jar过去的,这里我做测试一下的。
Remove prefix:install会有target目录,删除该目录
Remote directory:远程服务器目录,配置SSH Servers,有指定目录/opt/webapps/,我这不用了。
Exec command:远程服务器执行的命令
docker stop web #停止容器
docker rm web #删除容器
docker rmi 192.168.202.130:8082/fabric8/web #删除镜像
docker-compose -f /opt/webapps/docker-compose.yml up -d # 重新pull镜像,启动
配置打印详细输出,点高级勾选 Verbose output in console
保存配置,去jenkins首页,构建试试。
构建
点击构建后,去观察日志
看到这样的,就是成功了。
测试
游览器中访问 http://192.168.202.128:7654/user/info
出现OK,就是成功啦
说明
这里只是一个简单例子,代码中的ip,要换成自己的IP,是基于之前的文章,需要搭建nexus私有仓库,docker私有仓库我选择了nexus,选择nexus是因为可以和maven共用。
如果有疑问,可以看下我之前的文章,是连贯起来的
如果有错误的地方,希望可以评论出来,我会修改的.