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

标签 java spring-boot docker gradle

我正在学习如何编写Spring Boot应用程序,并使用gradle将其部署到Docker。我可以使用Gradle运行Spring Boot应用程序,但不能使用Docker Gradle运行Spring Boot应用程序。任何援助将不胜感激。我正在使用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 Boot应用程序
@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设置
PATH=/usr/local/bin/gradle:$PATH
export PATH

最佳答案

您的Dockerfile中包含以下行:

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

这是在告诉Docker在不存在的容器内运行Gradle。请从Dockerfile中删除此行,因为Gradle应该在容器外部运行。这还将使Dockerfile与您遵循的指南中的文件对齐。

运行Gradle时,它将生成通过以下几行添加到容器中的 Artifact :
COPY ${DEPENDENCY}/BOOT-INF/lib /app/lib
COPY ${DEPENDENCY}/META-INF /app/META-INF
COPY ${DEPENDENCY}/BOOT-INF/classes /app

关于java - 尝试运行Spring Boot应用程序时无法部署到Docker,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53688433/

相关文章:

java - 使检查异常成为 RuntimeException

Spring Profiles,不同的 Log4j2 配置

mysql - 在带有SQL文件的Docker容器中创建MYSQL表

python - 如何检查 docker 实例是否正在运行?

java - 删除 JFreeChart 中条形图和轴线之间的分隔

java - 如何使用斜杠分隔符在 windows 和 linux 中唯一地指定访问文件的路径

java - 如何添加额外的自定义 header ,同时抛出异常以将消息存储在 kafka 上的 dlq 中?

java - Spring @Bean Name,不适用于@Qualifier

node.js - Axios 的 POST 请求不向我的服务器发送数据

java - jfreechart:向甘特图添加十字线覆盖