multi-project docker build using jib

https://github.com/GoogleContainerTools/jib/tree/master/examples/multi-module

Try.

background

--I think it's common to use com.palantir.docker for gradle, but I didn't know how to make it work with multi project. ――In addition, ECR registration was very troublesome for com.palantir.docker. ――I wanted to use jib.

Constitution

rootProject--sample1 ← This is the repository that pushes to ECR
           --sample2

I tried it.

https://github.com/GoogleContainerTools/jib/tree/master/jib-gradle-plugin#quickstart Correct it like that while watching. It is important to put apply false in the rootProject plugins. If you do not enter this, an error will occur.

build.gradle(Excerpt from rootProject)


plugins {
    id 'org.springframework.boot' version '2.3.4.RELEASE'
    id 'io.spring.dependency-management' version "1.0.10.RELEASE"
    id 'com.google.cloud.tools.jib' version '2.6.0' apply false
}

ext {
    artifactGroup = 'prototype.app'
    artifactVersion = '1.0.0'
    springBootDependenciesVersion = '2.3.4.RELEASE'
}

/*Common to all projects*/
allprojects {

    ext {
        set('extention', 'jar')
        set('artifactVersion', "${artifactVersion}")
    }

    repositories {
        mavenCentral()
        jcenter()
        gradlePluginPortal()
        maven {
            url "https://plugins.gradle.org/m2/"
        }
    }
}

/*Common to Java projects*/
configure(subprojects) {

    apply plugin: 'java'
    apply plugin: 'java-library'
    apply plugin: 'org.springframework.boot'
    apply plugin: 'io.spring.dependency-management'
  
    dependencyManagement {
        imports {
            // Spring Boot
            mavenBom "org.springframework.boot:spring-boot-dependencies:${springBootDependenciesVersion}"
        }
        imports {
            // AWS SDK ver 2.X.X
            mavenBom "software.amazon.awssdk:bom:2.14.14"
        }
    }

    dependencies {
        // for Develop
        annotationProcessor 'org.springframework.boot:spring-boot-configuration-processor'
    }
}

/*Common to Docker projects*/
configure(subprojects - project('sample2') ) {
    apply plugin: "com.google.cloud.tools.jib"
}

build.gradle(sample1 excerpt)


plugins {
    id 'com.google.cloud.tools.jib'
}

jib {
    from {
        image = 'amazoncorretto:11'  
    }
    to {
        image = "XXXXXXXXXXXXXX.dkr.ecr.ap-northeast-1.amazonaws.com/prototype"
    //    tags = ["${artifactVersion}"]       
    }
    container {
        environment = [SPRING_PROFILES_ACTIVE:"prod"]
    }
}

Codebuild is also used, so create buildspec.yml. If you do not create an ECR repository, an error will occur, so I created it with terraform in advance. Also, I usually don't do this, but I added a docker pull and run in the post_build phase to see if it works.

buildspec.yml


version: 0.2

env:
  variables:
    # FIXME:
    AWS_ACCOUNT   : "XXXXXXXXXXXXX"
    IMAGE_REPO    : "prototype"
    TAG           : "latest" 
phases:
  install:
    runtime-versions:
      java: corretto11
    commands:
      - chmod +x ./gradlew
      - touch gradle.properties
      - |
        cat <<EOL >> gradle.properties
        org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m
        EOL
        
      - nohup /usr/local/bin/dockerd --host=unix:///var/run/docker.sock --host=tcp://127.0.0.1:2375 --storage-driver=overlay&
      - timeout 15 sh -c "until docker info; do echo .; sleep 1; done"

  pre_build:
    commands:
      - echo build start on `date`
      - $(aws ecr get-login --no-include-email)
      
  build:
    commands:
      - ./gradlew build 
      - ./gradlew :sample1:jib --info

  post_build:
    commands:
      #For operation check
      - docker pull ${AWS_ACCOUNT}.dkr.ecr.ap-northeast-1.amazonaws.com/${IMAGE_REPO}:${TAG}
      - docker run -p 8080:8080 ${AWS_ACCOUNT}.dkr.ecr.ap-northeast-1.amazonaws.com/${IMAGE_REPO}:${TAG}
      - echo build end on `date`
cache:
  paths:
    - /root/.gradle/caches/**/*
    - /root/.gradle/wrapper/**/*

result

At first, I was hit by the ECR specifications, but it worked. (The log has been modified like that)

Status: Downloaded newer image for XXXXXXXXXXXX.dkr.ecr.ap-northeast-1.amazonaws.com/prototype:latest
XXXXXXXXXXX.dkr.ecr.ap-northeast-1.amazonaws.com/prototype:latest

[Container] 2020/11/14 04:44:20 Running command docker run -p 8080:8080 ${AWS_ACCOUNT}.dkr.ecr.ap-northeast-1.amazonaws.com/${IMAGE_REPO}:${TAG}

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.3.4.RELEASE)

2020-11-14 04:44:24.281  INFO 1 --- [           main] p.b.transfer.BeforeTransferApplication   : Starting BeforeTransferApplication on f2ceab1be0d6 with PID 1 (/app/classes started by root in /)
2020-11-14 04:44:24.283  INFO 1 --- [           main] p.b.transfer.BeforeTransferApplication   : The following profiles are active: prod
2020-11-14 04:44:25.481  INFO 1 --- [           main] p.b.transfer.BeforeTransferApplication   : Started BeforeTransferApplication in 1.636 seconds (JVM running for 2.084)

[Container] 2020/11/14 04:44:25 Running command echo build end on `date`
build end on Sat Nov 14 04:44:25 UTC 2020

Recommended Posts

multi-project docker build using jib
How to build CloudStack using Docker
SpringBoot 2.0 multi-project build
Quick build maven project using maven docker container
Build an authentication proxy server using Docker
Build a test flow on CircleCI using Jib
Build a Kotlin app using OpenJDK's Docker container
Push the image to docker hub using Jib
[Road _node.js_1-1] Road to build Node.js Express MySQL environment using Docker
Easily build Redmine on Windows using WSL2 and Docker
Try to build a Java development environment using Docker
[2021] Build a Docker + Vagrant environment for using React / TypeScript
I tried to build an environment using Docker (beginner)
[Amateur remarks] Build multiple WordPress on AWS using Docker Compose
I tried to build the environment little by little using docker
Build docker environment with WSL
Using Docker from Java Gradle
Docker Easy Build Database (PostgreSQL)
Build DynamoDB local with Docker
People using docker Try using docker-compose
Using PlantUml with Honkit [Docker]
I tried to integrate Docker and Maven / Netbean nicely using Jib
Build a browser test environment using Capybara in the Docker development environment
Build Couchbase local environment with Docker
Try using Redmine on Mac docker
Easily Docker Java applications with Jib
Build PlantUML environment with VSCode + Docker
[Splunk] Build Splunk Enterprise (docker file creation)
Build environment with vue.js + rails + docker
Build Rails environment with Docker Compose
Build Clang x VSCode on Docker (1)
Using Docker on Windows10 Home WSL2
Build Unity development environment on docker
Use cuda11.0 with pytorch using Docker
Build docker + laravel environment with laradock
Build WebRTC Janus with Docker container
[Splunk] Build Splunk Enterprise (use docker image)
Spring Boot gradle build with Docker
Build an environment of "API development + API verification using Swagger UI" with Docker