From 48314f5d181fae7cdab6ec2994dab03290eb3218 Mon Sep 17 00:00:00 2001 From: Rhys Lloyd Date: Wed, 6 Aug 2025 05:49:30 +0000 Subject: [PATCH] Refactor Docker (#254) Refactor docker to use makefile build commands Reviewed-on: https://git.itzana.me/StrafesNET/maps-service/pulls/254 Co-authored-by: Rhys Lloyd Co-committed-by: Rhys Lloyd --- {validation/.cargo => .cargo}/config.toml | 0 .drone.yml | 109 +++++++++++----------- .gitignore | 1 + Containerfile | 33 ------- Dockerfile | 3 + Makefile | 46 +++++++-- compose.yaml | 6 +- validation/Containerfile | 27 +----- 8 files changed, 101 insertions(+), 124 deletions(-) rename {validation/.cargo => .cargo}/config.toml (100%) delete mode 100644 Containerfile create mode 100644 Dockerfile diff --git a/validation/.cargo/config.toml b/.cargo/config.toml similarity index 100% rename from validation/.cargo/config.toml rename to .cargo/config.toml diff --git a/.drone.yml b/.drone.yml index 731074c..467d7eb 100644 --- a/.drone.yml +++ b/.drone.yml @@ -7,7 +7,43 @@ platform: arch: amd64 steps: - - name: api + - name: build-backend + image: golang:1.24.0 + environment: + GIT_USER: + from_secret: GIT_USER + GIT_PASS: + from_secret: GIT_PASS + commands: + - echo "machine git.itzana.me login $${GIT_USER} password $${GIT_PASS}" > ~/.netrc + - chmod 600 ~/.netrc + - make build-backend + when: + branch: + - master + - staging + + - name: build-validator + image: clux/muslrust:1.86.0-stable + commands: + - make build-validator + when: + branch: + - master + - staging + + - name: build-frontend + image: oven/bun:1.2.8 + commands: + - apt-get update + - apt-get install make + - make build-frontend + when: + branch: + - master + - staging + + - name: image-backend image: plugins/docker settings: registry: registry.itzana.me @@ -19,8 +55,10 @@ steps: from_secret: REGISTRY_USER password: from_secret: REGISTRY_PASS - dockerfile: Containerfile + dockerfile: Dockerfile context: . + depends_on: + - build-backend when: branch: - master @@ -28,7 +66,7 @@ steps: event: - push - - name: frontend + - name: image-frontend image: plugins/docker settings: registry: registry.itzana.me @@ -42,6 +80,8 @@ steps: from_secret: REGISTRY_PASS dockerfile: web/Containerfile context: web + depends_on: + - build-frontend when: branch: - master @@ -49,7 +89,7 @@ steps: event: - push - - name: validator + - name: image-validator image: plugins/docker settings: registry: registry.itzana.me @@ -63,6 +103,8 @@ steps: from_secret: REGISTRY_PASS dockerfile: validation/Containerfile context: validation + depends_on: + - build-validator when: branch: - master @@ -83,9 +125,9 @@ steps: PASSWORD: from_secret: ARGO_PASS depends_on: - - api - - frontend - - validator + - image-backend + - image-frontend + - image-validator when: branch: - master @@ -94,64 +136,19 @@ steps: - push # pr dry run - - name: api-pr - image: plugins/docker - settings: - registry: registry.itzana.me - repo: registry.itzana.me/strafesnet/maptest-validator - tags: - - ${DRONE_BRANCH}-${DRONE_BUILD_NUMBER} - - ${DRONE_BRANCH} - dockerfile: Containerfile - context: . - dry_run: true - when: - event: - - pull_request - - - name: frontend-pr - image: plugins/docker - settings: - registry: registry.itzana.me - repo: registry.itzana.me/strafesnet/maptest-validator - tags: - - ${DRONE_BRANCH}-${DRONE_BUILD_NUMBER} - - ${DRONE_BRANCH} - dockerfile: web/Containerfile - context: web - dry_run: true - when: - event: - - pull_request - - - name: validator-pr - image: plugins/docker - settings: - registry: registry.itzana.me - repo: registry.itzana.me/strafesnet/maptest-validator - tags: - - ${DRONE_BRANCH}-${DRONE_BUILD_NUMBER} - - ${DRONE_BRANCH} - dockerfile: validation/Containerfile - context: validation - dry_run: true - when: - event: - - pull_request - - name: build-pr image: alpine commands: - echo "Success!" depends_on: - - api-pr - - frontend-pr - - validator-pr + - build-backend + - build-validator + - build-frontend when: event: - pull_request --- kind: signature -hmac: 11e6d7f1eb839d3798fdcb642ca5523c011bd14c1f3a0343a9c3106bab9ef142 +hmac: 9880a1bb4725c81e38b5d185bbfccaf70ddf8021299557d1815f78e78817c5e6 ... diff --git a/.gitignore b/.gitignore index 6db043d..6fb4a5d 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ +build .idea /target diff --git a/Containerfile b/Containerfile deleted file mode 100644 index 92d7328..0000000 --- a/Containerfile +++ /dev/null @@ -1,33 +0,0 @@ -# Stage 1: Build -FROM registry.itzana.me/docker-proxy/golang:1.24 AS builder - -# Set the working directory in the container -WORKDIR /app - -# Copy go.mod and go.sum files -COPY go.mod go.sum ./ - -# Copy the entire project -COPY . . - -# Build the Go application -RUN CGO_ENABLED=0 GOOS=linux go build -o service ./cmd/maps-service/service.go - -# Stage 2: Run -FROM registry.itzana.me/docker-proxy/alpine:3.21 - -# Set up a non-root user for security -RUN addgroup -S appgroup && adduser -S appuser -G appgroup -USER appuser - -# Set the working directory in the container -WORKDIR /home/appuser - -# Copy the built application from the builder stage -COPY --from=builder /app/service . - -# Expose application port (adjust if needed) -EXPOSE 8081 - -# Command to run the application -ENTRYPOINT ["./service"] diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..4d10493 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,3 @@ +FROM alpine +COPY build/server / +ENTRYPOINT ["/server"] diff --git a/Makefile b/Makefile index ece4ecb..8ca1e28 100644 --- a/Makefile +++ b/Makefile @@ -1,12 +1,42 @@ -submissions: - DOCKER_BUILDKIT=1 docker build . -f Containerfile -t maps-service-submissions +clean: + rm -rf build + rm -rf web/build -web: - docker build web -f web/Containerfile -t maps-service-web +# build +build-backend: + CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o build/server cmd/maps-service/service.go -validation: - docker build validation -f validation/Containerfile -t maps-service-validation +build-validator: + cargo build --release --target x86_64-unknown-linux-musl --bin maps-validation -all: submissions web validation +build-frontend: + rm -rf web/build + cd web && bun install --frozen-lockfile + cd web && bun run build -.PHONY: submissions web validation +build: build-backend build-validator build-frontend + +# image +image-backend: + docker build . -t maptest-api + +image-validator: + docker build . -f validation/Containerfile -t maptest-validator + +image-frontend: + docker build web -f web/Containerfile -t maptest-frontend + +# docker +docker-backend: + make build-backend + make image-backend +docker-validator: + make build-validator + make image-validator +docker-frontend: + make build-frontend + make image-frontend + +docker: docker-backend docker-validator docker-frontend + +.PHONY: clean build-backend build-validator build-frontend build image-backend image-validator image-frontend docker-backend docker-validator docker-frontend docker diff --git a/compose.yaml b/compose.yaml index a1b9fc2..3f149e2 100644 --- a/compose.yaml +++ b/compose.yaml @@ -13,7 +13,7 @@ services: submissions: image: - maps-service-submissions + maptest-api container_name: submissions command: [ # debug @@ -45,7 +45,7 @@ services: web: image: - maps-service-web + maptest-frontend networks: - maps-service-network ports: @@ -56,7 +56,7 @@ services: validation: image: - maps-service-validation + maptest-validator container_name: validation env_file: - ../auth-compose/strafesnet_staging.env diff --git a/validation/Containerfile b/validation/Containerfile index 43d3ca7..fcebf00 100644 --- a/validation/Containerfile +++ b/validation/Containerfile @@ -1,24 +1,3 @@ -# Using the `rust-musl-builder` as base image, instead of -# the official Rust toolchain -FROM registry.itzana.me/docker-proxy/clux/muslrust:1.86.0-stable AS chef -USER root -RUN cargo install cargo-chef -WORKDIR /app - -FROM chef AS planner -COPY . . -RUN cargo chef prepare --recipe-path recipe.json - -FROM chef AS builder -COPY --from=planner /app/recipe.json recipe.json -COPY api ./api -# Notice that we are specifying the --target flag! -RUN cargo chef cook --release --target x86_64-unknown-linux-musl --recipe-path recipe.json -COPY . . -RUN cargo build --release --target x86_64-unknown-linux-musl --bin maps-validation - -FROM registry.itzana.me/docker-proxy/alpine:3.21 AS runtime -RUN addgroup -S myuser && adduser -S myuser -G myuser -COPY --from=builder /app/target/x86_64-unknown-linux-musl/release/maps-validation /usr/local/bin/ -USER myuser -ENTRYPOINT ["/usr/local/bin/maps-validation"] +FROM alpine:3.21 AS runtime +COPY /target/x86_64-unknown-linux-musl/release/maps-validation / +ENTRYPOINT ["/maps-validation"]