Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Debugging Tomcat in Docker container

I have a CoreOS running in Vagrant. Vagrant private network IP is 192.168.111.1. Inside a CoreOS is a docker container with Tomcat 8.0.32. Pretty much everything works ok (app deployment etc.) just debugging does not. Tomcat is mapped to 8080 port and the JPDA port should be 8000.

Facts

Tomcat JPDA is configured with:

JDPA_OPTS -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=8000

It starts with catalina.sh jpda start command. The output in the console when running it with docker-compose is:

tomcat | Listening for transport dt_socket at address: 8000

From the container info I assume that ports are mapped as they should:

CONTAINER ID        IMAGE       COMMAND      CREATED             STATUS              PORTS                                            NAMES
dcae1e0148f8        tomcat      "/run.sh"    8 minutes ago       Up 8 minutes        0.0.0.0:8000->8000/tcp, 0.0.0.0:8080->8080/tcp   tomcat

My docker image is based on this Dockerfile.

Problem

When trying to run Remote debug configuration (screenshot below) I get the error Error running Debug: Unable to open debugger port (192.168.111.1:8000): java.net.ConnectException "Connection refused". I've tried everything from changing various configuration but no luck. Am I missing something?

enter image description here

like image 383
CAPS LOCK Avatar asked Feb 23 '16 17:02

CAPS LOCK


3 Answers

This is the command I use for this:

docker run -it --rm \
  -e JPDA_ADDRESS=8000 \
  -e JPDA_TRANSPORT=dt_socket \
  -p 8888:8080 \
  -p 9000:8000 \
  -v D:/tc/conf/tomcat-users.xml:/usr/local/tomcat/conf/tomcat-users.xml \
  tomcat:8.0 \
  /usr/local/tomcat/bin/catalina.sh jpda run

Explanation

  • -e JPDA_ADDRESS=8000
    debugging port in container, passed as environment variable
  • -e JPDA_TRANSPORT=dt_socket
    transport type for debugging as socket, passed as environment variable
  • -p 8888:8080
    expose tomcat port 8080 on host as port 8888
  • -p 9000:8000
    expose java debugging port 8000 on host as port 9000
  • -v {host-file}:{container-file}
    overwrite tomcat-user.xml with my local on, since I need access to the manager api
    omit this line if this isn't necessary for your use case
  • tomcat:8.0
    see https://hub.docker.com/_/tomcat/
  • /usr/local/tomcat/bin/catalina.sh jpda run
    command to run in the container
like image 189
Peter Avatar answered Oct 04 '22 18:10

Peter


The accepted answer didn't work for me, apparently because I was using Java 11. It seems that if you're using Java 9 or newer, you need to specify the JPDA address like this:

JPDA_ADDRESS=*:8100
like image 12
Sebastian Heikamp Avatar answered Oct 04 '22 18:10

Sebastian Heikamp


You can always update the Dockerfile to something like the following: -

FROM tomcat:8-jre8
MAINTAINER me

ADD target/app.war /usr/local/tomcat/webapps/app.war

ENV JPDA_ADDRESS="8000"
ENV JPDA_TRANSPORT="dt_socket"

EXPOSE 8080 8000
ENTRYPOINT ["catalina.sh", "jpda", "run"]

This does mean though that your docker file has debug on by default which is probably not suited to a production environment.

like image 9
bobmarksie Avatar answered Oct 02 '22 18:10

bobmarksie