I am trying to switch from Cmake to gradle. I want to configure gradle to work as follow
$ cd myapp && ls myapp
$ Dockerfile build.gradle src
$ gradle build
- Build the docker image from docker file
- start container
- build the application
The docker image contains complete environment for my app.
FROM debian:stretch
RUN apt-get update -y && apt install -y git
python3-dev libncurses5-dev libxml2-dev
libedit-dev swig doxygen graphviz xz-utils ninja-build
RUN echo "deb http://ftp.de.debian.org/debian stretch main" >> /etc/apt/source.list
RUN apt-get update && apt-get install -y openjdk-8-jre openjdk-8-jdk
# Clang 8 as a compiler
RUN apt-get update && apt-get install -y
xz-utils
build-essential
curl
&& rm -rf /var/lib/apt/lists/*
&& curl -SL http://releases.llvm.org/8.0.0/clang+llvm-8.0.0-x86_64-linux-gnu-ubuntu-18.04.tar.xz
| tar -xJC . &&
mv clang+llvm-8.0.0-x86_64-linux-gnu-ubuntu-18.04 clang_8.0.0 &&
echo 'export PATH=/clang_8.0.0/bin:$PATH' >> ~/.bashrc &&
echo 'export LD_LIBRARY_PATH=/clang_8.0.0/lib:LD_LIBRARY_PATH' >> ~/.bashrc
#
RUN apt-get update
#install sdkman
RUN rm /bin/sh && ln -s /bin/bash /bin/sh
RUN apt-get -qq -y install curl wget unzip zip
RUN curl -s "https://get.sdkman.io" | bash
RUN source "$HOME/.sdkman/bin/sdkman-init.sh"
#install gradle
RUN yes | /bin/bash -l -c 'sdk install gradle 6.1'
PS: This is cpp project
3
Answers
You can build a docker image from Gradle tasks by using
**com.bmuschko:gradle-docker-plugin:3.1.0 plugin
**read the documentation for more details https://bmuschko.github.io/gradle-docker-plugin/
Build an image from Gradle task – ./gradlew taskname
To start container and run the command inside it you can use CMD or ENTRYPOINT and specify the command in Dockerfile
CMD [“start.sh”]
in start.sh you can specify your command to be executed after running the container
Add docker plugin first
create simple task like this in build.gradle file
Let me slightly clean up that Dockerfile first:
The important bits are at the bottom: it creates a
/src
directory and executesgradle build
there. All that remains for you is to make that directory available when you build.Assuming you built the container once with
docker build -t my-build-container .
, you can run it as follows:Depending on your build system, this might pollute your source tree with various build artifacts owned by root. If so, consider switching to out-of-tree builds by changing the default working directory to
/work
instead. All build results will go to/work
, and you can extract them from the container afterwards.