Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

spring-boot cannot start in docker

I've a little problem with run my spring boot application in docker.

stack: maven 3+, spring boot(jpa/rest/jetty) - mysql - deploy in docker

So, i've have in my pom file

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

Environment: Ubuntu 16.04 x64 The problem: Locally: I try to run my app with follow command in terminal

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

it's not good by i can tolerate it. But not docker. When commands above will be run in docker then docker stop container (because -> app exit with status 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 will start container 1 second and immediately stop container because java return control. I look for method which allow me to configure spring app for predictable behavior or any ideas how to improve my docker instructions. my dockerfile content:

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 logs of a container enter image description here

For example: when i've start nodejs app control not return until application not finished

user$ node ./server.js
[...here program output and stdout strings]
[... it may be stopped by ctrl+c for example]
like image 298
Ivan Wong Avatar asked Jun 22 '16 19:06

Ivan Wong


People also ask

Is Docker necessary for Spring boot?

Spring Boot Maven and Gradle PluginsYou do not need a Dockerfile , but you do need a Docker daemon, either locally (which is what you use when you build with docker) or remotely through the DOCKER_HOST environment variable.


1 Answers

I think the problem is the ampersand (&) in your command line:

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

Try to escape it:

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

The ampersand denotes the shell to start your process in the background. That's exactly what's happening on your local machine. If you start your jar, the process should start in foreground... and the prompt shouldn't return directly.

like image 64
joshiste Avatar answered Oct 11 '22 05:10

joshiste