Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

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

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 .

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

I eventually get the expected output (the compiled java code in the build folder).

I am using Docker version 1.6.2

like image 859
TPPZ Avatar asked Jul 07 '15 22:07

TPPZ


2 Answers

I solved the problem using the ENV docker instructions (link to the documentation).

ENV GRADLE_HOME=/app/gradle-2.4
ENV PATH=$PATH:$GRADLE_HOME/bin
like image 99
TPPZ Avatar answered Nov 14 '22 22:11

TPPZ


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:

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.

like image 38
Alex V Avatar answered Nov 14 '22 22:11

Alex V