diff --git a/Dockerfile b/Dockerfile index 69f24ee..f37bb75 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,5 @@ -FROM golang:1.22.0 as builder +# Step 1: Build Stage +FROM golang:1.23.0 AS builder WORKDIR /app @@ -8,14 +9,29 @@ RUN go mod download COPY . . -RUN CGO_ENABLED=0 GOOS=linux go build -o /api ./cmd/main.go +RUN GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build -o /api ./cmd/main.go +RUN GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build -o /migrate ./cmd/migrate/main.go +COPY cmd/migrate/migrations /app/migrations + +# Step 2: Runtime Stage FROM alpine:3.14 WORKDIR / +# ✅ Install required runtime dependencies +RUN apk --no-cache add ca-certificates + +# Copy binaries from build stage COPY --from=builder /api /api +COPY --from=builder /migrate /migrate +RUN mkdir -p /cmd/migrate/migrations +COPY --from=builder /app/cmd/migrate/migrations /cmd/migrate/migrations + +# Copy entrypoint script and make executable +COPY entrypoint.sh /entrypoint.sh +RUN chmod +x /entrypoint.sh EXPOSE 8080 -CMD ["/api"] +ENTRYPOINT ["/entrypoint.sh"] diff --git a/README.md b/README.md index abfe4d6..89c6078 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,7 @@ This repository demonstrates the implementation of a RESTful API in Go with inte ## Prerequisites -- **Go Version**: Requires [Go 1.22.0](https://golang.org/dl/) or higher. +- **Go Version**: Requires [Go 1.23.0](https://golang.org/dl/) or higher. ## Dependencies diff --git a/docker/docker-compose.yaml b/docker/docker-compose.yaml index 60fb84d..1e58578 100644 --- a/docker/docker-compose.yaml +++ b/docker/docker-compose.yaml @@ -2,14 +2,15 @@ version: '3.8' services: postgres: - image: postgres:latest + image: postgres:14 + platform: linux/amd64 container_name: postgres_db ports: - "5432:5432" environment: - POSTGRES_USER: ${DB_USER} - POSTGRES_PASSWORD: ${DB_PASSWORD} - POSTGRES_DB: ${DB_NAME} + POSTGRES_USER: user + POSTGRES_PASSWORD: password + POSTGRES_DB: app volumes: - postgres_data:/var/lib/postgresql/data restart: always diff --git a/entrypoint.sh b/entrypoint.sh new file mode 100644 index 0000000..bb979c4 --- /dev/null +++ b/entrypoint.sh @@ -0,0 +1,7 @@ +#!/bin/sh + +echo "Running database migrations..." +/migrate up # ✅ Use the prebuilt migrate binary + +echo "Starting API..." +exec /api # ✅ Ensure API runs as the main process diff --git a/go.mod b/go.mod index 8c55381..dd7d35f 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,7 @@ module go-sample-rest-api -go 1.22.0 +go 1.23.0 + toolchain go1.24.1 require (