spring boot hello and docker
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了spring boot hello and docker相关的知识,希望对你有一定的参考价值。
主要是想试下spring boot运行在docker里的感觉, 小试牛刀 :)
这是原文,参考一下: https://spring.io/guides/gs/spring-boot-docker/
1. 环境
win10, eclipse, mvn, centos 7.3(vm), docker 1.12
即开发环境在win10下, docker环境在centos vm里
2. project图
两个代码文件: Application.java, HelloController.java
一个Dockerfile, 把生成的jar build成镜像
一个pom.xml, 这个不解释
3. 源码文件:
pom.xml
<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>SpringbootDocker</groupId> <artifactId>ex.springbootdocker</artifactId> <packaging>jar</packaging> <version>0.0.1</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.6.RELEASE</version> </parent> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <fork>true</fork> </configuration> </plugin> <plugin> <groupId>com.spotify</groupId> <artifactId>dockerfile-maven-plugin</artifactId> <version>1.3.4</version> <configuration> <repository>${docker.image.prefix}/${project.artifactId}</repository> </configuration> </plugin> </plugins> </build> </project>
Application.java
package ex.springbootdocker; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
HelloController.java
package ex.springbootdocker; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; @RestController public class HelloController { @RequestMapping(value="/hello/{name}", method=RequestMethod.GET) public String sayHello(@PathVariable("name") String name) { return "hello, " + name; } }
Dockerfile
FROM openjdk:8-jdk-alpine VOLUME /tmp ADD ex.springbootdocker-0.0.1.jar app.jar ENV JAVA_OPTS="" ENTRYPOINT exec java $JAVA_OPTS -jar /app.jar
4. 说明
1). 我的eclipse没有自动生成jar, 只能mvn package
2). 将生成的jar 和 Dockerfile 复制到 centos vm里, 比如/opt/docker
3). 生成镜像
4). 运行
再来一个firefox的图:
以上是关于spring boot hello and docker的主要内容,如果未能解决你的问题,请参考以下文章
spring boot学习总结-- 基础入门 Hello,spring boot!