1、在IDEA工具中开发代码
2、代码打成jar包
3、部署到Linux服务器上
4、如果用Docker(编写Dockerfile文件)
5、构建镜像
6、运行容器
现在使用IDEA集成Docker实现一键打包部署
1、Docker开启远程访问
修改Docker服务文件
vim /lib/systemd/system/docker.service
修改这一行
/usr/bin/dockerd-current -H tcp://0.0.0.0:2375 -H unix://var/run/docker.sock \
systemctl daemon-reload
3、重启Docker服务
systemctl restart docker
4、查看端口是否开启
netstat -nlpt
5、关闭防火墙
systemctl stop firewalld.service
6、禁止防火墙开机启动
systemctl disable firewalld.service
7、直接curl看看是否生效
curl http://127.0.0.1:2375/info
在IDEA中配置
而在持续集成过程中,项目工程一般使用Maven编译打包,然后生成镜像,通过镜像上线,能够大大提供上线效率,同时能够快速动态扩容,快速回滚,着实很方便。docker-maven-plugin 插件就是为了帮助我们在Maven工程中,通过简单的配置,自动生成镜像并推送到仓库中。
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0modelVersion>
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>2.2.2.RELEASEversion>
<relativePath/>
parent>
<groupId>edu.xpugroupId>
<artifactId>dockerartifactId>
<version>0.0.1-SNAPSHOTversion>
<name>dockername>
<description>Demo project for Spring Bootdescription>
<properties>
<java.version>1.8java.version>
<docker.image.prefix>zouchanglindocker.image.prefix>
properties>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
<exclusions>
<exclusion>
<groupId>org.junit.vintagegroupId>
<artifactId>junit-vintage-engineartifactId>
exclusion>
exclusions>
dependency>
dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
plugin>
<plugin>
<groupId>com.spotifygroupId>
<artifactId>docker-maven-pluginartifactId>
<version>1.0.0version>
<configuration>
<imageName>${docker.image.prefix}/${project.artifactId}imageName>
<imageTags>
<imageTag>latestimageTag>
imageTags>
<baseImage>javabaseImage>
<maintainer>zouchanglin [email protected]maintainer>
<workdir>/ROOTworkdir>
<cmd>["java", "-version"]cmd>
<entryPoint>["java", "-jar", "${project.build.finalName}.jar"]entryPoint>
<dockerHost>http://192.168.70.128dockerHost>
<resources>
<resource>
<targetPath>/ROOTtargetPath>
<directory>${project.build.directory}directory>
<include>${project.build.finalName}.jarinclude>
resource>
resources>
configuration>
<executions>
<execution>
<id>build-imageid>
<phase>packagephase>
<goals>
<goal>buildgoal>
goals>
execution>
executions>
plugin>
plugins>
build>
project>
如果用上述maven插件则会构造出这样的Dockerfile文件
FROM java
MAINTAINER zouchanglin [email protected]
WORKDIR /ROOT
ADD /ROOT/docker-0. 0.1-SNAPSHOT. jar /ROOT/
ENTRYPOINT ["java", "-jar", "docker-0.0. 1-SNAPSHOT. jar"]
CMD ["java", "-version"]
接下来使用命令构建镜像
mvn clean package docker:build
然后通过IDEA操作Docker去生成容器即可
绑定Docker命令道Maven各个阶段
我们可以绑定Docker命令到Maven各个阶段,我们可以把Docker分为buld、tag、 push ,然后分别绑定Maven的package、deploy 阶段,我们只需要执行mvn dep loy就可以完成整个build、tag、push操作了,当我们执行mvn build 就只完成build、tag 操作。
现在我们可以在mvn package的时候就完成镜像的打包
<executions>
<execution>
<id>build-imageid>
<phase>packagephase>
<goals>
<goal>buildgoal>
goals>
execution>
executions>