From 945df849f8a408f18538fbd211a12075d8a69d07 Mon Sep 17 00:00:00 2001 From: admin Date: Mon, 27 Apr 2026 10:41:48 +0800 Subject: [PATCH] Add Docker support: Dockerfile, docker-compose, GitHub Actions --- .github/workflows/docker.yml | 19 +++++-------------- backend/Dockerfile | 6 +++++- docker-compose.yml | 19 ++++--------------- frontend/Dockerfile | 19 ------------------- frontend/nginx.conf | 27 --------------------------- 5 files changed, 14 insertions(+), 76 deletions(-) delete mode 100644 frontend/Dockerfile delete mode 100644 frontend/nginx.conf diff --git a/.github/workflows/docker.yml b/.github/workflows/docker.yml index 867c788..0dd9948 100644 --- a/.github/workflows/docker.yml +++ b/.github/workflows/docker.yml @@ -1,4 +1,4 @@ -name: Build and Push Docker Images +name: Build and Push Docker Image on: push: @@ -40,22 +40,13 @@ jobs: type=sha,prefix= type=raw,value=latest,enable={{is_default_branch}} - - name: Build and push backend image + - name: Build and push image uses: docker/build-push-action@v5 with: - context: ./backend + context: . + file: ./backend/Dockerfile push: true - tags: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}-backend:${{ steps.meta.outputs.tags }} - labels: ${{ steps.meta.outputs.labels }} - cache-from: type=gha - cache-to: type=gha,mode=max - - - name: Build and push frontend image - uses: docker/build-push-action@v5 - with: - context: ./frontend - push: true - tags: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}-frontend:${{ steps.meta.outputs.tags }} + tags: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ steps.meta.outputs.tags }} labels: ${{ steps.meta.outputs.labels }} cache-from: type=gha cache-to: type=gha,mode=max diff --git a/backend/Dockerfile b/backend/Dockerfile index 22eebe2..97eb50e 100644 --- a/backend/Dockerfile +++ b/backend/Dockerfile @@ -2,13 +2,16 @@ FROM golang:1.21-alpine AS builder WORKDIR /app -RUN apk add --no-cache git +RUN apk add --no-cache git nodejs npm COPY go.mod go.sum ./ RUN go mod download COPY . . +RUN npm ci --prefix frontend +RUN npm run build --prefix frontend + RUN CGO_ENABLED=0 GOOS=linux go build -o /app/server ./cmd/server FROM alpine:latest @@ -18,6 +21,7 @@ WORKDIR /app RUN apk add --no-cache ca-certificates tzdata COPY --from=builder /app/server . +COPY --from=builder /app/frontend/dist ./static COPY --from=builder /app/uploads ./uploads EXPOSE 8080 diff --git a/docker-compose.yml b/docker-compose.yml index 425b415..aca9318 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,11 +1,11 @@ version: '3.8' services: - backend: + app: build: - context: ./backend - dockerfile: Dockerfile - container_name: sale-backend + context: . + dockerfile: backend/Dockerfile + container_name: sale-app ports: - "8080:8080" volumes: @@ -14,14 +14,3 @@ services: environment: - DATABASE_PATH=sale.db restart: unless-stopped - - frontend: - build: - context: ./frontend - dockerfile: Dockerfile - container_name: sale-frontend - ports: - - "80:80" - depends_on: - - backend - restart: unless-stopped diff --git a/frontend/Dockerfile b/frontend/Dockerfile deleted file mode 100644 index 1ff99f4..0000000 --- a/frontend/Dockerfile +++ /dev/null @@ -1,19 +0,0 @@ -FROM node:20-alpine AS builder - -WORKDIR /app - -COPY package*.json ./ -RUN npm ci - -COPY . . -RUN npm run build - -FROM nginx:alpine - -COPY --from=builder /app/dist /usr/share/nginx/html - -COPY nginx.conf /etc/nginx/conf.d/default.conf - -EXPOSE 80 - -CMD ["nginx", "-g", "daemon off;"] diff --git a/frontend/nginx.conf b/frontend/nginx.conf deleted file mode 100644 index 3d908db..0000000 --- a/frontend/nginx.conf +++ /dev/null @@ -1,27 +0,0 @@ -server { - listen 80; - server_name localhost; - root /usr/share/nginx/html; - index index.html; - - location / { - try_files $uri $uri/ /index.html; - } - - location /api/ { - proxy_pass http://backend:8080/api/; - proxy_http_version 1.1; - proxy_set_header Upgrade $http_upgrade; - proxy_set_header Connection 'upgrade'; - proxy_set_header Host $host; - proxy_set_header X-Real-IP $remote_addr; - proxy_cache_bypass $http_upgrade; - } - - location /uploads/ { - proxy_pass http://backend:8080/uploads/; - proxy_http_version 1.1; - proxy_set_header Host $host; - proxy_set_header X-Real-IP $remote_addr; - } -}