Skip to content

Commit

Permalink
feat(docker): add prod and dev mode for server with dev hot reload
Browse files Browse the repository at this point in the history
Signed-off-by: Charles Madjeri <[email protected]>
  • Loading branch information
Charles Madjeri committed Dec 8, 2024
1 parent e6852c9 commit 86becd0
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 12 deletions.
31 changes: 26 additions & 5 deletions client_mobile/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,42 @@ 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
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
12 changes: 11 additions & 1 deletion docker-compose.override.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
ssl-certs:
go-modules:
8 changes: 7 additions & 1 deletion docker-compose.prod.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,10 @@ services:
build:
target: production
environment:
- NODE_ENV=production
- NODE_ENV=production

area-server:
build:
target: production
environment:
- GO_ENV=production
26 changes: 26 additions & 0 deletions server/.air.toml
Original file line number Diff line number Diff line change
@@ -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
2 changes: 2 additions & 0 deletions server/.gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
.env
.env.server
tmp/
air_errors.log
37 changes: 32 additions & 5 deletions server/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,26 +1,53 @@
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 && \
adduser -D appuser

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 && \
Expand Down

0 comments on commit 86becd0

Please sign in to comment.