Docker Dockerfile 验证Docker内部使用jmap报错问题解决

Posted boonya

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Docker Dockerfile 验证Docker内部使用jmap报错问题解决相关的知识,希望对你有一定的参考价值。

对于线上排查问题,如果不能轻松使用一些Java命令行工具真的有点崩溃。

目录

异常信息

类型一:Can't attach to the process

类型二:unknown CollectedHeap type

实验验证

先决条件

Dockerfile创建

镜像创建

运行示例

进入容器测试jmap

参考文章


异常信息

类型一:Can't attach to the process

Caused by: sun.jvm.hotspot.debugger.DebuggerException: Can't attach to the process: ptrace(PTRACE_ATTACH, ..) failed for 14: 不允许的操作

类型二:unknown CollectedHeap type

Caused by: java.lang.RuntimeException: unknown CollectedHeap type : class sun.jvm.hotspot.gc_interface.CollectedHeap

实验验证

先决条件

Dockerfile创建

FROM ubuntu:20.04
MAINTAINER boonya <boonya@sina.com>
# now add java and tomcat support in the container
ADD jdk-8u291-linux-x64.tar.gz /usr/local/
#持久化到指定目录
VOLUME /tmp
# 将工程jar包(springboot-angular-0.0.1-SNAPSHOT.jar)拷贝到 app.jar中
ADD springboot-angular-0.0.1-SNAPSHOT.jar /app.jar
# configuration of java and tomcat ENV
ENV JAVA_HOME /usr/local/jdk1.8.0_291
ENV CLASSPATH $JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
ENV PATH $PATH:$JAVA_HOME/bin
# container listener port
EXPOSE 8080
# 执行jar包  "-Djava.security.egd=file:/dev/./urandom"加快随机数产生过程
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]

镜像创建

[root@boonya java]# docker build -t boonya/springboot-angular .
Sending build context to Docker daemon  527.8MB
Step 1/10 : FROM ubuntu:15.06
manifest for ubuntu:15.06 not found: manifest unknown: manifest unknown
[root@boonya java]# vi Dockerfile 
[root@boonya java]# docker build -t boonya/springboot-angular .
Sending build context to Docker daemon  527.8MB
Step 1/10 : FROM ubuntu:20.04
20.04: Pulling from library/ubuntu
c549ccf8d472: Pull complete 
Digest: sha256:aba80b77e27148d99c034a987e7da3a287ed455390352663418c0f2ed40417fe
Status: Downloaded newer image for ubuntu:20.04
 ---> 9873176a8ff5
Step 2/10 : MAINTAINER boonya <boonya@sina.com>
 ---> Running in 7022c0ee27cd
Removing intermediate container 7022c0ee27cd
 ---> 54cc9700971d
Step 3/10 : ADD jdk-8u291-linux-x64.tar.gz /usr/local/
 ---> 6961cba3491b
Step 4/10 : VOLUME /tmp
 ---> Running in 96b64b89d45d
Removing intermediate container 96b64b89d45d
 ---> ac1b616b2604
Step 5/10 : ADD springboot-angular-0.0.1-SNAPSHOT.jar /app.jar
 ---> 363ea3074b32
Step 6/10 : ENV JAVA_HOME /usr/local/jdk1.8.0_291
 ---> Running in 8aadc23e5bc5
Removing intermediate container 8aadc23e5bc5
 ---> 0b96e2a88e16
Step 7/10 : ENV CLASSPATH $JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
 ---> Running in ba105757392a
Removing intermediate container ba105757392a
 ---> d5f74064a5ca
Step 8/10 : ENV PATH $PATH:$JAVA_HOME/bin
 ---> Running in 9c713c0c1d58
Removing intermediate container 9c713c0c1d58
 ---> 1f36c428ee4d
Step 9/10 : EXPOSE 8080
 ---> Running in ea0689d40009
Removing intermediate container ea0689d40009
 ---> 8bfcd6e364af
Step 10/10 : ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]
 ---> Running in 9310b710652d
Removing intermediate container 9310b710652d
 ---> 985346a8bf9d
