4

I'm new to this. Can you tell me how to set up a debugging Java program that runs in Docker?

The project is built with the help of Maven after which Docker uses *.war to run the program. As far as I understand it, IntelliJ must be pushed remotely for debugging.

container

docker-compose.yml :

# myapp (Wildfly + Keycloak + myapp)
  myapp-myapp:
    image: myapp/wildfly-myapp:wf11
    container_name: myapp-myapp
    depends_on:
      - myapp-postgres
      - myapp-mailhog
    environment:
      TZ : Europe/Paris
      KEYCLOAK_URL: http://localhost:8080/auth
      WILDFLY_PROXY_ADDRESS_FORWARDING: "true"
      JAVA_OPTS: 
      DB_CONNEXION_URL: jdbc:postgresql://myapp-postgres:5432/myapp_db
      DB_CONNEXION_URL_DELIMITER: "|"
      DB_HOST: myapp-postgres
      DB_NAME: ${DB_NAME}
      DB_PASSWORD: ${DB_PASSWORD}
      DB_USER: ${DB_USER}
      KEYCLOAK_DB_CONNEXION_URL: jdbc:postgresql://myapp-postgres:5432/keycloak
      KEYCLOAK_DB_CONNEXION_URL_DELIMITER: "|"
      KEYCLOAK_DB_USER : keycloak
      KEYCLOAK_DB_PASSWORD : keycloak
      KEYCLOAK_DB_NAME : keycloak
      KEYCLOAK_DB_HOST: postgres
      APP_CLUSTER: "false"
      JGROUPS_EXTERNAL_ADDRESS: 10.178.27.30
      JGROUPS_TCP_EXTERNAL_PORT: 7600
      JGROUPS_TCP_BIND_PORT: 7600
      JGROUPS_DEFAULT_STACK: tcpping
      JGROUPS_INITIAL_HOSTS: "10.178.27.30[7600]"
      WILDFLY_CLUSTER_PASSWORD:
      WILDFLY_NODE_NAME: myapp-master
      SMTP_HOST: myapp-mailhog
      SMTP_PORT: 1025
      SMTP_FROM: ${SMTP_FROM}
      SMTP_USERNAME: ""
      SMTP_PASSWORD: ""
      myapp_LOG_LEVEL: "ALL"
      myapp_DEBUG_LEVEL: "ALL"
    volumes:
       - ./myapp/data:/opt/jboss/myappdata
       - ./myapp/logs:/opt/jboss/wildfly/standalone/log
       - ./myapp/deployments:/opt/jboss/wildfly/standalone/deployments
       - ./myapp/configuration/myapp-admin.properties:/opt/jboss/wildfly/standalone/configuration/myapp-admin.properties
       - ${ST_SRC}:/opt/jboss/myappdata/${PROVIDER_CODE}/frontend/st
       - ${CC_SRC}:/opt/jboss/myappdata/${PROVIDER_CODE}/frontend/cc
       - ${SC_SRC}:/opt/jboss/myappdata/${PROVIDER_CODE}/frontend/sc
    ports:
      - "8080:8080"
      - "7600:7600"
3
  • Do you create the Dockerfile yourself or use a library? Commented Aug 7, 2019 at 13:31
  • 1
    This might be helpful Debug java application running in Docker from IntelliJ Commented Aug 7, 2019 at 13:32
  • Hmmm, you could add the Java Arg in my answer after JAVA_OPTS: and include the port 8000 under ports: Commented Aug 7, 2019 at 14:06

1 Answer 1

8

You can enable remote debugging with the following JVM-Argument

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

Then you can attach the Intellij Debugger with Run -> Attach to process Your application should have the port specified (8000 in this case)

See here for more information

For this example (Docker-Compose with Wildfly)

environment:
  ...
  JAVA_OPTS: -agentlib:jdwp=transport=dt_socket,server=y,address=8000,suspend=n
  ...
ports:
  - "8000:8000"
  ...

In General

Docker

For usage in Docker, you additionally have to expose the port

EXPOSE 8000
ENTRYPOINT ["java", "-jar", "-agentlib:jdwp=transport=dt_socket,server=y,address=8000,suspend=n", "/path/to/my/java.jar"]

and then

docker run -p 8000:8000 <image>

IntelliJ (starting 2019.1)

This procedure can now be done automatically by IntelliJ https://blog.jetbrains.com/idea/2019/04/debug-your-java-applications-in-docker-using-intellij-idea/

Sign up to request clarification or add additional context in comments.

6 Comments

I have this like a container : 46aa7c658b99 myapp/wildfly-myapp:wf11 "/opt/jboss/docker-e…" 22 hours ago Up 5 hours (healthy) 1883/tcp, 0.0.0.0:7600->7600/tcp, 4000/tcp, 9990/tcp, 0.0.0.0:8080->8080/tcp
How do you get your code from IntelliJ into the Docker Container? Did you write a Dockerfile? Do you use a Maven plugin?
and I use docker-compose up for run all containers
So are you using containers you didn't create yourself? If yes, which containers?
It would be very helpful, if you included all you steps, and the docker-compose.yml in the question post
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.