9715. Source Code Search with OpenGrokOpenGrok
Use OpenGrok in docker for search source code.
1. OpenGrok
OpenGrok is a fast and usable source code search and cross reference engine, written in Java. It helps you search, cross-reference and navigate your source tree. It can understand various program file formats and version control histories of many source code management systems.
2. Docker Image for OpenGrok
You can manually setup OpenGrok from scratch, but it is much easier to use the docker image for OpenGrok. There is one image called ‘docker-opengrok’ on Docker Hub.
3. Setting up OpenGrok with Docker
3.1 Preparing Source Code
Create two directories in local disk.
$ mkdir -p /opengrok/src /opengrok/data
src
- Contains your source files.data
- Used by OpenGrok. OpenGrok will generate indexes for the source files and store them here.
Download source code from GitHub to ‘src’ folder. Here, I cloned my three repositories.
$ cd /opengrok/src
$ git clone https://github.com/jojozhuang/Algorithm
$ git clone https://github.com/jojozhuang/Portfolio
$ git clone https://github.com/jojozhuang/Tutorials
Check directories/files.
Johnny@Johnny-Mac:~$ ls -all
total 0
drwxr-xr-x 5 Johnny 1694527156 160 May 03 21:34 .
drwxr-xr-x 5 Johnny 1694527156 160 May 03 21:20 ..
drwxr-xr-x 12 Johnny 1694527156 384 May 03 21:31 Algorithm
drwxr-xr-x 24 Johnny 1694527156 768 May 03 21:34 Portfolio
drwxr-xr-x 59 Johnny 1694527156 1888 May 03 21:10 Tutorials
3.2 Installing Image and Running Container
Pull the OpenGrok docker image.
$ docker pull opengrok/docker
Run a Docker container and mount these two directories: src and data; this will automatically run indexing as a part of startup.
$ docker run --name=opengrok-git -v /opengrok/src:/src -v /opengrok/data:/data -p 31030:8080 opengrok/docker
Later, you can use the following commands to start and stop the container.
$ docker start opengrok-git
$ docker stop opengrok-git
Use the following command to check logs.
$ docker logs opengrok-git
3.3 Issues
Sometimes, the opengrok indexer stops working, and you see the following message in the logs.
Indexer still locked, skipping indexing
This is because of the locker check, see here https://github.com/OpenGrok/docker/blob/master/scripts/index.sh.
#!/bin/bash
LOCKFILE=/var/run/opengrok-indexer
URI="http://localhost:8080"
if [ -f "$LOCKFILE" ]; then
date +"%F %T Indexer still locked, skipping indexing"
exit 1
fi
...
This issues occurs especially when indexing stops unexpectedly. The solution is to get inside the container and delete the file /var/run/opengrok-indexer
.
$ docker exec -it opengrok-git bash
rm /var/run/opengrok-indexer
When the next time indexing happens, you won’t see this error, and indexer should start working again.
4. Searching Code
The OpenGrok application will now be running on http://192.168.99.100:31030/source/. Notice, we have three projects as we cloned three repositories into the ‘src’ folder.
Search ‘trie’ in algorithm, all the questions related to trie are displayed. Search ‘course player’ in portfolio, all my portfolios related to ‘course player’ are displayed. Search ‘docker’ in tutorials, all my tutorials related to ‘docker’ are displayed.