Detailed steps to use Arthas in a Docker container

Detailed steps to use Arthas in a Docker container

What can Arthas do for you?

Arthas is Alibaba's open source Java diagnostic tool, which is deeply loved by developers.

When you are at a loss as to what to do with the following problems, Arthas can help you solve them:

From which jar package is this class loaded? Why are various types of Exceptions reported? Why is the code I changed not executed? Is it because I didn't commit? Wrong branch? If I encounter a problem and cannot debug it online, is the only way to add logs and re-publish it? There is a problem with the data processing of a certain user online, but it cannot be debugged online and cannot be reproduced offline! Is there a global view of how the system is performing? Is there any way to monitor the real-time running status of JVM? How to quickly locate application hotspots and generate flame graphs? How to find an instance of a class directly from the JVM?

Arthas supports JDK 6+, Linux/Mac/Windows, uses command line interaction mode, and provides rich Tab auto-completion functions to further facilitate problem location and diagnosis.

For specific content, please refer to the official documentation, each command has a detailed description: https://arthas.aliyun.com/doc/

This article does not introduce how to use arthas. What we want to talk about here is how to use arthas in our docker container.

Since it is rather cumbersome to use arthas in a docker container, we need to find the container ID, copy the entire arthas directory to the container, enter the container, switch to the user of the target service, and start arthas. These steps are not friendly to many students who are not familiar with Linux commands and docker commands.

Therefore, I wrote a script that can directly replace the above steps. The effect is shown in the figure below:

Simply enter the complete service name (here the IMAGE name of the container) after the script and you can use it. It is simple and convenient.

How to use: First, you need to unzip arhas-bin.zip on the Linux server, and the unzipped version is the arthas software. Make sure Docker is installed on your machine

arhas-bin.zip download directory: https://github.com/alibaba/arthas/releases

Put the arthasDocker.sh script into the arthas directory you just unzipped, open the script, edit the ARTHAS_PATH variable, and change it to the directory where you placed arthas.

Contents of the arthasDocker.sh script:

#!/bin/bash
#
# author: dijia478
# date: 2020-8-20 18:14:38
# desc: This script needs to be placed in the arthas directory and copied into the docker container along with the entire directory. The main purpose is to switch the user of the target service in the container and start arthas

echo "Start querying the process ID and user of the target service..."
PID=`ps -eo pid,user=aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa -o args | grep java | grep -v grep | awk '{print $1}'`
echo "The process id of the target service is ${PID}"
USER=`ps -eo pid,user=aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa -o args | grep java | grep -v grep | awk '{print $2}'`
echo "The user of the target service is ${USER}"

if [[ ! -d "/home/${USER}" ]]
then
  mkdir -p /home/${USER}
  echo "Create directory /home/${USER}"
fi
chmod 777 /home/${USER}

echo "Start switching users and starting arthas..."
# The following arthas path needs to be modified and should be consistent with the startArthas.sh script ARTHAS_PATH="/opt/arthas"
su ${USER} -c "java -jar ${ARTHAS_PATH}/arthas-client.jar 127.0.0.1 3658 -c 'stop'"
su ${USER} -c "java -jar ${ARTHAS_PATH}/arthas-boot.jar ${PID}"

Put the startArthas.sh script on the Linux server, it is recommended to put it in the ~ directory, open the script, edit the ARTHAS_PATH variable, and change it to the directory where your arthas is placed. Then give the script execution permissions

startArthas.sh script content:

#!/bin/bash
#
# author: dijia478
# date: 2020-9-18 10:36:27
# desc: The main purpose of this script is to start the arthas diagnostic tool to diagnose a java service in a docker if [[ ${1} == '' ]]
then
  echo "Please select a service:"
  sudo docker ps | awk 'NR>1 {print $2}'
  exit 0
fi

echo "Starting to search for container for service ${1}..."
DOCKER_LIST=`sudo docker ps | awk 'NR>1 {print $2}'`
FLAG=0
for i in ${DOCKER_LIST[@]}
do
  if [[ ${i} == ${1} ]]
  then
    FLAG=1
    break
  fi
done

if [[ ${FLAG} == 0 ]]
then
  DOCKER_NAME=`sudo docker ps | awk 'NR>1 {print $2}' | grep ${1}`
  if [[ ${DOCKER_NAME} == '' ]]
  then
    echo "The container for this service was not found. Please reselect the service:"
    sudo docker ps | awk 'NR>1 {print $2}'
  else
    echo "Please enter the full name of the service:"
    sudo docker ps | awk 'NR>1 {print $2}' | grep ${1}
  fi

else
  ID=`sudo docker ps --filter ancestor=${1} | awk '{print $1}' | sed -n '2p'`
  echo "Found container ${ID}"

  echo "Start copying arthas to the container..."
  # The following arthas path needs to be modified and should be consistent with the arthasDocker.sh script ARTHAS_PATH="/opt/arthas"
  sudo docker exec -it ${ID} /bin/bash -c "rm -rf ${ARTHAS_PATH}"
  sudo docker cp ${ARTHAS_PATH} ${ID}:${ARTHAS_PATH}
  echo "Copy completed"

  echo "About to enter the container..."
  sudo docker exec -it ${ID} /bin/bash -c "bash ${ARTHAS_PATH}/arthasDocker.sh"
fi

Finally, just run the startArthas.sh script

© Copyright Statement The copyright of the article belongs to the author. Reprinting is welcome, but the original link must be given, otherwise the right to pursue legal liability is reserved. THE END

This is the end of this article on how to use Arthas in Docker containers. For more information about using Arthas with Docker, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • The lambda expression causes Arthas to fail to redefine

<<:  Sharing experience on the priority of CSS style loading

>>:  How to hide and remove scroll bars in HTML

Recommend

Detailed explanation of browser negotiation cache process based on nginx

This article mainly introduces the detailed proce...

JavaScript to implement login form

This article example shares the specific code of ...

Linux operation and maintenance basics httpd static web page tutorial

Table of contents 1. Use the warehouse to create ...

How to use Docker container to access host network

Recently, a system was deployed, using nginx as a...

Common failures and reasons for mysql connection failure

=================================================...

Native js to implement form validation function

Table of contents When developing, analyzing the ...

MySQL Tutorial: Subquery Example Detailed Explanation

Table of contents 1. What is a subquery? 2. Where...

Postman automated interface testing practice

Table of contents Background Description Creating...

How to build sonarqube using docker

Table of contents 1. Install Docker 2. Install so...

Question about custom attributes of html tags

In previous development, we used the default attr...

The image element img has extra blank space in IE6

When doing DIV+CSS layout of the page, it is very...

Vue implements pull-down to load more

Developers familiar with Element-UI may have had ...

Analysis of Hyper-V installation CentOS 8 problem

CentOS 8 has been released for a long time. As so...