java 在 docker 镜像/容器中安装和使用 Gradle
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31280753/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Installing and using Gradle in a docker image/container
提问by TPPZ
I am getting this strange error at the end of the process of creating a docker image from a Dockerfile
:
在从以下位置创建 docker 映像的过程结束时,我收到了这个奇怪的错误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:
奇怪的是,如果:
- I run a container from the previous image commenting out
RUN gradle test jar
(command:docker run -d -p 9093:8080 -p 9094:8081 --name java_c -i -t java_i
), - then I log into that container (command:
docker exec -it java_c bash
), - then I manually check the gradle environment variables finding them,
- then I manually run that commented out command from within the running container (
gradle test jar
):
- 我从上一个图像注释运行一个容器
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
, - 然后我手动检查找到它们的 gradle 环境变量,
- 然后我从正在运行的容器 (
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
回答by TPPZ
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
回答by Alex V
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. Which does not affect the parent process. As soon as variables are set, process exits. You can check this by running something like this:
此命令/bin/bash -c "source $HOME/.bashrc"
意味着您创建一个新的非交互式进程并在其中运行命令以在其中设置环境变量。这不影响父进程。一旦设置了变量,进程就退出。您可以通过运行以下内容来检查这一点:
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
.
并且它在您登录时起作用的原因是因为新进程读取了 already updated ~/.bashrc
。