[英]Installing and using Gradle in a docker image/container
I am getting this strange error at the end of the process of creating a docker image from a Dockerfile
:在从
Dockerfile
创建Dockerfile
映像的过程结束时,我收到了这个奇怪的错误:
/bin/sh: 1: gradle: not found
INFO[0003] The command [/bin/sh -c gradle test jar] returned a non-zero code: 127
The relevant part of the Dockerfile
: Dockerfile
的相关部分:
FROM debian:jessie
[...]
RUN curl -L https://services.gradle.org/distributions/gradle-2.4-bin.zip -o gradle-2.4-bin.zip
RUN apt-get install -y unzip
RUN unzip gradle-2.4-bin.zip
RUN echo 'export GRADLE_HOME=/app/gradle-2.4' >> $HOME/.bashrc
RUN echo 'export PATH=$PATH:$GRADLE_HOME/bin' >> $HOME/.bashrc
RUN /bin/bash -c "source $HOME/.bashrc"
RUN gradle test jar
[...]
The command I am using is: docker build -t java_i .
我使用的命令是:
docker build -t java_i .
The strange thing is that if:奇怪的是,如果:
RUN gradle test jar
(command: docker run -d -p 9093:8080 -p 9094:8081 --name java_c -i -t java_i
),RUN gradle test jar
(命令: docker run -d -p 9093:8080 -p 9094:8081 --name java_c -i -t java_i
),docker exec -it java_c bash
),docker exec -it java_c bash
),gradle test jar
):gradle test jar
)中手动运行注释掉的命令: I eventually get the expected output (the compiled java code in the build
folder).我最终得到了预期的输出(
build
文件夹中编译的 java 代码)。
I am using Docker version 1.6.2我正在使用 Docker 版本 1.6.2
I solved the problem using the ENV
docker instructions ( link to the documentation ).我使用
ENV
docker 说明(链接到文档)解决了这个问题。
ENV GRADLE_HOME=/app/gradle-2.4
ENV PATH=$PATH:$GRADLE_HOME/bin
This command /bin/bash -c "source $HOME/.bashrc"
means that you create a new non-interactive process and run a command in it to set environment variables there.此命令
/bin/bash -c "source $HOME/.bashrc"
意味着您创建一个新的非交互式进程并在其中运行命令以在那里设置环境变量。 Which does not affect the parent process.这不影响父进程。 As soon as variables are set, process exits.
一旦设置了变量,进程就退出。 You can check this by running something like this:
您可以通过运行以下内容来检查这一点:
RUN /bin/bash -c "source $HOME/.bashrc; env"
RUN env
What should be working is this option:应该工作的是这个选项:
RUN source ~/.bashrc
And the reason why it works when you log in, is because the new process reads already updated ~/.bashrc
.并且它在您登录时起作用的原因是因为新进程读取已经更新
~/.bashrc
。
I was trying to install same version with JDK 11.0.7
but gradle-2.4
does not work.我试图安装与
JDK 11.0.7
相同的版本,但gradle-2.4
不起作用。 and got below error并得到以下错误
FAILURE: Build failed with an exception.
* What went wrong:
Could not determine java version from '11.0.7'.
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
I install later version to fix the above issue after installation.安装后我安装了更高版本来解决上述问题。
Posting as an answer might help someone else.作为答案发布可能会帮助其他人。
FROM openjdk:11.0.7-jdk
RUN apt-get update && apt-get install -y unzip
WORKDIR /gradle
RUN curl -L https://services.gradle.org/distributions/gradle-6.5.1-bin.zip -o gradle-6.5.1-bin.zip
RUN unzip gradle-6.5.1-bin.zip
ENV GRADLE_HOME=/gradle/gradle-6.5.1
ENV PATH=$PATH:$GRADLE_HOME/bin
RUN gradle --version
You can use multi-stage builds and the Gradle Docker image (no need to install Gradle...) to build the application then use the result in the runtime container:您可以使用多阶段构建和 Gradle Docker 映像(无需安装 Gradle...)来构建应用程序,然后在运行时容器中使用结果:
# Build
FROM gradle AS build
WORKDIR /appbuild
COPY . /appbuild
RUN gradle --version
# here goes your build code
Once the Gradle build is done, switch to the runtime container:一旦 Gradle 构建完成,切换到运行时容器:
# Runtime
FROM openjdk:8-jre-alpine
# more stuff here...
COPY --from=0 appbuild/<somepath>/some.jar application.jar
# more stuff here...
The COPY command copies the build artifacts from the build phase to the runtime container (in this case a jar file). COPY 命令将构建工件从构建阶段复制到运行时容器(在本例中为 jar 文件)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.