zoukankan      html  css  js  c++  java
  • springboot-dokcer

                          

    项目就一个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
    

      

  • 相关阅读:
    使用 yo 命令行向导给 SAP UI5 应用添加一个新的视图
    SAP Fiori Elements 应用的 manifest.json 文件运行时如何被解析的
    SAP UI5 标准应用的多语言支持
    微软 Excel 365 里如何设置下拉菜单和自动高亮成指定颜色
    SAP Fiori Elements 应用里的 Title 显示的内容是从哪里来的
    本地开发好的 SAP Fiori Elements 应用,如何部署到 ABAP 服务器上?
    如何在 Cypress 测试代码中屏蔽(Suppress)来自应用代码报出的错误消息
    教你一招:让集群慢节点无处可藏
    应用架构步入“无服务器”时代 Serverless技术迎来新发展
    MySQL数据库事务隔离性的实现
  • 原文地址:https://www.cnblogs.com/shuiyonglewodezzzzz/p/8372967.html
Copyright © 2011-2022 走看看