preface

Use Gradle to quickly build SpringBoot project Docker image and push the image to Ali Cloud remote image warehouse.

Gradle built SpringBoot project, Docker environment, docker-compose (not required).

This article project source address: GitHub

SpringBoot project foundation project

  1. Type Gradle

  1. Check MVC dependencies

  1. The directory structure

    1. Modifying the Application configuration file Modifies the port

    1. Add an interface to test for successful startup

Modify the build.gradle file

Configure the image to speed up the download

repositories {
    mavenLocal() // The configuration starts by looking for the jar package from the local repository
    maven { url 'http://maven.aliyun.com/nexus/content/groups/public/' }
    mavenCentral()
}
Copy the code

Add the docker-spring-boot plugin

id 'com.bmuschko.docker-spring-boot-application' version '6.6.1'
Copy the code

Add the Docker build configuration

docker {
    springBootApplication {
        baseImage = 'openjdk:8-alpine' // A basic Docker image for Spring Boot applications.
        maintainer = 'codelong' // Mirror maintainer
        ports = [8080] // Exposed port
        // Image name and version
        images = ["${project.group}/${rootProject.name}:${project.version}"."${project.group}/${rootProject.name}:latest"]
        // JVM parameters passed to the Java command.
        jvmArgs = ["-Djava.security.egd=file:/dev/./urandom".'-Xmx2048m']
        // The name of the main class used to launch the application. Setting an explicit value for this option is useful if your source code contains more than one main class file.
        // mainClassName = the unique mainClassName found by scanning the classpath by default
    }
    // If the URL is not specified
    / / Unix system default: Unix: / / / var/run/docker. The sock
    // In Windows, the default is TCP ://127.0.0.1:2375.
    / / url = 'https://192.168.3.103:2375'
}
Copy the code

Complete build.gradle after modification

plugins {
    id 'org.springframework.boot' version 'against 2.4.1'
    id 'io.spring.dependency-management' version '1.0.10. RELEASE'
    / / docker - spring - the boot plug-in
    id 'com.bmuschko.docker-spring-boot-application' version '6.6.1'
    id 'java'
}

group = 'com.codelong'
version = '1.0.0'sourceCompatibility = JavaVersion.VERSION_1_8 targetCompatibility = JavaVersion.VERSION_1_8 configurations { compileOnly  { extendsFrom annotationProcessor } } repositories { mavenLocal()// The configuration starts by looking for the jar package from the local repository
    maven { url 'http://maven.aliyun.com/nexus/content/groups/public/' }
    mavenCentral()

}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-web'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

// Docker build configuration
docker {
    springBootApplication {
        baseImage = 'openjdk:8-alpine' // A basic Docker image for Spring Boot applications.
        maintainer = 'codelong' // Mirror maintainer
        ports = [8080] // Exposed port
        // Image name and version
        images = ["${project.group}/${rootProject.name}:${project.version}"."${project.group}/${rootProject.name}:latest"]
        // JVM parameters passed to the Java command.
        jvmArgs = ["-Djava.security.egd=file:/dev/./urandom".'-Xmx2048m']
        // The name of the main class used to launch the application. Setting an explicit value for this option is useful if your source code contains more than one main class file.
        // mainClassName = the unique mainClassName found by scanning the classpath by default
    }
    // If the URL is not specified
    / / Unix system default: Unix: / / / var/run/docker. The sock
    // In Windows, the default is TCP ://127.0.0.1:2375.
    / / url = 'https://192.168.3.103:2375'
}

test {
    useJUnitPlatform()
}
Copy the code

Click the refresh button and you’ll see some more task commands

  • DockerBuildImage: Creates a Docker image.
  • DockerCreateDockerfile: Generates a Dockerfile file.
  • DockerPushImage: Push a Docker image.
  • DockerSyncBuildContext: Copy the application files to a temporary directory to create a Docker image.

Build the Docker local image

Here, for the sake of demonstration, we start with a local build, which requires a docker environment.

View the Docker version

Click To execute the build task

Look at mirror

docker images
Copy the code

Start the container

Docker-compose can be started with docker-compose. Yml

version: '3.1'
services:
  xiaoyan-oss-file:
    image: com.codelong/l-gradle-springboot-docker:latest
    container_name: l-gradle-springboot-docker
    restart: always
    environment:
      TZ: Asia/Shanghai
    ports:
      - 8080: 8080
Copy the code

If docker-compos is not installed, run can be used to start it

docker run --name l-gradle-springboot-docker -d -p 8080:8080 com.codelong/l-gradle-springboot-docker:latest
Copy the code

Check whether the container is started successfully

docker ps
Copy the code

Access to the test

Browser: http://127.0.0.1:8080/

The access succeeds.