refactor Dockerfile and docker-compose.yml for multi-stage builds; update makefile for test execution

This commit is contained in:
KidusAlemayehu 2025-05-12 02:31:36 +03:00
parent 29642bfd14
commit 8d7911375b
3 changed files with 21 additions and 4 deletions

View File

@ -8,7 +8,7 @@ COPY . .
RUN go build -ldflags="-s -w" -o ./bin/web ./cmd/main.go
# Runner stage
FROM alpine:3.21
FROM alpine:3.21 AS runner
WORKDIR /app
COPY .env .
COPY --from=builder /app/bin/web /app/bin/web

View File

@ -39,6 +39,7 @@ services:
build:
context: .
dockerfile: Dockerfile
target: runner
ports:
- ${PORT}:8080
environment:
@ -50,6 +51,18 @@ services:
- app
command: ["/app/bin/web"]
test:
build:
context: .
dockerfile: Dockerfile
target: builder
volumes:
- .:/app
command: ["tail", "-f", "/dev/null"]
networks:
- app
networks:
app:
driver: bridge

View File

@ -1,13 +1,17 @@
include .env
.PHONY: test
test:
@docker compose exec app go test ./app
@docker compose up -d test
@docker compose exec test go test ./...
@docker compose stop test
.PHONY: coverage
coverage:
@mkdir -p coverage
@docker compose exec app go test -coverprofile=coverage.out ./internal/...
@docker compose exec app go tool cover -func=coverage.out -o coverage/coverage.txt
@docker compose up -d test
@docker compose exec test sh -c "go test -coverprofile=coverage.out ./internal/... && go tool cover -func=coverage.out -o coverage/coverage.txt"
@docker cp $(shell docker ps -q -f "name=fortunebet-test-1"):/app/coverage ./ || true
@docker compose stop test
.PHONY: build
build: