项目就一个java文件,仅用于样例
package com.example.demo;import org.springframework.beans.factory.annotation.Value;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;@SpringBootApplication@RestControllerpublic class BootDockerApplication{ @Value("${app.env}") private String env; @RequestMapping("/") public String home() { return "Hello Docker World-this env "+env; } public static void main(String[] args) { SpringApplication.run(BootDockerApplication.class, args); }}
Dockerfile
FROM openjdk:8-jdk-alpineVOLUME /tmp#编译传入的参数ARG JAR_FILEADD ${JAR_FILE} app.jarENTRYPOINT ["sh","-c","java $JAVA_OPTS -jar /app.jar"]
关于sh -c
执行sh -c ls 和 sh ls
-c string If the -c option is present, then commands are read from string. If there are arguments after the string, they are assigned to the positional parameters, starting with $0.
pom.xml
4.0.0 com.caicai boot-demo 1.0 org.springframework.boot spring-boot-starter-parent 1.5.4.RELEASE 1.8 zzzzz org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-test test org.springframework.boot spring-boot-maven-plugin com.spotify dockerfile-maven-plugin 1.3.6 ${docker.image.prefix}/${project.artifactId} ${project.version} target/${project.build.finalName}.jar
springboot的插件需要放在docker插件前面
application.yml
spring: profiles: active: - beta
application-beta.yml
logging: level: org.springframework: INFO app: env: beta
application-prod.yml
logging: level: org.springframework: INFO app: env: prod
编译镜像命令
mvn install dockerfile:build
查看镜像 docker images
启动命令,注意JAVA_OPSTS
分别为spring的环境配置,JVM远程调试和JMX命令
docker run \-e "JAVA_OPTS=-agentlib:jdwp=transport=dt_socket,address=5005,server=y,suspend=y -Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=9010 -Dcom.sun.management.jmxremote.rmi.port=9110 -Dcom.sun.management.jmxremote.local.only=false -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false -Djava.rmi.server.hostname=127.0.0.1 " \-e "SPRING_PROFILES_ACTIVE=prod" \--init \-p 8080:8080 \-p 9110:9110 \-p 9010:9010 \-p 5005:5005 \-it \zzzzz/boot-demo:1.0
posted on 2018-01-29 00:03 阅读( ...) 评论( ...)