尝试运行 Spring Boot 应用程序时无法部署到 docker

Posted

技术标签:

【中文标题】尝试运行 Spring Boot 应用程序时无法部署到 docker【英文标题】:Unable to deploy to docker when attempting to run a spring boot application 【发布时间】:2019-05-10 08:03:36 【问题描述】:

我正在学习如何编写 Spring Boot 应用程序,并使用 gradle 将其部署到 Docker。我可以使用 Gradle 运行 Spring Boot 应用程序,但不能使用 Docker AND Gradle。任何援助将不胜感激。我使用 Spring 的 git repo 作为我的指南。 https://github.com/spring-guides/gs-spring-boot-docker/blob/master/complete

当我执行以下命令时,

docker run -p 8080:8080 -t com.iheartmedia/iheartdemo

错误信息

docker: Error response from daemon: OCI runtime create failed: container_linux.go:348: starting container process caused "exec: \"gradle\": executable file not found in $PATH": unknown.

我用 Homebrew 安装了 gradle,这是我的 gradle 设置

find / -name 'gradle' -type f

/usr/local/Cellar/gradle/5.0/bin/gradle
/usr/local/Cellar/gradle/5.0/libexec/bin/gradle

Gradle 设置

which gradle

/usr/local/bin/gradle

Gradle 命令构建 docker

./gradlew build docker

Starting a Gradle Daemon, 3 busy Daemons could not be reused, use --status for details

> Task :test
objc[14153]: Class JavaLaunchHelper is implemented in both /Library/Java/JavaVirtualMachines/jdk1.8.0_102.jdk/Contents/Home/bin/java (0x10dc4a4c0) and /Library/Java/JavaVirtualMachines/jdk1.8.0_102.jdk/Contents/Home/jre/lib/libinstrument.dylib (0x10dce24e0). One of the two will be used. Which one is undefined.
2018-12-09 03:26:56.494  INFO 14153 --- [      Thread-92] o.s.w.c.s.GenericWebApplicationContext   : Closing org.springframework.web.context.support.GenericWebApplicationContext@389f0591: startup date [Sun Dec 09 03:26:55 PST 2018]; root of context hierarchy
2018-12-09 03:26:56.498  INFO 14153 --- [      Thread-92] o.s.c.support.DefaultLifecycleProcessor  : Stopping beans in phase 2147483647
2018-12-09 03:26:56.499  INFO 14153 --- [      Thread-92] j.LocalContainerEntityManagerFactoryBean : Closing JPA EntityManagerFactory for persistence unit 'default'
2018-12-09 03:26:56.500  INFO 14153 --- [      Thread-92] com.zaxxer.hikari.HikariDataSource       : HikariPool-22 - Shutdown initiated...
2018-12-09 03:26:56.500  INFO 14153 --- [      Thread-92] com.zaxxer.hikari.HikariDataSource       : HikariPool-22 - Shutdown completed.

BUILD SUCCESSFUL in 49s
10 actionable tasks: 7 executed, 3 up-to-date

Docker 文件

FROM java:8
VOLUME /tmp
ARG DEPENDENCY=target/dependency
# Installed with homebrew with a symlink
COPY $DEPENDENCY/BOOT-INF/lib /app/lib
COPY $DEPENDENCY/META-INF /app/META-INF
COPY $DEPENDENCY/BOOT-INF/classes /app
ENTRYPOINT ["java","-cp","app:app/lib/*","com.iheartmedia.IHeartMedia"]

build.gradle

buildscript 
    repositories 
        mavenCentral()
    
    dependencies 
        classpath("org.springframework.boot:spring-boot-gradle-plugin:2.0.5.RELEASE")
        classpath('gradle.plugin.com.palantir.gradle.docker:gradle-docker:0.13.0')
    


plugins 
    id 'java'
    id 'maven'
    id 'io.franzbecker.gradle-lombok' version '1.14'


apply plugin: 'org.springframework.boot'
apply plugin: org.springframework.boot.gradle.plugin.SpringBootPlugin
apply plugin: "io.spring.dependency-management"
apply plugin: 'jacoco'
apply plugin: 'com.palantir.docker'

