如何将探查器附加到 docker 进程

How to attach profiler to docker process

我在 docker 容器中存在作业服务器内存泄漏问题。要分析导致问题的原因,我需要将 jprofiler 或 yourkit 附加到 docker 容器进程。我不知道该怎么做。有人可以解释一下吗?

您可以尝试关注“Configure JProfiler 9.2 to profiling applications running in Docker containers" from Andrew Liu:

这将涉及完成您现有的 Dockerfile:

RUN wget http://download-keycdn.ej-technologies.com/jprofiler/jprofiler_linux_9_2.tar.gz -P /tmp/ &&\
 tar -xzf /tmp/jprofiler_linux_9_2.tar.gz -C /usr/local &&\
 rm /tmp/jprofiler_linux_9_2.tar.gz

ENV JPAGENT_PATH="-agentpath:/usr/local/jprofiler9/bin/linux-x64/libjprofilerti.so=nowait"
EXPOSE 8849

这将使您能够对 运行 容器执行 bash:

docker exec -it [container-name] bash

cd /usr/local/jrofiler9/
bin/jpenable

Alternatively, if you want to enable JProfiler agent at your web server start up and wait for JProfiler GUI connecting from host, instead of putting "ENV JPAGENT_PATH="-agentpath:/usr/local/jprofiler9/bin/linux-x64/libjprofilerti.so=nowait"" in the Dockerfile. Add following line to the JAVA_OPTS. For tomcat, it will be CATALINA_OPTS.
Note: the config.xml will be the place to put your JProfiler license key.

JAVA_OPTS="$JAVA_OPTS -agentpath:/usr/local/jprofiler9/bin/linux-x64/libjprofilerti.so=port=8849,wait,config=/usr/local/jprofiler9/config.xml"

Now you are done at the docker container side. The container is ready to be attached to your JProfiler GUI. The steps below are to be done on the host machine.

  1. Download JProfiler 9.2 from https://www.ej-technologies.com/download/jprofiler/files and install it.
  2. Open JProfiler and open a new session by press Ctrl + N or Click 'New Session' in Session menu.
  3. Select 'Attach to profiled JVM (local or remote)' in Session Type section. Enter the IP address and 8849 as profiling port in Profiled JVM Settings section. Leave the other settings as default. Then click OK.

您可以像这样将 jProfiler 附加到 docker 容器内的应用程序:

EXPOSE 8849

Exposing the profiling port is important (8849 is default)

RUN wget http://download-keycdn.ej-technologies.com/jprofiler/jprofiler_linux_9_2_1.tar.gz --no-verbose -P /tmp/ && \
  tar -xzf /tmp/jprofiler_linux_9_2_1.tar.gz -C /usr/local && \
  rm /tmp/jprofiler_linux_9_2_1.tar.gz

This downloads and extracts the jProfiler inside your docker container, when the container is build.

ENTRYPOINT exec java -jar /app.jar & \

 echo $! >>/tmp/process.pid && \

 sleep 60s && \

 /usr/local/jprofiler9/bin/jpenable --pid=$(cat /tmp/process.pid) --gui --port=8849 && \

 while true; do sleep 2147483647; done

This is how I dealt with the fact that you can't run two applications inside one docker container. First we execute the jar and save the processId to a file. Then we simply wait 60 seconds and after that we start the jProfiler (jpenable) and attach it to our process (via processId). The while loop is necessary to keep the container running afterwards.