skip to Main Content

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
  1. Build the docker image from docker file
  2. start container
  3. 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


  1. You can build a docker image from Gradle tasks by using **com.bmuschko:gradle-docker-plugin:3.1.0 plugin**

    buildscript {
        repositories {
            jcenter()
                    mavenCentral()
        }
    
        dependencies {
            classpath 'com.bmuschko:gradle-docker-plugin:3.1.0'
        }
    }
    
    apply plugin: 'com.bmuschko.docker-remote-api'
    
        import com.bmuschko.gradle.docker.tasks.image.Dockerfile
        import com.bmuschko.gradle.docker.tasks.image.DockerBuildImage
        import com.bmuschko.gradle.docker.tasks.image.*
    
        task buildImage(type: DockerBuildImage) {
                group = ''
                inputDir = file('.')
                tag = 'image name:'+tag
        }
    

    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

    Login or Signup to reply.
  2. Add docker plugin first

    buildscript {
        dependencies {
            classpath("se.transmode.gradle:gradle-docker:1.2")
        }
    }
    

    create simple task like this in build.gradle file

    task buildDocker(type: Docker, dependsOn: build) {
        push = false
        project.group = 'testProject'
        project.archivesBaseName = jar.baseName
        applicationName = jar.baseName
        dockerfile = file('src/main/docker/Dockerfile')
        doFirst {
            copy {
                from jar
                into stageDir
            }
        }
    }
    
    Login or Signup to reply.
  3. Let me slightly clean up that Dockerfile first:

    FROM debian:stretch
    
    RUN echo "deb http://ftp.de.debian.org/debian stretch main" >> /etc/apt/source.list
    RUN apt-get update -y && apt install -qq -y 
          python3-dev libncurses5-dev libxml2-dev 
          libedit-dev swig doxygen graphviz xz-utils ninja-build 
          openjdk-8-jre openjdk-8-jdk  
          xz-utils curl git build-essential wget unzip zip
    
    # Clang 8 as a compiler
    RUN 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
    
    #install sdkman 
    RUN ln -fs /bin/bash /bin/sh
    RUN curl -s "https://get.sdkman.io" | bash
    RUN source "$HOME/.sdkman/bin/sdkman-init.sh"
    RUN yes | /bin/bash -l -c 'sdk install gradle 6.1'
    
    RUN mkdir /src /work
    WORKDIR /src
    ENTRYPOINT gradle build -p /src
    

    The important bits are at the bottom: it creates a /src directory and executes gradle 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:

    docker run -v $(pwd):/src my-build-container
    

    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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search