这里主要用到spring-boot开箱即用,能生成一个独立运行的程序,及maven的插件docker-maven-plugin
这里主要步骤
构建一个简单的springboot项目
添加docker-maven-plugin及写dockerfile
实践生成 docker镜像
一个简单 Spring Boot 项目
以spring boot 2.0 为例
在pom.xml文件中增加parament依赖
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.0.RELEASE</version>
</parent>
增加web和测试依赖
<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>
</dependencies>
创建一个 Controller,在其中有一个index()方法,访问时返回:Hello Docker!
@RestController
public class Controller { @RequestMapping("/")
public String index() {
return "Hello Docker!";
}
}
启动类
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
添加完毕后启动项目,启动成功后浏览器放问:http://localhost:8080/,页面返回:Hello Docker!,说明 Spring Boot 项目配置正常。
添加dcoker-maven-plugin
在pom.xml的properties节点中添加Docker镜像前缀
<properties>
<docker.image.prefix>springboot</docker.image.prefix>
</properties>
在plugins中添加docker构建插件
<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>
<imageName>${docker.image.prefix}/${project.artifactId}</imageName>
<dockerDirectory>src/main/docker</dockerDirectory>
<resources>
<resource>










