From 0c1434a9dad8f62d1cdd245ffcaba88f8d683222 Mon Sep 17 00:00:00 2001 From: 0x1d Date: Mon, 27 Oct 2025 15:18:55 +0100 Subject: [PATCH] feat: add Docker build --- .dockerignore | 9 +++++++++ Dockerfile | 42 +++++++++++++++++++++++++++++++++++++++ Makefile | 21 ++++++++++++++++++-- README.md | 2 +- docs/ARCHITECTURE.md | 2 +- internal/config/config.go | 2 +- 6 files changed, 73 insertions(+), 5 deletions(-) create mode 100644 .dockerignore create mode 100644 Dockerfile diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..22b2348 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,9 @@ +.git +.gitignore +.cursor +*.md +*.db +*.bak +spore-registry +registry/* + diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..335f5ee --- /dev/null +++ b/Dockerfile @@ -0,0 +1,42 @@ +# Build stage +FROM golang:1.22-alpine AS builder + +WORKDIR /app + +# Install build dependencies for CGO +RUN apk add --no-cache gcc musl-dev sqlite-dev + +# Copy go mod files +COPY go.mod go.sum ./ +RUN go mod download + +# Copy source code +COPY . . + +# Build the application +# Note: CGO_ENABLED=1 is required for sqlite3 +RUN CGO_ENABLED=1 GOOS=linux go build -a -installsuffix cgo -o spore-registry ./main.go + +# Runtime stage +FROM alpine:latest + +# Install ca-certificates, sqlite runtime, and wget for health checks +RUN apk --no-cache add ca-certificates sqlite wget + +WORKDIR /root/ + +# Copy the binary from builder +COPY --from=builder /app/spore-registry . + +# Create registry directory +RUN mkdir -p /data/registry + +# Expose port +EXPOSE 8080 + +# Set environment variable for registry path +ENV REGISTRY_PATH=/data/registry + +# Run the application +CMD ["./spore-registry"] + diff --git a/Makefile b/Makefile index 3493685..1f17a25 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -.PHONY: build run test clean +.PHONY: build run test clean docker-build docker-run docker-push # Build the application build: @@ -16,7 +16,6 @@ test: clean: rm -f spore-registry rm -rf registry/ - rm -f registry.db # Run tests with coverage test-coverage: @@ -35,3 +34,21 @@ lint: deps: go mod download go mod tidy + +# Docker variables +DOCKER_REGISTRY ?= +IMAGE_NAME = wirelos/spore-registry +IMAGE_TAG ?= latest +FULL_IMAGE_NAME = $(if $(DOCKER_REGISTRY),$(DOCKER_REGISTRY)/$(IMAGE_NAME),$(IMAGE_NAME)):$(IMAGE_TAG) + +# Build Docker image +docker-build: + docker build -t $(FULL_IMAGE_NAME) . + +# Run Docker container +docker-run: + docker run -p 8080:8080 -v registry-data:/data/registry --rm $(FULL_IMAGE_NAME) + +# Push Docker image +docker-push: + docker push $(FULL_IMAGE_NAME) diff --git a/README.md b/README.md index fdf71e7..94c929f 100644 --- a/README.md +++ b/README.md @@ -122,7 +122,7 @@ go test ./... - `main.go` - Main application with HTTP server and API handlers - `api/openapi.yaml` - OpenAPI specification - `registry/` - Firmware storage directory (created automatically) -- `registry.db` - SQLite database (created automatically) + - `registry.db` - SQLite database (created automatically) ## License diff --git a/docs/ARCHITECTURE.md b/docs/ARCHITECTURE.md index 8079d39..155bc37 100644 --- a/docs/ARCHITECTURE.md +++ b/docs/ARCHITECTURE.md @@ -57,7 +57,7 @@ The refactored code follows a clean architecture pattern with clear separation o ## Environment Variables - `PORT`: Server port (default: 3002) -- `DB_PATH`: Database file path (default: ./registry.db) +- `DB_PATH`: Database file path (default: ./registry/registry.db) - `REGISTRY_PATH`: Firmware storage directory (default: registry) - `MAX_UPLOAD_SIZE`: Maximum upload size in bytes (default: 32MB) diff --git a/internal/config/config.go b/internal/config/config.go index f5abd2e..f5b7e5f 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -22,7 +22,7 @@ func LoadConfig() *Config { dbPath := os.Getenv("DB_PATH") if dbPath == "" { - dbPath = "./registry.db" + dbPath = "./registry/registry.db" } registry := os.Getenv("REGISTRY_PATH")