Docker 从入门到放弃(二)之Spring Boot

Docker 从入门到放弃(二)之Spring Boot

  • Docker 从入门到放弃二之Spring Boot
    • 项目结构
    • pom文件
    • main文件
    • Dockerfile
    • 访问

首先写一个Spring Boot的项目,建立一个Maven的项目,可以用任何一种 IDE,最好推荐使用IDEA,因为用起来不仅漂亮,而且方便。

项目结构

Docker 从入门到放弃(二)之Spring Boot_第1张图片

pom文件


<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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0modelVersion>
  <groupId>com.chen.webgroupId>
  <artifactId>docker-springartifactId>
  <packaging>jarpackaging>
  <version>1.0-SNAPSHOTversion>
  <name>docker-springname>
  <url>http://maven.apache.orgurl>
  <parent>
    <groupId>org.springframework.bootgroupId>
    <artifactId>spring-boot-starter-parentartifactId>
    <version>1.3.5.RELEASEversion>
    <relativePath/>
  parent>

  <dependencies>
    <dependency>
      <groupId>org.springframework.bootgroupId>
      <artifactId>spring-boot-starter-webartifactId>
    dependency>
    <dependency>
      <groupId>org.springframework.bootgroupId>
      <artifactId>spring-boot-starter-testartifactId>
      <scope>testscope>
    dependency>
  dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-maven-pluginartifactId>
      plugin>
      
      <plugin>
        <groupId>com.spotifygroupId>
        <artifactId>docker-maven-pluginartifactId>
        <version>0.4.3version>
        <configuration>
          <imageName>${docker.image.prefix}/${project.artifactId}imageName>
          <dockerDirectory>src/main/dockerdockerDirectory>
          <resources>
            <resource>
              <targetPath>/targetPath>
              <directory>${project.build.directory}directory>
              <include>${project.build.finalName}.jarinclude>
            resource>
          resources>
        configuration>
      plugin>
      
    plugins>
  build>

  <properties>
    <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
    <docker.image.prefix>waylaudocker.image.prefix>
    <spring.boot.version>1.3.3.RELEASEspring.boot.version>
  properties>
project>

main文件

package com.chen.web;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by CHEN on 2016/8/20.
 */
@SpringBootApplication
@RestController
public class Application {

    @RequestMapping("/")
    public String home() {
        return "welcome to docker world";
    }

    public static void main(String[] args) {
        SpringApplication.run(Application.class,args);
    }

}

Dockerfile

FROM frolvlad/alpine-oraclejdk8:slim
VOLUME /tmp
ADD docker-spring-1.0-SNAPSHOT.jar app.jar
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]

访问

编译:
mvn clean package

运行:(此处的docker-spring-1.0-SNAPSHOT.jar为项目名)
java -jar docker-spring-1.0-SNAPSHOT.jar

访问:
访问http://localhost:8080/

你可能感兴趣的:(项目经验,Spring,分布式,docker)