菜菜

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

                      

项目就一个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
@RestController
public 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-alpine
VOLUME /tmp
#编译传入的参数
ARG JAR_FILE
ADD ${JAR_FILE} app.jar
ENTRYPOINT ["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

<?xml version="1.0" encoding="UTF-8"?>
<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>com.caicai</groupId>
    <artifactId>boot-demo</artifactId>
    <version>1.0</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.4.RELEASE</version>
    </parent>

    <properties>
        <java.version>1.8</java.version>
        <!-- 可修改成自己的docker仓库名 -->
        <docker.image.prefix>zzzzz</docker.image.prefix>
    </properties>

    <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>


    <build>

        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>com.spotify</groupId>
                <artifactId>dockerfile-maven-plugin</artifactId>
                <version>1.3.6</version>
                <configuration>
                    <repository>${docker.image.prefix}/${project.artifactId}</repository>
                    <tag>${project.version}</tag>
                    <buildArgs>
                        <JAR_FILE>target/${project.build.finalName}.jar</JAR_FILE>
                    </buildArgs>
                </configuration>
                <!-- auto push -->
                <!-- 
                <executions>
                    <execution>
                        <id>default</id>
                        <phase>install</phase>
                        <goals>
                            <goal>build</goal>
                            <goal>push</goal>
                        </goals>
                    </execution>
                </executions>
                 -->
            </plugin>
        </plugins>
    </build>

</project>

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  好吧,就是菜菜  阅读(174)  评论(0编辑  收藏  举报