Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 19 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
FROM golang:1.22.0 as builder
# Step 1: Build Stage
FROM golang:1.23.0 AS builder

WORKDIR /app

Expand All @@ -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"]
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
9 changes: 5 additions & 4 deletions docker/docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions entrypoint.sh
Original file line number Diff line number Diff line change
@@ -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
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module go-sample-rest-api

go 1.22.0
go 1.23.0

toolchain go1.24.1

require (
Expand Down