随着阿里中台概念的提出,大大小小的互联网公司都对原有项目技术架构进行改造、优化、升级,其中使用最多的莫过于微服务、容器以及云服务。我司综合考虑将来业务发展决定项目逐步云化、容器化,下面就基于springboot+maven项目远程部署到docker中进行分享下,但愿能给大伙儿有些帮助。html
远程部署前提须要开启docker远程访问功能,下面介绍经常使用两个操做系统的配置java
DOCKER_OPTS="-H=unix:///var/run/docker.sock -H=0.0.0.0:2375"
复制代码
ExecStart=/usr/bin/dockerd -H tcp://0.0.0.0:2375 -H unix:///var/run/docker.sock
复制代码
效果截图linux
重启服务git
systemctl daemon-reload
systemctl restart docker.service
复制代码
打开intelliJ -> New Project -> Spring Assistant(Spring Initializr Server) -> 工程名(mvhello) -> 点击完成,项目结构以下github
在src/main/kotlin 相应目录下新建HelloControllerspring
@RestController
class HelloController {
@GetMapping("hello")
fun helloWorld() = "hello world"
}
复制代码
在src/main/resource的application.properties里面添加自定义端口号docker
server.port=8888
复制代码
启动项目,浏览器访问centos
~$ mvn spring-boot:run
复制代码
确保项目能正常访问后,在src/main/docker文件夹下面,新建dockerfile浏览器
FROM openjdk:8-jdk-alpine
VOLUME /tmp ADD mvhello-0.0.1.jar app.jar # -Djava.security.egd=file:/dev/./urandom 可解决tomcat可能启动慢的问题
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"] EXPOSE 8888
复制代码
在pom.xml中添加下面代码块tomcat
<plugins>
<!-- springboot 打包 -->
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<!-- mvn clean package docker:build -Dmaven.test.skip=true -->
<plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>1.0.0</version>
<configuration>
<!-- 镜像名称 -->
<imageName>maven-hello</imageName>
<imageTags>0.0.1</imageTags>
<!-- docker远程服务器地址 -->
<dockerHost>http://192.168.80.200:2375</dockerHost>
<!-- Dockerfile文件存放目录 -->
<dockerDirectory>src/main/docker</dockerDirectory>
<resources>
<resource>
<directory>${project.build.directory}</directory>
<include>${project.build.finalName}.jar</include>
</resource>
</resources>
</configuration>
</plugin>
</plugins>
复制代码
保存后,执行如下命令
~$ mvn clean package docker:build -Dmaven.test.skip=true
复制代码
看到执行成功后,远程登陆80.200的服务,看下docker镜像
【demo地址】(https://github.com/lenvonsam/mvhello.git)
【docker官网】(https://docs.docker.com)
【docker介绍】(https://www.cnblogs.com/boshen-hzb/p/6400272.html)
【kotlin介绍】(https://www.runoob.com/kotlin/kotlin-tutorial.html)