diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index e651b52ca..1d265d619 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -91,7 +91,7 @@ jobs: - name: Run the benchmark continue-on-error: true run: | - docker run --add-host host.docker.internal:host-gateway -p 5678:5678 -p 7890:7890 -i isucari-benchmarker /opt/go/benchmarker -target-url http://host.docker.internal -data-dir /initial-data -static-dir /static -payment-url http://host.docker.internal:5678 -payment-port 5678 -shipment-url http://host.docker.internal:7890 -shipment-port 7890 || echo "BENCHMARK_FAILED=true" >> $GITHUB_ENV + docker container run --add-host host.docker.internal:host-gateway -p 5678:5678 -p 7890:7890 -i isucari-benchmarker /bin/benchmarker -target-url http://host.docker.internal -data-dir /initial-data -static-dir /static -payment-url http://host.docker.internal:5678 -payment-port 5678 -shipment-url http://host.docker.internal:7890 -shipment-port 7890 || echo "BENCHMARK_FAILED=true" >> $GITHUB_ENV - name: Show logs run: | diff --git a/README.md b/README.md index 6ebcd8452..d8aacc95c 100644 --- a/README.md +++ b/README.md @@ -251,7 +251,7 @@ docker compose up docker build -t isucari-benchmarker -f bench/Dockerfile . # benchmarkerの実行(Linuxは --add-host host.docker.internal:host-gateway を追加) -docker run -p 5678:5678 -p 7890:7890 -i isucari-benchmarker /opt/go/benchmarker -target-url http://host.docker.internal -data-dir /initial-data -static-dir /static -payment-url http://host.docker.internal:5678 -payment-port 5678 -shipment-url http://host.docker.internal:7890 -shipment-port 7890 +docker container run -p 5678:5678 -p 7890:7890 -i isucari-benchmarker /bin/benchmarker -target-url http://host.docker.internal -data-dir /initial-data -static-dir /static -payment-url http://host.docker.internal:5678 -payment-port 5678 -shipment-url http://host.docker.internal:7890 -shipment-port 7890 ``` ### external service diff --git a/bench/Dockerfile b/bench/Dockerfile index 5ff4f1c34..64675aca4 100644 --- a/bench/Dockerfile +++ b/bench/Dockerfile @@ -1,18 +1,46 @@ -FROM golang:1.22 +# syntax=docker/dockerfile:1 -RUN mkdir -p /opt/go -WORKDIR /opt/go +FROM --platform=$BUILDPLATFORM golang:1.22 AS build +WORKDIR /src + +RUN --mount=type=cache,target=/go/pkg/mod/ \ + --mount=type=bind,source=go.sum,target=go.sum \ + --mount=type=bind,source=go.mod,target=go.mod \ + go mod download -x + +RUN --mount=type=cache,target=/go/pkg/mod/ \ + --mount=type=bind,target=. \ + CGO_ENABLED=0 go build -o /bin/benchmarker cmd/bench/main.go + + +FROM alpine:latest AS final + +RUN --mount=type=cache,target=/var/cache/apk \ + apk --update add \ + ca-certificates \ + tzdata \ + && \ + update-ca-certificates + +# Create a non-privileged user that the app will run under. +# See https://docs.docker.com/go/dockerfile-user-best-practices/ +ARG UID=10001 +RUN adduser \ + --disabled-password \ + --gecos "" \ + --home "/nonexistent" \ + --shell "/sbin/nologin" \ + --no-create-home \ + --uid "${UID}" \ + appuser +USER appuser COPY initial-data /initial-data COPY webapp/public/static /static -COPY go.mod /opt/go/go.mod -COPY go.sum /opt/go/go.sum -RUN go mod download - -COPY cmd/ /opt/go/cmd -COPY bench/ /opt/go/bench +COPY bench/run.sh /run.sh -RUN go build -o benchmarker cmd/bench/main.go +# Copy the executable from the "build" stage. +COPY --from=build /bin/benchmarker /bin/ -ENTRYPOINT ["/opt/go/bench/run.sh"] +ENTRYPOINT ["/run.sh"] diff --git a/bench/Dockerfile-payment b/bench/Dockerfile-payment index d9f8b9a9e..cfebbef3b 100644 --- a/bench/Dockerfile-payment +++ b/bench/Dockerfile-payment @@ -1,15 +1,43 @@ -FROM golang:1.22 +# syntax=docker/dockerfile:1 -RUN mkdir -p /opt/go -WORKDIR /opt/go +FROM --platform=$BUILDPLATFORM golang:1.22 AS build +WORKDIR /src -COPY go.mod /opt/go/go.mod -COPY go.sum /opt/go/go.sum -RUN go mod download +RUN --mount=type=cache,target=/go/pkg/mod/ \ + --mount=type=bind,source=go.sum,target=go.sum \ + --mount=type=bind,source=go.mod,target=go.mod \ + go mod download -x -COPY cmd/ /opt/go/cmd -COPY bench/ /opt/go/bench +RUN --mount=type=cache,target=/go/pkg/mod/ \ + --mount=type=bind,target=. \ + CGO_ENABLED=0 go build -o /bin/server cmd/payment/main.go -RUN go build -o bin/payment cmd/payment/main.go +FROM alpine:latest AS final -CMD [ "/opt/go/bin/payment", "-port", "5556" ] +RUN --mount=type=cache,target=/var/cache/apk \ + apk --update add \ + ca-certificates \ + tzdata \ + && \ + update-ca-certificates + +# Create a non-privileged user that the app will run under. +# See https://docs.docker.com/go/dockerfile-user-best-practices/ +ARG UID=10001 +RUN adduser \ + --disabled-password \ + --gecos "" \ + --home "/nonexistent" \ + --shell "/sbin/nologin" \ + --no-create-home \ + --uid "${UID}" \ + appuser +USER appuser + +# Copy the executable from the "build" stage. +COPY --from=build /bin/server /bin/ + +EXPOSE 5556 + +# What the container should run when it is started. +ENTRYPOINT [ "/bin/server", "-port", "5556" ] diff --git a/bench/Dockerfile-shipment b/bench/Dockerfile-shipment index 7faf12554..a3669d066 100644 --- a/bench/Dockerfile-shipment +++ b/bench/Dockerfile-shipment @@ -1,17 +1,45 @@ -FROM golang:1.22 +# syntax=docker/dockerfile:1 -RUN mkdir -p /opt/go -WORKDIR /opt/go +FROM --platform=$BUILDPLATFORM golang:1.22 AS build +WORKDIR /src -COPY initial-data /initial-data +RUN --mount=type=cache,target=/go/pkg/mod/ \ + --mount=type=bind,source=go.sum,target=go.sum \ + --mount=type=bind,source=go.mod,target=go.mod \ + go mod download -x + +RUN --mount=type=cache,target=/go/pkg/mod/ \ + --mount=type=bind,target=. \ + CGO_ENABLED=0 go build -o /bin/server cmd/shipment/main.go + +FROM alpine:latest AS final -COPY go.mod /opt/go/go.mod -COPY go.sum /opt/go/go.sum -RUN go mod download +RUN --mount=type=cache,target=/var/cache/apk \ + apk --update add \ + ca-certificates \ + tzdata \ + && \ + update-ca-certificates + +# Create a non-privileged user that the app will run under. +# See https://docs.docker.com/go/dockerfile-user-best-practices/ +ARG UID=10001 +RUN adduser \ + --disabled-password \ + --gecos "" \ + --home "/nonexistent" \ + --shell "/sbin/nologin" \ + --no-create-home \ + --uid "${UID}" \ + appuser +USER appuser + +COPY initial-data /initial-data -COPY cmd/ /opt/go/cmd -COPY bench/ /opt/go/bench +# Copy the executable from the "build" stage. +COPY --from=build /bin/server /bin/ -RUN go build -o bin/shipment cmd/shipment/main.go +EXPOSE 7002 -CMD [ "/opt/go/bin/shipment", "-data-dir", "/initial-data", "-port", "7002" ] +# What the container should run when it is started. +ENTRYPOINT [ "/bin/server", "-data-dir", "/initial-data", "-port", "7002" ] diff --git a/bench/run.sh b/bench/run.sh index 5fc44481d..214eb4c77 100755 --- a/bench/run.sh +++ b/bench/run.sh @@ -1,3 +1,3 @@ -#!/bin/bash +#!/bin/sh exec "$@" diff --git a/compose.yml b/compose.yml index e88193c58..42d1a1415 100644 --- a/compose.yml +++ b/compose.yml @@ -4,6 +4,7 @@ services: build: context: . dockerfile: bench/Dockerfile-payment + target: final ports: - "5556:5556" @@ -11,5 +12,6 @@ services: build: context: . dockerfile: bench/Dockerfile-shipment + target: final ports: - "7002:7002"