思路:将Contrast配置到liberty上,希望当liberty运行的时候,开启Contrast;首先在项目的build.gradle中添加Contrast的必要依赖;然后将项目打成war;接下来在自定义目录/mydocker中创建Dockerfile文件并编辑;将项目war包拷贝到自定义目录/mydocker中,将Contrast.xxx.jar拷贝到/mydocker目录中;最后构建image。
- build.gradle配置文件内容如下,
apply plugin: 'application'
apply plugin: 'war'
apply plugin: 'liberty'
apply plugin: 'docker'
group = 'com.cczhao'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'
targetCompatibility = '1.8'
tasks.withType(JavaCompile) {
options.encoding = 'UTF-8'
}
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'net.wasdev.wlp.gradle.plugins:liberty-gradle-plugin:2.6.3'
classpath('se.transmode.gradle:gradle-docker:1.2')
}
}
//configure Contrast
configurations {
contrastAgent
}
repositories {
mavenCentral()
}
def contrast_version = "3.6.3.8220"
dependencies {
//Contrast configuration
contrastAgent "com.contrastsecurity:contrast-agent:${contrast_version}"
libertyRuntime group:'io.openliberty', name:'openliberty-runtime', version:'[17.0.0.4,)'
compile group: 'org.springframework.boot', name: 'spring-boot-starter-web', version: '2.1.7.RELEASE'
//testImplementation 'org.springframework.boot:spring-boot-starter-test'
testCompile group:'junit', name:'junit', version:'4.12'
}
application {
mainClassName = "com.ihep.ac.TestcontrastApplication"
}
jar{
baseName='gradle-docker-contrast'
version='0.1.0'
}
ext {
appName = project.name
testServerHttpPort = 9080
testServerHttpsPort = 9443
warContext = appName
}
liberty {
server {
name = "${appName}Server"
configFile = file("src/main/liberty/config/server.xml")
bootstrapProperties = ['default.http.port': testServerHttpPort,
'default.https.port': testServerHttpsPort,
'app.context.root': warContext]
packageLiberty {
archive = "$buildDir/${appName}.zip"
include = "usr"
}
}
}
war {
archiveName = "${baseName}.${extension}"
}
- 在项目根目录下,打成war包,
gradle clean build
image.png
-
新建文件夹/mydocker ,并把项目war包拷贝到/mydocker中,以及contrast-agent-3.6.3.8220.jar拷贝其中,再新建Dockerfile文件,
image.png
编辑Dockerfile文件如下,
FROM websphere-liberty:kernel
COPY contrast-agent-3.6.3.8220.jar /home/
# Add my app and config
COPY --chown=1001:0 GradleSample.war /config/dropins/
#COPY --chown=1001:0 src/main/liberty/config/server.xml /config/
ENV CONTRAST__API__URL https://ce.contrastsecurity.com/Contrast/
ENV CONTRAST__API__API_KEY Y6Xv6205Gr1h7u5EWteLOAyiRluTWIAf
ENV CONTRAST__API__USER_NAME agent_9e9e9db1-c274-4f95-a82d-ef6a7b6c6599@MingsOrg
ENV CONTRAST__API__SERVICE_KEY YT1GRAXTCS7L2YWQ
#最简单的方法:可以通过设置下面这个变量来控制contrast是否需要启动
ENV JAVA_TOOL_OPTIONS -javaagent:/home/contrast-agent-3.6.3.8220.jar
# This script will add the requested XML snippets, grow image to be fit-for-purpose and apply interim fixes
RUN configure.sh
- 在/mydocker目录构建image,
docker build .
-
查询刚刚构建的image,并启动,
image.png
image.png - 从liberty启动日志可以看到 Contrast已启动成功!
网友评论