IntelliJ IDEA – remote debug of java code inside docker container

Since I do Java development on windows and IntelliJ, sometimes I need to do remote debugging of files inside docker image that is running inside VM. The problem that I have here is that each time I want to debug, I would need to pack project as .jar (long process), then go to shell of Linux running inside docker and run java -jar in server mode (waiting for my connection), and then go to IntelliJ and run app with Remote config.

This plugin that I created (Remote Debug Plugin), when installed is automatically communicating with remote docker process, and all output of this remote process is redirected directly to IntelliJ console – so there is no need to go to two places – just press DEBUG icon. Prerequisites are that you need to start my image (https://hub.docker.com/r/bojanv55/rds/) that has this java remote process running. Also, in order to run your files, all your windows drives (C:, D:, …) needs to be mounted inside docker image.

Hare is a walk-trough of installation

Inside IntelliJ you will need this plugging installed:

Currently plugin is being approved by Jetbrains, so in case it is not already in their repo, you can download it manually from this address https://github.com/bojanv55/remote-debug/blob/master/remote-debug.jar?raw=true and isntall it manually from disk.

Since my docker is running inside linux which is running as virtual machine inside VirtualBox, I had to share all drives where my Java code is on (C: drive is usually needed, since java SDK and maven repositories are there).

   

Or, if you are using Vagrant, this is how you share your C: drive as /c:

Vagrant.configure("2") do |config|
  config.vm.box = "ubuntu/trusty64"
  config.vm.network "private_network", ip: "192.168.50.50"
  config.vm.synced_folder "C:\\", "/c"
  config.vm.provider "virtualbox" do |v|
    v.memory = 2048
    v.cpus = 2
  end
end

On the docker side, you will need to pull my image (bojanv55/rds), and since I’m using minikube (Kubernetes) in my demonstration, I will show pod configuration for this specific case (inside file named rds.yaml):

apiVersion: v1
kind: Pod
metadata:
  name: remotedbg
  labels:
    app: remotedbg
spec:
  hostNetwork: true
  volumes:
  - name: drive-c
    hostPath:
      path: /media/sf_C_DRIVE
  - name: drive-d
    hostPath:
      path: /media/sf_D_DRIVE
  containers:
  - image: bojanv55/rds
    name: remotedbg
    volumeMounts:
    - name: drive-c
      mountPath: /c
    - name: drive-d
      mountPath: /d
    ports:
    - containerPort: 55004
    - containerPort: 55005
    - containerPort: 55006

Here we are mounting C: and D: drives shared inside VirtualBox to docker image, so we will have full access to host disk (where java source is). It is important that those drives are mounted as “/c”, “/d” … “/z” folders inside docker. Container ports are used for communication with my server (55004) and (55005-55015) for communication with java remote debug server (multiple ports in order to support multiple parallel process debugging).

Now we can start this pod:

kubectl create -f rds.yaml

And if we list all the pods, we will now see our remote server running:

kubectl get pods

NAME                                READY     STATUS    RESTARTS   AGE
remotedbg                           1/1       Running   0          28m

Or, if you don’t use minikube, you can run docker image inside VM with this simple command:

docker run -d -v /c:/c -p 55004:55004 -p 55005:55005 -p 55006:55006 bojanv55/rds

Now we are ready to setup our project for remote debugging. I just created new project that outputs “HI RDS”, and run it locally (this is needed in order to create local Application run configuration that will be used for remote debugging also).

Then we can go to “Edit Configurations” window, and setup our “Remote Debug” configuration:

As you can see, we are using local Application configuration as base for our remote config (this is where we pickup working directory, all needed libraries, etc…). Host is address of my Linux running inside VirtualBox in this case (since we are using “hostNetwork: true” inside kubernetes configuration, docker image will also have access to this IP).

Finally, we can run this Remote debug session:

As you can see we have our app running on remote system with 1 click of a button. If we need to add new code, and test again, all we need to do is add code to Intellij and hit that debug button inside IDEA. Much easier… And maybe someone will find it useful…

In case that you want to modify project, code is on github:

Server part: https://github.com/bojanv55/remote-debug-server
Plugin: https://github.com/bojanv55/remote-debug

One comment

Leave a comment