Added Docker binding

This commit is contained in:
Valentin Popov 2025-05-05 23:30:38 +00:00
parent 97206989c0
commit 6bfcb79d41
No known key found for this signature in database
GPG Key ID: AE3CE523DAAA8401
5 changed files with 93 additions and 2 deletions

View File

@ -13,7 +13,7 @@ run-worker:
go run ./cmd/worker go run ./cmd/worker
docker-up: docker-up:
docker-compose up --build --detach docker-compose up --build --detach --wait
docker-down: docker-down:
docker-compose down docker-compose down

View File

@ -84,7 +84,7 @@ curl http://localhost:8080/stats/123
| Requirement | Minimum / Bonus | | Requirement | Minimum / Bonus |
| - | - | | - | - |
| Go 1.22+, only stdlib + light libs | sqlx / chi / validator OK | | Go 1.24+, only stdlib + light libs | sqlx / chi / validator OK |
| `go vet` and `go test -race` pass | required | | `go vet` and `go test -race` pass | required |
| Test coverage | ≥ 60% | | Test coverage | ≥ 60% |
| Dockerfile + docker-compose.yml | bonus (+1) | | Dockerfile + docker-compose.yml | bonus (+1) |

37
cmd/server/Dockerfile Normal file
View File

@ -0,0 +1,37 @@
# Build stage
FROM golang:1.24-alpine AS builder
WORKDIR /app
# Install build dependencies
RUN apk add --no-cache gcc musl-dev
# Copy go mod files
COPY go.mod go.sum ./
RUN go mod download
# Copy source code
COPY . .
# Build the application
RUN CGO_ENABLED=1 GOOS=linux go build -o server ./cmd/server
# Final stage
FROM alpine:latest
WORKDIR /app
# Install runtime dependencies
RUN apk add --no-cache sqlite
# Copy the binary from builder
COPY --from=builder /app/server .
# Create directory for database
RUN mkdir -p /data
# Expose the port
EXPOSE 8080
# Run the application
CMD ["./server", "--db", "/data/data.db", "--listen", "8080"]

34
cmd/worker/Dockerfile Normal file
View File

@ -0,0 +1,34 @@
# Build stage
FROM golang:1.24-alpine AS builder
WORKDIR /app
# Install build dependencies
RUN apk add --no-cache gcc musl-dev
# Copy go mod files
COPY go.mod go.sum ./
RUN go mod download
# Copy source code
COPY . .
# Build the application
RUN CGO_ENABLED=1 GOOS=linux go build -o worker ./cmd/worker
# Final stage
FROM alpine:latest
WORKDIR /app
# Install runtime dependencies
RUN apk add --no-cache sqlite
# Copy the binary from builder
COPY --from=builder /app/worker .
# Create directory for database
RUN mkdir -p /data
# Run the application
CMD ["./worker", "--db", "/data/data.db", "--poll", "100ms"]

20
docker-compose.yml Normal file
View File

@ -0,0 +1,20 @@
services:
server:
build:
context: ./
dockerfile: ./cmd/server/Dockerfile
volumes:
- broker-db:/data
ports:
- "8080:8080"
worker:
build:
context: ./
dockerfile: ./cmd/worker/Dockerfile
volumes:
- broker-db:/data
volumes:
broker-db:
driver: local