This documentation provides an in-depth look at how Docker is utilized within the development environment of the open-telemetry/opentelemetry-demo
project. The focus is on the setup and use of Docker during the development phase, excluding any production-related aspects.
Dockerfile
The Dockerfile is critical for building the project using Docker. Below is an explanation of each part of the Dockerfile found in the repository.
FROM --platform=${BUILDPLATFORM} gradle:8-jdk17 AS builder
WORKDIR /usr/src/app/
COPY ./src/frauddetectionservice/ ./
COPY ./pb/ ./src/main/proto/
RUN gradle shadowJar
Base Image: The Dockerfile begins with specifying a base image. Here, the Gradle image is being used, which has Java 17 installed. This image serves as the build environment.
Working Directory: The
WORKDIR
instruction sets the working directory inside the container to/usr/src/app/
. All subsequent commands will run from this directory.Copying Files: The
COPY
instructions replicate the necessary source files into the working directory. The contents ofsrc/frauddetectionservice/
and protocol buffers located inpb/
are copied into the appropriate paths.Building the Application: The
RUN gradle shadowJar
command invokes Gradle to build the application and produce a shadow JAR file. This is crucial for combining all dependencies into a single executable JAR.
The second stage of the Dockerfile is as follows:
FROM gcr.io/distroless/java17-debian11
ARG OTEL_JAVA_AGENT_VERSION
WORKDIR /usr/src/app/
COPY --from=builder /usr/src/app/build/libs/frauddetectionservice-1.0-all.jar ./
ADD --chmod=644 https://github.com/open-telemetry/opentelemetry-java-instrumentation/releases/download/v$OTEL_JAVA_AGENT_VERSION/opentelemetry-javaagent.jar /app/opentelemetry-javaagent.jar
ENV JAVA_TOOL_OPTIONS=-javaagent:/app/opentelemetry-javaagent.jar
ENTRYPOINT [ "java", "-jar", "frauddetectionservice-1.0-all.jar" ]
Distroless Image: The second stage uses a distroless image optimized for Java applications. This results in a smaller container size while eliminating unnecessary operating system components.
Arguments: The
ARG OTEL_JAVA_AGENT_VERSION
line allows for passing in the version of the OpenTelemetry Java agent during the build process.Copying the JAR: The built shadow JAR file created in the previous stage is copied into this stage for runtime.
Adding OpenTelemetry Java Agent: The
ADD
command fetches the OpenTelemetry Java agent from the specified URL and places it in the container. The--chmod=644
flag sets the permissions for the file.Setting Java Options: The
ENV JAVA_TOOL_OPTIONS
sets environment variables for Java, instructing it to use the OpenTelemetry agent when launching the application.Entry Point: Finally, the
ENTRYPOINT
command specifies how the container will be executed. It instructs Docker to runjava -jar frauddetectionservice-1.0-all.jar
.
Makefile
A Makefile is used to facilitate various development tasks, including building and managing the Docker images. Below is an excerpt regarding its usage:
The specific contents of the Makefile are not provided here, but it typically contains targets for building the Docker containers, managing dependencies, and executing tests.
The Makefile can include commands like the following:
build:
docker build -t frauddetectionservice .
Here, the build
target utilizes docker build
to create an image called frauddetectionservice
using the current directory’s Dockerfile.
By using the above definitions, developers can effectively utilize Docker in their development environment, managing dependencies, and compiling code in an isolated setting.
Sources: The code definitions and configuration details have been extracted directly from the project repository.