java通过Idea远程一键部署springboot到Docker详解
一、开发前准备
1. docker的安装可以参考https://docs.docker.com/install/
2. 配置docker远程连接端口
vi /usr/lib/systemd/system/docker.service
找到 execstart,在最后面添加 -h tcp://0.0.0.0:2375,如下图所示
3. 重启docker
systemctl daemon-reload
systemctl start docker
4. 开放端口
firewall-cmd --zone=public --add-port=2375/tcp --permanent
5. idea安装插件,重启
6. 连接远程docker
(1) 编辑配置
(2) 填远程docker地址
(3) 连接成功,会列出远程docker容器和镜像
二、新建项目
1. 创建springboot项目
项目结构图
(1) 配置pom文件
<?xml version="1.0" encoding="utf-8"?> <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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelversion>4.0.0</modelversion> <groupid>docker-demo</groupid> <artifactid>com.demo</artifactid> <version>1.0-snapshot</version> <parent> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-parent</artifactid> <version>2.0.2.release</version> <relativepath /> </parent> <properties> <project.build.sourceencoding>utf-8</project.build.sourceencoding> <project.reporting.outputencoding>utf-8</project.reporting.outputencoding> <docker.image.prefix>com.demo</docker.image.prefix> <java.version>1.8</java.version> </properties> <build> <plugins> <plugin> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-maven-plugin</artifactid> </plugin> <plugin> <groupid>com.spotify</groupid> <artifactid>docker-maven-plugin</artifactid> <version>1.0.0</version> <configuration> <dockerdirectory>src/main/docker</dockerdirectory> <resources> <resource> <targetpath>/</targetpath> <directory>${project.build.directory}</directory> <include>${project.build.finalname}.jar</include> </resource> </resources> </configuration> </plugin> <plugin> <artifactid>maven-antrun-plugin</artifactid> <executions> <execution> <phase>package</phase> <configuration> <tasks> <copy todir="src/main/docker" file="target/${project.artifactid}-${project.version}.${project.packaging}"></copy> </tasks> </configuration> <goals> <goal>run</goal> </goals> </execution> </executions> </plugin> </plugins> </build> <dependencies> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-web</artifactid> </dependency> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-test</artifactid> <scope>test</scope> </dependency> <dependency> <groupid>log4j</groupid> <artifactid>log4j</artifactid> <version>1.2.17</version> </dependency> </dependencies> </project>
(2) 在src/main目录下创建docker目录,并创建dockerfile文件
from openjdk:8-jdk-alpine add *.jar app.jar entrypoint ["java","-djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]
(3) 在resource目录下创建application.properties文件
logging.config=classpath:logback.xml logging.path=/home/developer/app/logs/ server.port=8990
(4) 创建dockerapplication文件
@springbootapplication public class dockerapplication { public static void main(string[] args) { springapplication.run(dockerapplication.class, args); } }
(5) 创建dockercontroller文件
@restcontroller public class dockercontroller { static log log = logfactory.getlog(dockercontroller.class); @requestmapping("/") public string index() { log.info("hello docker!"); return "hello docker!"; } }
(6) 增加配置
命令解释
- image tag : 指定镜像名称和tag,镜像名称为 docker-demo,tag为1.1
- bind ports : 绑定宿主机端口到容器内部端口。格式为[宿主机端口]:[容器内部端口]
- bind mounts : 将宿主机目录挂到到容器内部目录中。格式为[宿主机目录]:[容器内部目录]。这个springboot项目会将日志打印在容器 /home/developer/app/logs/ 目录下,将宿主机目录挂载到容器内部目录后,那么日志就会持久化容器外部的宿主机目录中。
(7) maven打包
(8) 运行
先pull基础镜像,然后再打包镜像,并将镜像部署到远程docker运行
这里我们可以看到镜像名称为docker-demo:1.1,docker容器为docker-server
(9) 运行成功
(10) 浏览器访问
(11) 日志查看
自此通过idea 部署springboot项目到docker成功!难以想象,部署一个javaweb项目竟然如此简单方便!
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
上一篇: seo外链发布技巧
下一篇: js隐式转换的知识实例讲解