Successfully built 985346a8bf9d
Successfully tagged boonya/springboot-angular:latest

运行示例

[root@boonya java]# docker run -p 8080:8080 --name sa boonya/springboot-angular:latest --cap-add=SYS_PTRACE

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

2021-07-02 06:43:05.895  INFO 1 --- [           main] c.b.s.a.SpringbootAngularApplication     : Starting SpringbootAngularApplication v0.0.1-SNAPSHOT using Java 1.8.0_291 on c0a283afb31b with PID 1 (/app.jar started by root in /)
2021-07-02 06:43:05.900  INFO 1 --- [           main] c.b.s.a.SpringbootAngularApplication     : No active profile set, falling back to default profiles: default
2021-07-02 06:43:07.596  INFO 1 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2021-07-02 06:43:07.619  INFO 1 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2021-07-02 06:43:07.619  INFO 1 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.46]
2021-07-02 06:43:07.680  INFO 1 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2021-07-02 06:43:07.680  INFO 1 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 1719 ms
2021-07-02 06:43:08.083  INFO 1 --- [           main] o.s.b.a.w.s.WelcomePageHandlerMapping    : Adding welcome page: class path resource [static/index.html]
2021-07-02 06:43:08.248  INFO 1 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2021-07-02 06:43:08.270  INFO 1 --- [           main] c.b.s.a.SpringbootAngularApplication     : Started SpringbootAngularApplication in 3.296 seconds (JVM running for 3.894)
2021-07-02 06:43:08.278  INFO 1 --- [           main] o.s.b.a.ApplicationAvailabilityBean      : Application availability state LivenessState changed to CORRECT
2021-07-02 06:43:08.281  INFO 1 --- [           main] o.s.b.a.ApplicationAvailabilityBean      : Application availability state ReadinessState changed to ACCEPTING_TRAFFIC

进入容器测试jmap

[root@boonya java]# docker exec -it sa  /bin/sh
# jps
1 jar
43 Jps
# jp^Hm^H^H^H
/bin/sh: 2:: not found
# jmap -heap 1
Attaching to process ID 1, please wait...
Debugger attached successfully.
Server compiler detected.
JVM version is 25.291-b10

using thread-local object allocation.
Parallel GC with 2 thread(s)

Heap Configuration:
   MinHeapFreeRatio         = 0
   MaxHeapFreeRatio         = 100
   MaxHeapSize              = 981467136 (936.0MB)
   NewSize                  = 20971520 (20.0MB)
   MaxNewSize               = 327155712 (312.0MB)
   OldSize                  = 41943040 (40.0MB)
   NewRatio                 = 2
   SurvivorRatio            = 8
   MetaspaceSize            = 21807104 (20.796875MB)
   CompressedClassSpaceSize = 1073741824 (1024.0MB)
   MaxMetaspaceSize         = 17592186044415 MB
   G1HeapRegionSize         = 0 (0.0MB)

Heap Usage:
PS Young Generation
Eden Space:
   capacity = 125829120 (120.0MB)
   used     = 120039720 (114.47879791259766MB)
   free     = 5789400 (5.521202087402344MB)
   95.39899826049805% used
From Space:
   capacity = 6291456 (6.0MB)
   used     = 0 (0.0MB)
   free     = 6291456 (6.0MB)
   0.0% used
To Space:
   capacity = 6815744 (6.5MB)
   used     = 0 (0.0MB)
   free     = 6815744 (6.5MB)
   0.0% used
PS Old Generation
   capacity = 36175872 (34.5MB)
   used     = 9333400 (8.901023864746094MB)
   free     = 26842472 (25.598976135253906MB)
   25.800069173177082% used

11384 interned Strings occupying 967408 bytes.

参考文章

https://blog.csdn.net/russle/article/details/99708261

https://www.jb51.net/article/201430.htm

以上是关于Docker Dockerfile 验证Docker内部使用jmap报错问题解决的主要内容,如果未能解决你的问题,请参考以下文章

Docker 入门:Dockerfile

docker 笔记第一个dockerfile

Docker-dockerfile文件

Docker-dockerfile文件

Docker 学习笔记总结

Docker 学习笔记总结