bootJar 
    baseName = 'iheartdemo'
    version =  '1.0-SNAPSHOT'


repositories 
    mavenLocal()
    maven 
        url = 'http://repo.maven.apache.org/maven2'
    


springBoot 
    mainClassName = 'com.iheartmedia.IHeartMedia'


jacoco 
    toolVersion = "0.8.2"
    reportsDir = file("$buildDir/reports/jacoco")


jacocoTestReport 
    reports 
        xml.enabled false
        csv.enabled false
        html.destination file("$buildDir/reports/coverage")
    


dependencies 
    compile 'org.springframework:spring-web:5.0.9.RELEASE'
    compile 'org.springframework.boot:spring-boot-starter-web:2.0.5.RELEASE'
    compile 'org.springframework.boot:spring-boot-starter-data-jpa:2.0.5.RELEASE'
    compile 'org.projectlombok:lombok:1.18.4'
    compile 'org.springframework.retry:spring-retry:1.2.2.RELEASE'
    compile 'io.springfox:springfox-swagger2:2.9.2'
    compile 'io.springfox:springfox-swagger-ui:2.9.2'
    runtime 'com.h2database:h2:1.4.197'
    testCompile 'org.springframework.boot:spring-boot-starter-test:2.0.5.RELEASE'
    compile 'org.springframework.boot:spring-boot-starter-actuator:2.0.5.RELEASE'
    compile 'org.springframework.boot:spring-boot-starter-web:2.0.5.RELEASE'


group = 'com.iheartmedia'
version = '1.0-SNAPSHOT'
sourceCompatibility = '1.8'
targetCompatibility = '1.8'

tasks.withType(JavaCompile) 
    options.encoding = 'UTF-8'


task unpack(type: Copy) 
    dependsOn bootJar
    from(zipTree(tasks.bootJar.outputs.files.singleFile))
    into("build/dependency")

docker 
    name "$project.group/$bootJar.baseName"
    copySpec.from(tasks.unpack.outputs).into("dependency")
    buildArgs(['DEPENDENCY': "dependency"])

// end::task[]

我的 Spring 启动应用程序

@SpringBootApplication
@EnableJpaRepositories(considerNestedRepositories = true)
public class IHeartMedia 

    public static void main(String[] args) 
        SpringApplication.run(IHeartMedia.class, args);
    

任何建议将不胜感激。我查看了其他 SO 帖子,但它们似乎特定于 Python、Go 或 Node JS。

Gradle 是在我的路径变量中定义的,所以我不知道为什么会这样。这是我的 PATH 设置

PATH=/usr/local/bin/gradle:$PATH
export PATH

【问题讨论】:

【参考方案1】:

您的Dockerfile 中有以下行:

CMD ["gradle", "/usr/local/bin/gradle"]

这告诉 Docker 在不存在的容器中运行 Gradle。从您的 Dockerfile 中删除此行,因为 Gradle 应该在容器外运行。这还将使您的 Dockerfile 与您正在遵循的指南中的文件保持一致。

Gradle 运行时,将生成通过以下几行添加到容器的工件:

COPY $DEPENDENCY/BOOT-INF/lib /app/lib
COPY $DEPENDENCY/META-INF /app/META-INF
COPY $DEPENDENCY/BOOT-INF/classes /app

【讨论】:

如果您按照建议删除了该行并且 Docker 仍然失败并出现相同的错误,那么您还没有描述其他事情。 Docker 在构建镜像时不会尝试运行gradle,除非你告诉它这样做。

以上是关于尝试运行 Spring Boot 应用程序时无法部署到 docker的主要内容,如果未能解决你的问题,请参考以下文章

尝试在外部在tomcat 9上运行带有rest控制器的spring boot war文件

如何在外部 tomcat 中部署 Spring-boot REST API

spring-boot 应用程序的外部配置

当我尝试使用 Spring Boot 为 SessionFactory 创建 bean 时无法解析的循环引用

无法使Spring Boot + JavaFX在Eclipise中运行

在 Docker 容器内运行 Spring Boot 应用程序,无法连接 MySQL