Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[chore] multi platform builds #1651

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ the release.

* [tests] run trace based tests concurrently
([#1659](https://github.com/open-telemetry/opentelemetry-demo/pull/1659))
* [chore] multi-platform build support for all services
([#1651](https://github.com/open-telemetry/opentelemetry-demo/pull/1651))

## 1.11.0

Expand Down
48 changes: 48 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,54 @@ on each other), the owner should try to get people aligned by:
the owner should bring it to the OpenTelemetry Community Demo SIG
[meeting](README.md#contributing).

## Multi-platform Builds

Creating multi-platform builds requires docker buildx to be installed. This
is part of Docker Desktop for macOS, or can be installed using
`apt install docker-buildx` on Ubuntu.

You will need a multi-platform capable builder with a limiter set of parallelism
to avoid errors while building the images. On an M2 Mac, it is recommended to
limit the parallelism to 8. This can be done by specifying a configuration file
when creating the builder. First create the configuration file:

```shell
cat <<EOF > otel-builder.toml
[worker.oci]
max-parallelism = 8
EOF
```

Then create the builder for your environment.

If you are using **MacOS** use this command to create the builder:

```shell
docker buildx create --name otel-builder --bootstrap --use --driver docker-container --buildkitd-config ./otel-builder.toml
```

If you are using **Ubuntu** use this command to create the builder:

```shell
docker buildx create --name otel-builder --bootstrap --use --driver docker-container --config ./otel-builder.toml
```

A builder will be created and set as the active builder. You can check the
builder status with `docker buildx inspect`. To build multi-platform images for
linux/amd64 and linux/arm64, use the following command:

```shell
make build-multiplatform
```

To build and push multi-platform images to a registry, ensure to set
`IMAGE_NAME` to the name of the registry and image repository to use in the
`.env.override` file and run:

```shell
make build-multiplatform-and-push
```

## Making a new release

Maintainers can create a new release when desired by following these steps.
Expand Down
33 changes: 15 additions & 18 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -79,24 +79,21 @@ install-tools: $(MISSPELL)
build:
$(DOCKER_COMPOSE_CMD) build

.PHONY: build-and-push-dockerhub
build-and-push-dockerhub:
$(DOCKER_COMPOSE_CMD) --env-file .dockerhub.env -f docker-compose.yml build
$(DOCKER_COMPOSE_CMD) --env-file .dockerhub.env -f docker-compose.yml push

.PHONY: build-and-push-ghcr
build-and-push-ghcr:
$(DOCKER_COMPOSE_CMD) --env-file .ghcr.env -f docker-compose.yml build
$(DOCKER_COMPOSE_CMD) --env-file .ghcr.env -f docker-compose.yml push

.PHONY: build-env-file
build-env-file:
cp .env .dockerhub.env
sed -i '/IMAGE_VERSION=.*/c\IMAGE_VERSION=${RELEASE_VERSION}' .dockerhub.env
sed -i '/IMAGE_NAME=.*/c\IMAGE_NAME=${DOCKERHUB_REPO}' .dockerhub.env
cp .env .ghcr.env
sed -i '/IMAGE_VERSION=.*/c\IMAGE_VERSION=${RELEASE_VERSION}' .ghcr.env
sed -i '/IMAGE_NAME=.*/c\IMAGE_NAME=${GHCR_REPO}' .ghcr.env
.PHONY: build-and-push
build-and-push:
$(DOCKER_COMPOSE_CMD) $(DOCKER_COMPOSE_ENV) build --push

# Build and push multiplatform images (linux/amd64, linux/arm64) using buildx.
# Requires docker with buildx enabled and a multi-platform capable builder in use.
.PHONY: build-multiplatform
build-multiplatform:
# Because buildx bake does not support --env-file yet, we need to load it into the environment first.
set -a; . ./.env.override; set +a && docker buildx bake -f docker-compose.yml --load --set "*.platform=linux/amd64,linux/arm64"

.PHONY: build-multiplatform-and-push
build-multiplatform-and-push:
# Because buildx bake does not support --env-file yet, we need to load it into the environment first.
set -a; . ./.env.override; set +a && docker buildx bake -f docker-compose.yml --push --set "*.platform=linux/amd64,linux/arm64"

.PHONY: run-tests
run-tests:
Expand Down
46 changes: 20 additions & 26 deletions src/accountingservice/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,31 +1,25 @@
# Copyright The OpenTelemetry Authors
# SPDX-License-Identifier: Apache-2.0

FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS base
USER app
WORKDIR /app

FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build
ARG BUILD_CONFIGURATION=Release
WORKDIR /src
COPY ["/src/accountingservice/", "AccountingService/"]
COPY ["/pb/demo.proto", "AccountingService/proto/"]
RUN dotnet restore "./AccountingService/AccountingService.csproj"
WORKDIR "/src/AccountingService"

RUN dotnet build "./AccountingService.csproj" -c $BUILD_CONFIGURATION -o /app/build

FROM build AS publish
ARG BUILD_CONFIGURATION=Release
RUN dotnet publish "./AccountingService.csproj" --use-current-runtime -c $BUILD_CONFIGURATION -o /app/publish /p:UseAppHost=false

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .

USER root
RUN mkdir -p "/var/log/opentelemetry/dotnet"
RUN chown app "/var/log/opentelemetry/dotnet"
USER app
FROM --platform=${BUILDPLATFORM} mcr.microsoft.com/dotnet/sdk:8.0 AS builder
ARG TARGETARCH

WORKDIR /usr/src/app/

COPY ./src/accountingservice/ ./
COPY ./pb/ ./proto/

RUN dotnet restore "./AccountingService.csproj" -r linux-musl-$TARGETARCH

RUN dotnet publish "./AccountingService.csproj" -r linux-musl-$TARGETARCH --no-restore -o /accountingservice

# -----------------------------------------------------------------------------

FROM --platform=${TARGETPLATFORM} mcr.microsoft.com/dotnet/aspnet:8.0

WORKDIR /usr/src/app/
COPY --from=builder /accountingservice/ ./

ENV DOTNET_HOSTBUILDER__RELOADCONFIGONCHANGE=false

ENTRYPOINT ["./instrument.sh", "dotnet", "AccountingService.dll"]
4 changes: 2 additions & 2 deletions src/adservice/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# SPDX-License-Identifier: Apache-2.0


FROM eclipse-temurin:21-jdk as builder
FROM --platform=${BUILDPLATFORM} eclipse-temurin:21-jdk as builder

WORKDIR /usr/src/app/

Expand All @@ -18,7 +18,7 @@ RUN ./gradlew installDist -PprotoSourceDir=./proto

# -----------------------------------------------------------------------------

FROM eclipse-temurin:21-jre
FROM --platform=${TARGETPLATFORM} eclipse-temurin:21-jre

ARG version=2.3.0
WORKDIR /usr/src/app/
Expand Down
6 changes: 3 additions & 3 deletions src/cartservice/src/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ WORKDIR /usr/src/app/
COPY ./src/cartservice/ ./
COPY ./pb/ ./pb/

RUN dotnet restore ./src/cartservice.csproj -v d -r linux-musl-$TARGETARCH
RUN dotnet restore ./src/cartservice.csproj -r linux-musl-$TARGETARCH

RUN dotnet publish ./src/cartservice.csproj -v d -r linux-musl-$TARGETARCH --no-restore -o /cartservice
RUN dotnet publish ./src/cartservice.csproj -r linux-musl-$TARGETARCH --no-restore -o /cartservice

# -----------------------------------------------------------------------------

# https://mcr.microsoft.com/v2/dotnet/runtime-deps/tags/list
FROM mcr.microsoft.com/dotnet/runtime-deps:8.0.6-alpine3.20
FROM --platform=$TARGETPLATFORM mcr.microsoft.com/dotnet/runtime-deps:8.0.6-alpine3.20

WORKDIR /usr/src/app/
COPY --from=builder /cartservice/ ./
Expand Down
4 changes: 2 additions & 2 deletions src/frauddetectionservice/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# SPDX-License-Identifier: Apache-2.0


FROM gradle:8-jdk17 AS builder
FROM --platform=${BUILDPLATFORM} gradle:8-jdk17 AS builder

WORKDIR /usr/src/app/

Expand All @@ -12,7 +12,7 @@ RUN gradle shadowJar

# -----------------------------------------------------------------------------

FROM gcr.io/distroless/java17-debian11
FROM --platform=${TARGETPLATFORM} gcr.io/distroless/java17-debian11

ARG version=2.4.0
WORKDIR /usr/src/app/
Expand Down
10 changes: 10 additions & 0 deletions src/shippingservice/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ RUN if [ "${TARGETPLATFORM}" = "${BUILDPLATFORM}" ] ; then \
apt-get update && apt-get install --no-install-recommends -y g++-aarch64-linux-gnu libc6-dev-arm64-cross libprotobuf-dev protobuf-compiler ca-certificates && \
rustup target add aarch64-unknown-linux-gnu && \
rustup toolchain install stable-aarch64-unknown-linux-gnu; \
elif [ "${TARGETPLATFORM}" = "linux/amd64" ] ; then \
apt-get update && apt-get install --no-install-recommends -y g++-x86-64-linux-gnu libc6-amd64-cross libprotobuf-dev protobuf-compiler ca-certificates && \
rustup target add x86_64-unknown-linux-gnu && \
rustup toolchain install stable-x86_64-unknown-linux-gnu; \
else \
echo "${TARGETPLATFORM} is not supported"; \
exit 1; \
Expand All @@ -34,6 +38,12 @@ RUN if [ "${TARGETPLATFORM}" = "${BUILDPLATFORM}" ] ; then \
CXX_aarch64_unknown_linux_gnu=aarch64-linux-gnu-g++ \
cargo build -r --features="dockerproto" --target aarch64-unknown-linux-gnu && \
cp /app/target/aarch64-unknown-linux-gnu/release/shippingservice /app/target/release/shippingservice; \
elif [ "${TARGETPLATFORM}" = "linux/amd64" ] ; then \
env CARGO_TARGET_X86_64_UNKNOWN_LINUX_GNU_LINKER=x86_64-linux-gnu-gcc \
CC_x86_64_unknown_linux_gnu=x86_64-linux-gnu-gcc \
CXX_x86_64_unknown_linux_gnu=x86_64-linux-gnu-g++ \
cargo build -r --features="dockerproto" --target x86_64-unknown-linux-gnu && \
cp /app/target/x86_64-unknown-linux-gnu/release/shippingservice /app/target/release/shippingservice; \
else \
echo "${TARGETPLATFORM} is not supported"; \
exit 1; \
Expand Down
Loading