sbt / sbt-native-packager

sbt Native Packager
https://sbt-native-packager.readthedocs.io/en/stable/
BSD 2-Clause "Simplified" License
1.59k stars 439 forks source link

Adding files to docker image generated by AshScriptPlugin #1502

Closed mgralle closed 2 years ago

mgralle commented 2 years ago

Expected behaviour

An additional jar file (for automatic instrumentation) should be placed in the docker image. I have tried two ways:

  1. By adding files through dockerCommands:
    ...,
    Cmd("WORKDIR", "/opt/"),
    ExecCmd("RUN", "wget", "https://github.com/open-telemetry/opentelemetry-java-instrumentation/releases/latest/download/opentelemetry-javaagent.jar"),
    ...

Those of my targets that don't include AshScriptPlugin create docker images as indicated. However, those that do include AshScriptPlugin completely ignore the modified dockerCommands.

  1. As static alternative, I copied the downloaded opentelemetry-javaagent.jar directly into the folder <my-service>-impl/src/universal, since the documentation says "By default, all files found in the src/universal directory are included in the distribution". However, the file is again not included in the docker images generated with the AshScriptPlugin.

Actual behaviour

The docker image contains only the default AshScriptPlugin-generated content, not the opentelemetry-javaagent.jar.

Information

mgralle commented 2 years ago

I am aware that this may not be a bug, but an intended feature of the plugin. Please indicate any workarounds!

mgralle commented 2 years ago

I have found my mistake, it was about sbt scopes. The dockerCommands for the microservices that use AshScriptPlugin are not copied in from the other microservices, but created by AshScriptPlugin. So I had to edit the dockerCommands generated by AshScriptPlugin directly. FWIW, I did it this way:

lazy val otelCommands = Seq(
  Cmd("WORKDIR", "/opt/"),
  ExecCmd("RUN", "wget", "https://github.com/open-telemetry/opentelemetry-java-instrumentation/releases/latest/download/opentelemetry-javaagent.jar"),
  Cmd("ENV", "JAVA_TOOL_OPTIONS=-javaagent:/opt/opentelemetry-javaagent.jar"),
  Cmd("ENV", "OTEL_TRACES_EXPORTER=jaeger"),
  Cmd("ENV", "OTEL_EXPORTER_JAEGER_ENDPOINT=collector.linkerd-jaeger.svc.cluster.local:55678")
)
...
dockerCommands := {
    dockerCommands.value.flatMap {
      // insert instrumentation commands in mainstage right before losing root privileges
      case down@Cmd("USER", "daemon") => otelCommands :+ down
      case other => Seq(other)
    }
  }