From 86becd0c239321ddfa3e4ae921b32ccdd3f3d9a5 Mon Sep 17 00:00:00 2001 From: Charles Madjeri Date: Sun, 8 Dec 2024 15:52:31 +0100 Subject: [PATCH] feat(docker): add prod and dev mode for server with dev hot reload Signed-off-by: Charles Madjeri --- client_mobile/Dockerfile | 31 ++++++++++++++++++++++++++----- docker-compose.override.yml | 12 +++++++++++- docker-compose.prod.yml | 8 +++++++- server/.air.toml | 26 ++++++++++++++++++++++++++ server/.gitignore | 2 ++ server/Dockerfile | 37 ++++++++++++++++++++++++++++++++----- 6 files changed, 104 insertions(+), 12 deletions(-) create mode 100644 server/.air.toml diff --git a/client_mobile/Dockerfile b/client_mobile/Dockerfile index 824acdd..4898004 100644 --- a/client_mobile/Dockerfile +++ b/client_mobile/Dockerfile @@ -2,15 +2,28 @@ FROM ghcr.io/cirruslabs/flutter:3.24.5 AS builder WORKDIR /app -RUN git config --system --add safe.directory /sdks/flutter && \ - git config --system --add safe.directory /app && \ - chmod -R 777 /sdks/flutter +# Create a non-root user and give permissions to Android SDK directory +RUN useradd -m -d /home/flutteruser -s /bin/bash flutteruser && \ + chown -R flutteruser:flutteruser /app && \ + chown -R flutteruser:flutteruser /sdks/flutter && \ + chown -R flutteruser:flutteruser /opt/android-sdk-linux && \ + mkdir -p /opt/android-sdk-linux/licenses && \ + chown -R flutteruser:flutteruser /opt/android-sdk-linux/licenses -COPY pubspec.* ./ +USER flutteruser + +RUN git config --global --add safe.directory /sdks/flutter && \ + git config --global --add safe.directory /app + +# Ensure Flutter is properly set up +RUN flutter doctor -v && \ + flutter config --no-analytics + +COPY --chown=flutteruser:flutteruser pubspec.* ./ RUN flutter pub get -COPY . . +COPY --chown=flutteruser:flutteruser . . ARG API_URL ARG WEB_CLIENT_URL @@ -18,5 +31,13 @@ ARG MOBILE_CLIENT_URL ARG GITHUB_CLIENT_ID ARG GITHUB_CLIENT_SECRET +# Accept Android licenses and install SDK components +RUN yes | sdkmanager --licenses && \ + sdkmanager "build-tools;30.0.3" + +# Clean and get dependencies again after copying all files +RUN flutter clean && \ + flutter pub get + RUN flutter build apk --release RUN mv build/app/outputs/flutter-apk/app-release.apk build/app/outputs/flutter-apk/client.apk \ No newline at end of file diff --git a/docker-compose.override.yml b/docker-compose.override.yml index cda3e7e..ad43493 100644 --- a/docker-compose.override.yml +++ b/docker-compose.override.yml @@ -10,5 +10,15 @@ services: - NODE_ENV=development - CHOKIDAR_USEPOLLING=true + area-server: + build: + target: development + volumes: + - ./server:/app + - go-modules:/go/pkg/mod + environment: + - GO_ENV=development + volumes: - ssl-certs: \ No newline at end of file + ssl-certs: + go-modules: \ No newline at end of file diff --git a/docker-compose.prod.yml b/docker-compose.prod.yml index 04315d6..849844e 100644 --- a/docker-compose.prod.yml +++ b/docker-compose.prod.yml @@ -3,4 +3,10 @@ services: build: target: production environment: - - NODE_ENV=production \ No newline at end of file + - NODE_ENV=production + + area-server: + build: + target: production + environment: + - GO_ENV=production diff --git a/server/.air.toml b/server/.air.toml new file mode 100644 index 0000000..40a26d8 --- /dev/null +++ b/server/.air.toml @@ -0,0 +1,26 @@ +root = "." +tmp_dir = "tmp" + +[build] +cmd = "go build -o ./tmp/main ." +bin = "./tmp/main" +full_bin = "./tmp/main" +include_ext = ["go", "tpl", "tmpl", "html"] +exclude_dir = ["assets", "tmp", "vendor"] +include_dir = [] +exclude_file = [] +delay = 1000 +stop_on_error = true +log = "air_errors.log" + +[log] +time = true + +[color] +main = "magenta" +watcher = "cyan" +build = "yellow" +runner = "green" + +[misc] +clean_on_exit = true \ No newline at end of file diff --git a/server/.gitignore b/server/.gitignore index 796fa7f..ad909a5 100644 --- a/server/.gitignore +++ b/server/.gitignore @@ -1,2 +1,4 @@ .env .env.server +tmp/ +air_errors.log diff --git a/server/Dockerfile b/server/Dockerfile index dbe5521..fbe1802 100644 --- a/server/Dockerfile +++ b/server/Dockerfile @@ -1,17 +1,44 @@ -FROM golang:1.23.4-alpine3.20 AS builder +# Development stage +FROM golang:1.23.4-alpine AS development -RUN apk add --no-cache gcc musl-dev +WORKDIR /app + +# Install air for hot reload +RUN go install github.com/air-verse/air@latest + +# Install dependencies +COPY go.mod go.sum ./ +RUN go mod download + +# Copy the rest of the code +COPY . . + +EXPOSE 8080 + +# Run with air for hot reload +CMD ["air", "-c", ".air.toml"] + +# Production stage +FROM golang:1.23.4-alpine AS production WORKDIR /app +# Copy go mod and sum files COPY go.mod go.sum ./ +# Download all dependencies RUN go mod download +# Copy the source code COPY . . -RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o main . +# Build the application +RUN CGO_ENABLED=0 GOOS=linux go build -o main . + +# Run the application +CMD ["./main"] +# Final stage FROM alpine:3.20.3 RUN apk add --no-cache ca-certificates && \ @@ -19,8 +46,8 @@ RUN apk add --no-cache ca-certificates && \ WORKDIR /app -COPY --from=builder /app/main . -COPY --from=builder /app/config.json . +COPY --from=production /app/main . +COPY --from=production /app/config.json . COPY .env.server .env RUN chown -R appuser:appuser /app && \