zoukankan      html  css  js  c++  java
  • Docker 中无法使用 JDK jhsdb jmap之 Can't attach to the process: ptrace(PTRACE_ATTACH问题

    原文

    Docker 中无法使用 JDK jmap之 Can't attach to the process: ptrace(PTRACE_ATTACH问题

    问题描述

    一个老服务最近出现OOM问题了(日志中发现的),但是由于启动参数没有添加-XX:HeapDumpOnOutOfMemoryError无法获取dump文件,这时想着使用jmap获取dump文件,结果执行jmap报如下错:

    [root@cn8011sl-meet01 jdk-11.0.4]# jhsdb jmap --heap --pid 14980
    Attaching to process ID 14980, please wait...
    ERROR: ptrace(PTRACE_ATTACH, ..) failed for 14980: Operation not permitted
    Error attaching to process: sun.jvm.hotspot.debugger.DebuggerException: Can't attach to the process: ptrace(PTRACE_ATTACH, ..) failed for 14980: Operation not permitted
    sun.jvm.hotspot.debugger.DebuggerException: sun.jvm.hotspot.debugger.DebuggerException: Can't attach to the process: ptrace(PTRACE_ATTACH, ..) failed for 14980: Operation not permitted
    at jdk.hotspot.agent/sun.jvm.hotspot.debugger.linux.LinuxDebuggerLocal$LinuxDebuggerLocalWorkerThread.execute(LinuxDebuggerLocal.java:163)
    at jdk.hotspot.agent/sun.jvm.hotspot.debugger.linux.LinuxDebuggerLocal.attach(LinuxDebuggerLocal.java:274)
    at jdk.hotspot.agent/sun.jvm.hotspot.HotSpotAgent.attachDebugger(HotSpotAgent.java:672)
    at jdk.hotspot.agent/sun.jvm.hotspot.HotSpotAgent.setupDebuggerLinux(HotSpotAgent.java:612)
    at jdk.hotspot.agent/sun.jvm.hotspot.HotSpotAgent.setupDebugger(HotSpotAgent.java:338)
    at jdk.hotspot.agent/sun.jvm.hotspot.HotSpotAgent.go(HotSpotAgent.java:305)
    at jdk.hotspot.agent/sun.jvm.hotspot.HotSpotAgent.attach(HotSpotAgent.java:141)
    at jdk.hotspot.agent/sun.jvm.hotspot.tools.Tool.start(Tool.java:185)
    at jdk.hotspot.agent/sun.jvm.hotspot.tools.Tool.execute(Tool.java:118)
    at jdk.hotspot.agent/sun.jvm.hotspot.tools.JMap.main(JMap.java:176)
    at jdk.hotspot.agent/sun.jvm.hotspot.SALauncher.runJMAP(SALauncher.java:326)
    at jdk.hotspot.agent/sun.jvm.hotspot.SALauncher.main(SALauncher.java:455)
    Caused by: sun.jvm.hotspot.debugger.DebuggerException: Can't attach to the process: ptrace(PTRACE_ATTACH, ..) failed for 14980: Operation not permitted
    at jdk.hotspot.agent/sun.jvm.hotspot.debugger.linux.LinuxDebuggerLocal.attach0(Native Method)
    at jdk.hotspot.agent/sun.jvm.hotspot.debugger.linux.LinuxDebuggerLocal$1AttachTask.doit(LinuxDebuggerLocal.java:265)
    at jdk.hotspot.agent/sun.jvm.hotspot.debugger.linux.LinuxDebuggerLocal$LinuxDebuggerLocalWorkerThread.run(LinuxDebuggerLocal.java:138)

    问题分析

    百度和google之后,我们发现docker的官方文档提到了。

    这不是什么 docker 或者jmap的Bug,而是 Docker 自 1.10 版本开始加入的安全特性。类似于 jmap 这些 JDK 工具依赖于 Linux 的 PTRACE_ATTACH,而是 Docker 自 1.10 在默认的 seccomp 配置文件中禁用了 ptrace。

    主要提及三种:

    1.1 –security-opt seccomp=unconfined

    直接关闭 seccomp 配置或者将ptrace添加到允许的名单中。用法:

    docker run --security-opt seccomp:unconfined ...
    

    官方文档提到you can pass unconfined to run a container without the default seccomp profile. 连接为https://docs.docker.com/engine/security/seccomp/

    1.2 –cap-add=SYS_PTRACE

    使用 --cap-add 明确添加指定功能:

    docker run --cap-add=SYS_PTRACE ...
    

    官方文档连接https://docs.docker.com/engine/reference/run/

     
    在这里插入图片描述

    1.3 Docker Compose 的支持

    Docker Compose 自 version 1.1.0 (2015-02-25) 起支持 cap_add。官方文档:cap_add, cap_drop。用法:

    前面的 docker-compose.yml 改写后文件内容如下(相同内容部分就不重复贴了):

    version: '2'
    
    services:
      mysql:
        ...
      api:
        ...
        cap_add:
          - SYS_PTRACE
    

    官方连接在https://docs.docker.com/compose/compose-file/compose-file-v2/#cap_add-cap_drop

    最终解决办法

    本人最终以类似如下方式启动容器,然后 执行jmap命令

    启动容器的命令参数
    docker run --cap-add=SYS_PTRACE -d -p 8080:8080 hello:v2  --name helloWithPtrace

  • 相关阅读:
    MySQL数据库学习笔记(八)----JDBC入门及简单增删改数据库的操作
    vim使用技巧大全
    virtualbox下centos虚拟机安装增强工具教程和常见错误解决
    python初始化list列表(1维、2维)
    Python中列表(list)、字典(dict)排序的程序
    字符画
    php 过滤器filter_var验证邮箱/url/ip等
    Python3 MySQL 数据库连接
    PHP socket 服务器框架集
    python3 获取int最大值
  • 原文地址:https://www.cnblogs.com/thirteen-yang/p/14452199.html
Copyright © 2011-2022 走看看