docker - 在容器中调试Groovy脚本

标签 docker jenkins groovy

我正在构建一个 Jenkins 容器,并希望在初始流程中设置KeycloakSecurityRealm。我已经有了一个适用于默认设置的脚本:

    import hudson.*
    import hudson.model.*
    import jenkins.*
    import jenkins.model.*
    import java.util.logging.Logger
    import hudson.security.*
    import java.util.*
    import com.michelin.cio.hudson.plugins.rolestrategy.*
    import java.lang.reflect.*

    import org.jenkinsci.plugins

    /*
    NOTE:  that megascript should be split someday, when my groovy skills improves.
    */

    Thread.start {
        //vbles definition
        def logger = Logger.getLogger("")
        def instance = Jenkins.getInstance()
        def pm = instance.getPluginManager()
        def uc = instance.getUpdateCenter()
        def installed = false
    // More code...

    //No plugins to update. Security can be set
        logger.info("All plugins are up to date")
        logger.info("Setting security")

        //  logger.info("Creating initial user")
        //  hudsonRealm.createAccount("USER","USER")

        logger.info("Creating reaml")
        def keycloakSecurityRealm = keycloakRealm.createSecurityComponents()

        //realm based on jenkins database
        logger.info("Setting Security realm to KEYCLOAK")
        instance.setSecurityRealm(keycloakSecurityRealm)

        //not anonymous read allowed

没有任何信息,此代码将崩溃。我想调试容器,因此我检查了可以运行容器崩溃的步骤来附加交互式控制台:

enter image description here

可以做
docker run -ti 3ed

而且我可以以根用户身份访问该容器。

问题是我无法调试脚本。我已经检查了使用jdb的某些方法,但是在所有情况下都需要运行它的邮件Java类。原始脚本在不更改的情况下可以正常运行,因此它可以在不包含任何类的情况下运行文件。

如何调试容器内的此类Java脚本?

谢谢。

最佳答案

一旦进入容器,请确保以 Debug模式运行代码。

java -agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=32887 ...

如果您没有机会自己启动Java,则可以随时使用_JAVA_OPTIONS
export _JAVA_OPTIONS=-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=32887

将您的调试器(例如IntelliJ)连接到远程计算机(基于Docker的容器)。从容器外部启动的IDE中调试代码。

它应该可以解决问题。

对于样本(用于剖析),请看这里:

https://github.com/mkowsiak/ProfilerDocker

这不是一回事,但是从某种意义上说,这类似,基于Docker的代码(Java)在容器内执行,而探查器(在容器外运行)则附加到远程机器上。

关于docker - 在容器中调试Groovy脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53997817/

相关文章:

windows - Docker:Hyper-V在尝试访问计算机 'host-machine'上的对象时遇到错误

continuous-integration - Jenkins (Jenkins)-在所有下游工作完成后触发工作,而不仅仅是立即完成的工作

spring - ref() 没有在 grails 上使用 spring dsl 获取嵌套映射值的 bean 配置

meteor - ./配置: error: the HTTP gzip module requires the zlib library

python - Docker容器和虚拟python环境

iphone - 单色触摸 : Build iOS app with mdtool from command line: Multiple provisioning profiles

Jenkins 三叶草 REST API

gradle - 如何为 Gradle 依赖项 {} block 编写扩展方法

xml - 常规 : Compare SOAP Response with XML file

python - 在将远程调试附加到Docker容器中的Flask应用程序期间,Visual Studio代码被卡住