diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..4eb0c24 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,19 @@ +# Build stage +FROM golang:1.26.3-alpine3.23 AS builder +WORKDIR /app +COPY . . +RUN go build -o main main.go +RUN go install -tags 'postgres' github.com/golang-migrate/migrate/v4/cmd/migrate@latest + +# Run stage +FROM alpine:3.23 +WORKDIR /app +COPY --from=builder /app/main . +COPY --from=builder /go/bin/migrate . +COPY db/migration ./migration +COPY app.env . +COPY start.sh . +RUN chmod +x /app/start.sh +EXPOSE 8080 +ENTRYPOINT ["/app/start.sh"] +CMD ["/app/main"] \ No newline at end of file diff --git a/docker-compose.yaml b/docker-compose.yaml new file mode 100644 index 0000000..900ca96 --- /dev/null +++ b/docker-compose.yaml @@ -0,0 +1,26 @@ +services: + postgres: + image: postgres:16-alpine + ports: + - "25432:5432" + environment: + - POSTGRES_USER=root + - POSTGRES_PASSWORD=secret + - POSTGRES_DB=bank_db + healthcheck: + test: ["CMD-SHELL", "pg_isready -U root -d bank_db"] + interval: 5s + timeout: 5s + retries: 5 + api: + build: + context: . + dockerfile: Dockerfile + ports: + - "8080:8080" + environment: + - DB_SOURCE=postgresql://root:secret@postgres:5432/bank_db?sslmode=disable + depends_on: + postgres: + condition: service_healthy + diff --git a/start.sh b/start.sh new file mode 100755 index 0000000..adedef8 --- /dev/null +++ b/start.sh @@ -0,0 +1,9 @@ +#!/bin/sh + +set -e + +echo "Run db migration" +/app/migrate -path /app/migration -database "$DB_SOURCE" --verbose up + +echo "Start the app" +exec "$@"