spring boot hello and docker

** 原創文章,請勿轉載 **java

主要是想試下spring boot運行在docker裏的感受, 小試牛刀   :)web

這是原文,參考一下:  https://spring.io/guides/gs/spring-boot-docker/spring

 

1. 環境docker

  win10, eclipse, mvn, centos 7.3(vm), docker 1.12apache

  即開發環境在win10下, docker環境在centos vm裏centos

 

2. project圖springboot

  兩個代碼文件: Application.java,  HelloController.javaapp

   一個Dockerfile, 把生成的jar build成鏡像eclipse

   一個pom.xml, 這個不解釋maven

 

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的圖:

 

相關文章
相關標籤/搜索