Dmitry Krychylskyy
Dmitry Krychylskyy

Reputation: 197

IntelliJ debug Java application in Docker

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"

Upvotes: 4

Views: 5808

Answers (1)

Looki
Looki

Reputation: 952

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/

Upvotes: 8

Related Questions