|
| 1 | +--- |
| 2 | +title: Setup App and API Protection for Java in Docker |
| 3 | +code_lang: docker |
| 4 | +type: multi-code-lang |
| 5 | +code_lang_weight: 10 |
| 6 | +further_reading: |
| 7 | +- link: "/security/application_security/how-it-works/" |
| 8 | + tag: "Documentation" |
| 9 | + text: "How App and API Protection Works" |
| 10 | +- link: "/security/default_rules/?category=cat-application-security" |
| 11 | + tag: "Documentation" |
| 12 | + text: "OOTB App and API Protection Rules" |
| 13 | +- link: "/security/application_security/troubleshooting" |
| 14 | + tag: "Documentation" |
| 15 | + text: "Troubleshooting App and API Protection" |
| 16 | +--- |
| 17 | + |
| 18 | +{{< partial name="api_security/callout.html" >}} |
| 19 | + |
| 20 | +{{< partial name="api_security/java/overview.html" >}} |
| 21 | + |
| 22 | +This guide explains how to set up App and API Protection (AAP) for Java applications running in Docker containers. The setup involves: |
| 23 | +1. Installing the Datadog Agent |
| 24 | +2. Configuring your Java application container |
| 25 | +3. Enabling AAP monitoring |
| 26 | + |
| 27 | +## Prerequisites |
| 28 | + |
| 29 | +- Docker installed on your host |
| 30 | +- Java application containerized with Docker |
| 31 | +- Datadog Agent installed on the host or as a container |
| 32 | + |
| 33 | +# Setup |
| 34 | + |
| 35 | +## 1. Install and run the Datadog Agent |
| 36 | + |
| 37 | +If you haven't already, install the Datadog Agent on your host or as a container. For containerized installation: |
| 38 | + |
| 39 | +```bash |
| 40 | +docker run -d --name datadog-agent \ |
| 41 | + -v /var/run/docker.sock:/var/run/docker.sock:ro \ |
| 42 | + -v /proc/:/host/proc/:ro \ |
| 43 | + -v /sys/fs/cgroup/:/host/sys/fs/cgroup:ro \ |
| 44 | + -e DD_API_KEY=<YOUR_API_KEY> \ |
| 45 | + -e DD_APM_ENABLED=true \ |
| 46 | + -e DD_APM_NON_LOCAL_TRAFFIC=true \ |
| 47 | + datadog/agent:latest |
| 48 | +``` |
| 49 | + |
| 50 | +### Library setup |
| 51 | + |
| 52 | +To enable AAP capabilities, you need the Datadog Java tracing library (version 0.94.0 or higher) installed in your application environment. Normally, this is done with the run command above, however, if it does not, you can download the agent manually. |
| 53 | + |
| 54 | +#### Download the library |
| 55 | + |
| 56 | +Download the latest version of the Datadog Java library: |
| 57 | + |
| 58 | +```dockerfile |
| 59 | +ADD 'https://dtdg.co/latest-java-tracer' /dd-java-agent.jar |
| 60 | +``` |
| 61 | + |
| 62 | +#### Verify compatibility |
| 63 | + |
| 64 | +To check that your service's language and framework versions are supported for AAP capabilities, see [Single Step Instrumentation Compatibility][2]. |
| 65 | + |
| 66 | +## Service configuration |
| 67 | + |
| 68 | +### Run your application with AAP enabled |
| 69 | +{{% tabs %}} |
| 70 | + {{% tab "APM Tracing Enabled" %}} |
| 71 | + |
| 72 | +Start your Java application with the Datadog agent and AAP enabled: |
| 73 | + |
| 74 | +```dockerfile |
| 75 | +ENTRYPOINT ["java", "-javaagent:/dd-java-agent.jar", "-Ddd.appsec.enabled=true", "-Ddd.service=<MY_SERVICE>", "-Ddd.env=<MY_ENV>", "-jar", "/app.jar"] |
| 76 | +``` |
| 77 | + {{% /tab %}} |
| 78 | + {{% tab "APM Tracing Disabled (Standalone Billing)" %}} |
| 79 | + |
| 80 | +If you want to use Application Security Management without APM tracing functionality, you can deploy with [Standalone App and API Protection][2]. This configuration reduces the amount of APM data sent to Datadog to the minimum required by App and API Protection products. |
| 81 | + |
| 82 | +To enable standalone mode: |
| 83 | +1. Set `DD_APM_TRACING_ENABLED=false` environment variable |
| 84 | +2. Keep `DD_APPSEC_ENABLED=true` environment variable |
| 85 | +3. This configuration will minimize APM data while maintaining full security monitoring capabilities |
| 86 | + |
| 87 | +```dockerfile |
| 88 | +ENTRYPOINT ["java", "-javaagent:/dd-java-agent.jar", "-Ddd.appsec.enabled=true", "-Ddd.apm.tracing.enabled=false", "-Ddd.service=<MY_SERVICE>", "-Ddd.env=<MY_ENV>", "-jar", "/app.jar"] |
| 89 | +``` |
| 90 | + |
| 91 | +**Important considerations:** |
| 92 | +- **File system requirements**: Read-only file systems are not currently supported. The application must have access to a writable `/tmp` directory. |
| 93 | +- **Service identification**: Always specify `DD_SERVICE` (or `-Ddd.service`) and `DD_ENV` (or `-Ddd.env`) for proper service identification in Datadog. |
| 94 | + {{% /tab %}} |
| 95 | +{{% /tabs %}} |
| 96 | + |
| 97 | +## 2. Configure your Java application container |
| 98 | + |
| 99 | +Add the following to your Dockerfile: |
| 100 | + |
| 101 | +```dockerfile |
| 102 | +# Download the Datadog Java agent |
| 103 | +ADD 'https://dtdg.co/latest-java-tracer' /dd-java-agent.jar |
| 104 | + |
| 105 | +# Set environment variables |
| 106 | +ENV DD_APPSEC_ENABLED=true |
| 107 | +ENV DD_SERVICE=<YOUR_SERVICE_NAME> |
| 108 | +ENV DD_ENV=<YOUR_ENVIRONMENT> |
| 109 | + |
| 110 | +# Add the Java agent to your application's startup command |
| 111 | +ENTRYPOINT ["java", "-javaagent:/dd-java-agent.jar", "-jar", "/app.jar"] |
| 112 | +``` |
| 113 | + |
| 114 | +## 3. Run your container |
| 115 | + |
| 116 | +When running your container, make sure to: |
| 117 | +1. Connect it to the same Docker network as the Datadog Agent |
| 118 | +2. Set the required environment variables |
| 119 | + |
| 120 | +```bash |
| 121 | +docker run -d \ |
| 122 | + --name your-java-app \ |
| 123 | + --network datadog-network \ |
| 124 | + -e DD_APPSEC_ENABLED=true \ |
| 125 | + -e DD_SERVICE=<YOUR_SERVICE_NAME> \ |
| 126 | + -e DD_ENV=<YOUR_ENVIRONMENT> \ |
| 127 | + your-java-app-image |
| 128 | +``` |
| 129 | + |
| 130 | +### Verify setup |
| 131 | + |
| 132 | +To verify that AAP is working correctly: |
| 133 | + |
| 134 | +1. Send some traffic to your application |
| 135 | +2. Check the [Application Signals Explorer][5] in Datadog |
| 136 | +3. Look for security signals and vulnerabilities |
| 137 | + |
| 138 | +## Troubleshooting |
| 139 | + |
| 140 | +If you encounter issues while setting up App and API Protection for your Java application, see the [Java App and API Protection troubleshooting guide][3]. |
| 141 | + |
| 142 | +## Further Reading |
| 143 | + |
| 144 | +{{< partial name="whats-next/whats-next.html" >}} |
| 145 | + |
| 146 | +[1]: https://docs.datadoghq.com/tracing/trace_collection/automatic_instrumentation/single-step-apm/?tab=docker |
| 147 | +[2]: /security/application_security/setup/environments/java/compatibility |
| 148 | +[3]: /security/application_security/setup/environments/java/troubleshooting |
| 149 | +[4]: /security/application_security/setup/java/standalone |
| 150 | +[5]: https://app.datadoghq.com/security/appsec |
0 commit comments