debug spring boot in vagrant and docker

2019-04-30 06:21发布

问题:

I'm trying to setup development environment for spring-boot project based on docker and vagrant. I'm using IntelliJ running on windows to write code and vagrant to be able to run project in docker containers on any system. I'm building project with maven. Currently I'm able to run application packaged in jar in docker container running in ubuntu on virtualbox via vagrant. I cannot figure it out how to debug my application in IntelliJ, I start application with remote debugging on, correctly (or at least I think I'm doing it right) configure port forwarding, but IntelliJ still tells me "Connection reset" and cannot connect to debugger.

Here is my Dockerfile:

FROM java:8
VOLUME /tmp
ADD test-1.0-SNAPSHOT.jar app.jar
ENTRYPOINT ["java", "-Djava.security.egd=file:/dev/./urandom", "-Xdebug -Xrunjdwp:server=y,transport=dt_socket,suspend=n,address=8000", "-jar", "/app.jar"]

Vagrantfile:

ENV['VAGRANT_DEFAULT_PROVIDER'] = 'docker'

Vagrant.configure("2") do |config|
  config.vm.define "app" do |a|
    a.vm.provider "docker" do |d|
      d.vagrant_machine = "dockerhost"
      d.vagrant_vagrantfile = "./Vagrant_docker_wrapper"
      d.build_dir = "."
      d.build_args = ["-t=app"]
      d.ports = ["8080:8080", "8000:8000"]

      d.name = "app"
      d.remains_running = true
      d.has_ssh = true
      d.cmd = ["run"]
    end
  end
end

And Vagrant_docker_wrapper:

Vagrant.configure("2") do |config|

  config.vm.provision "docker"

  config.vm.provision "shell", inline: "ps aux | grep 'sshd:' | awk '{print $2}' | xargs kill"

  config.vm.define "dockerhost"
  config.vm.box = "ubuntu/trusty64"
  config.vm.network "forwarded_port", guest: 8080, host: 9080
  config.vm.network "forwarded_port", guest: 8000, host: 9081

  config.vm.provider :virtualbox do |vb|
      vb.name = "dockerhost"
  end

end

I created this files with help of http://blog.zenika.com/2014/10/07/Setting-up-a-development-environment-using-Docker-and-Vagrant/ and https://spring.io/guides/gs/spring-boot-docker/

In IntelliJ I added new remote debug configuration and set port to 9081. If anyone has any ideas how I should configure this environment to work in debug mode I will be glad for any help.

If I manage to connect with debugger to my app, I also want to have some hot swap and static resources reload functionallity without need to recompile with maven and restart docker. So any help in this area also will be great but it isn't must to have.

回答1:

Ok, I found my error, in vagrant file I has only port 8080 exposed from docker container. I has also two options -Xdebug -Xrunjdwp defined as single option in docker entrypoint. Correct Docker file looks like this:

FROM java:8
VOLUME /tmp
ADD simple-test-1.0-SNAPSHOT.jar app.jar
ENTRYPOINT ["java", "-Djava.security.egd=file:/dev/./urandom", "-Xdebug", "-Xrunjdwp:server=y,transport=dt_socket,address=8000,suspend=n", "-jar", "/app.jar"]

And correct vagrant file:

ENV['VAGRANT_DEFAULT_PROVIDER'] = 'docker'

Vagrant.configure("2") do |config|
  config.vm.define "app" do |a|
    a.vm.provider "docker" do |d|
      d.vagrant_machine = "dockerhost"
      d.vagrant_vagrantfile = "./Vagrant_docker_wrapper"
      d.build_dir = "."
      d.build_args = ["-t=app"]
      d.ports = ["8080:8080", "8000:8000"]
      d.name = "app"
      d.remains_running = true
      d.cmd = ["run"]
    end
  end
end

Also, when I'm now connected via debugger to the server, I can hot swap java classes without problems (I didn't check static resources yet).