spring-boot无法在docker

时间:2016-06-22 19:34:27

标签: java spring shell docker spring-boot

我在docker中运行spring boot应用程序时遇到了一些问题。

堆栈:maven 3+,spring boot(jpa / rest / jetty) - mysql - 在docker中部署

所以,我已经在我的pom文件中了

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.4.0.M3</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <java.version>1.8</java.version>
</properties>

<dependencies>
    <!-- SPRING BOOT DEPENDENCIES -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-rest</artifactId>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
        <!-- add for exlude tomcat -->
        <exclusions>
            <exclusion>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-tomcat</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <!-- END SPRING BOOT DEPENDENCIES-->
    <!-- Jetty (tomcat replacement) -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-jetty</artifactId>
    </dependency>
    <!-- mysql connector -->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <scope>runtime</scope>
    </dependency>
    <!-- optional dependency javax.el -->
    <dependency>
        <groupId>javax.el</groupId>
        <artifactId>javax.el-api</artifactId>
        <version>3.0.0</version>
    </dependency>
    <!-- google http client -->
    <dependency>
        <groupId>com.google.http-client</groupId>
        <artifactId>google-http-client</artifactId>
        <version>1.21.0</version>
    </dependency>
    <!-- google http jackson -->
    <dependency>
        <groupId>com.google.http-client</groupId>
        <artifactId>google-http-client-jackson2</artifactId>
        <version>1.21.0</version>
    </dependency>
</dependencies>

环境: Ubuntu 16.04 x64 问题: 本地:我尝试在终端

中使用follow命令运行我的应用程序
user$ java -Xmx768m -jar /mnf-backend.jar --spring.datasource.url=jdbc:mysql://$MYSQL_PORT_3306_TCP_ADDR/app_1?autoReconnect=true&useSSL=false
user$ #<--- LOOK AT THIS jvm has return of control with 1 status (or same status but not negative)
 :: Spring Boot ::             (v1.4.0.M3) # <--- spring boot starts by itself. HOW????

我能忍受它并不好。但不是码头工人。 当上面的命令将在docker中运行,然后docker stop container(因为 - &gt; app exit,状态为1)

ENTRYPOINT ["java", "-Xmx768m", "-jar", "/mnf-backend.jar", "--spring.datasource.url=jdbc:mysql://$MYSQL_PORT_3306_TCP_ADDR/app_1?autoReconnect=true&useSSL=false"]

Docker将启动容器1秒并立即停止容器,因为java返回控制。 我寻找方法,允许我配置spring应用程序的可预测行为或任何想法如何改进我的docker指令。 我的dockerfile内容:

FROM frolvlad/alpine-oraclejdk8:slim

ENV MNFB_ENV production
ENV SERVER_PORT 9000

ADD ./builds/mnf-latest.jar mnf-backend.jar

EXPOSE 9000



ENTRYPOINT ["java", "-Xmx768m", "-jar", "/mnf-backend.jar", "--spring.datasource.url=jdbc:mysql://$MYSQL_PORT_3306_TCP_ADDR/minifinance?autoReconnect=true&useSSL=false"]

容器的docker日志 enter image description here

例如:当我启动nodejs app控件时,在应用程序未完成之前不会返回

user$ node ./server.js
[...here program output and stdout strings]
[... it may be stopped by ctrl+c for example]

2 个答案:

答案 0 :(得分:8)

我认为问题在于命令行中的&符号(&amp;):

--spring.datasource.url=jdbc:mysql://$MYSQL_PORT_3306_TCP_ADDR/app_1?autoReconnect=true&useSSL=false"]

试着逃避它:

--spring.datasource.url=jdbc:mysql://$MYSQL_PORT_3306_TCP_ADDR/app_1?autoReconnect=true\&useSSL=false"]

&符表示在后台启动进程的shell。这正是您本地计算机上发生的事情。如果你启动你的jar,那么这个过程应该从前台开始......并且提示不应该直接返回。

答案 1 :(得分:0)

为了保持简单和干净,我们在database.properties

中添加了数据库属性

mongo db数据库配置

spring.data.mongodb.database=abc-auth
spring.data.mongodb.host=192.168.2.2
spring.data.mongodb.port=27017
spring.data.mongodb.password=abc234quth
spring.data.mongodb.username=abc-auth

我们在运行Docker时推送此文件,因此只有数据库属性才会被现有的application.properties覆盖

ENTRYPOINT ["java","-jar","/home/docker/service/abc.jar","--spring.config.location=application.properties"]