在Docker中部署Springboot项目(windows版)
一、创建springboot项目1.1 controller中加入方法@RestControllerpublic class HelloController {@GetMapping("/docker")public String hello() {return "Hello Docker!!";}}1.2 pom中加入doc
·
一、创建springboot项目
1.1 controller中加入方法
@RestController
public class HelloController {
@GetMapping("/docker")
public String hello() {
return "Hello Docker!!";
}
}
1.2 pom中加入docker-maven-plugin
<?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>com.payne.study</groupId>
<artifactId>spring-boot-study</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>spring-boot-study</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<!--properties节点中设置docker镜像的前缀“springboot”-->
<docker.image.prefix>springboot</docker.image.prefix>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<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>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<!--加入maven插件“docker-maven-plugin”-->
<plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<!--<version>0.2.3</version>-->
<version>0.4.11</version>
<configuration>
<imageName>${docker.image.prefix}/${project.artifactId}</imageName>
<dockerDirectory>src/main/docker</dockerDirectory>
<resources>
<resource>
<targetPath>/</targetPath>
<directory>${project.build.directory}</directory>
<include>${project.build.finalName}.jar</include>
</resource>
</resources>
</configuration>
</plugin>
</plugins>
<finalName>spring-boot-study</finalName>
</build>
</project>
1.3 编写Dockerfile
放置到src/docker目录下
FROM java:8
VOLUME /tmp
ADD spring-boot-study.jar app.jar
RUN sh -c 'touch /app.jar'
ENV JAVA_OPTS=""
ENTRYPOINT [ "sh", "-c", "java $JAVA_OPTS -Djava.security.egd=file:/dev/./urandom -jar /app.jar" ]
二、部署项目到docker
2.1 准备文件
把spring-boot-study.jar和dockerfile复制到某一个文件夹中;
2.2 执行docker builder命令
docker build -t spring-boot-docker .
-t代表要构建的镜像的名字和标签,通常 name:tag 或者 name 格式,Tag默认是latest
.代表当前目录,也就是Dockerfile所在的目录。
docker builder命令详解: http://www.runoob.com/docker/docker-build-command.html
第一次执行会下载相关镜
运行完成之后查看images,可以看到spring-boot-docker 镜像已经build成功
2.3 执行docker run命令
docker run -d -p 8080:8080 spring-boot-docker
Docker Run命令大全:http://www.runoob.com/docker/docker-run-command.html
三、见证结果
访问地址:http://localhost:8080/docker
部署成功
更多推荐
已为社区贡献2条内容
所有评论(0)