Skip to content
Open
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
5 changes: 4 additions & 1 deletion .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,7 @@ data/
.vscode/
.idea/
brasilcep-webservice
wserver
wserver
.git/
data.tar.gz
data-dne.tar.gz
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ tmp_dne/
.vscode/
.idea/
brasilcep-webservice
wserver
wserver
data-dne.tar.gz
43 changes: 32 additions & 11 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,24 +1,45 @@
FROM alpine:latest AS base
RUN apk update && apk upgrade
# syntax=docker/dockerfile:1

FROM base AS builder
FROM golang:1.24-alpine AS builder

RUN apk add go make git
WORKDIR /src

WORKDIR /app
# Keep dependency downloads cached when only application sources change.
COPY go.mod go.sum ./
RUN go mod download

COPY . .

RUN make build
ARG VERSION=dev
ARG COMMIT=none
ARG REPO=github.com/brasilcep/api

RUN CGO_ENABLED=0 GOOS=linux go build \
-trimpath \
-ldflags="-s -w \
-X 'main.Version=${VERSION}' \
-X 'main.Commit=${COMMIT}' \
-X 'main.Repo=${REPO}' \
-X 'main.Compiler=$(go version | awk '{print $3}')'" \
-o /out/wserver ./main.go

FROM base AS final
FROM alpine:3.22 AS final

RUN apk add --no-cache su-exec tar && \
addgroup -S brasilcep && \
adduser -S -G brasilcep -h /app brasilcep

WORKDIR /app

COPY --from=builder /app/data.tar.gz /app/data.tar.gz
COPY --from=builder /out/wserver ./wserver
COPY --chown=brasilcep:brasilcep --chmod=755 docker-entrypoint.sh ./docker-entrypoint.sh

ENV DB_PATH=/app/data \
MODE=listen

RUN tar -xzf data.tar.gz && rm -rf data.tar.gz
EXPOSE 8080

COPY --from=builder /app/wserver /app/wserver
HEALTHCHECK --interval=30s --timeout=5s --start-period=30s --retries=3 \
CMD wget -q -O /dev/null http://127.0.0.1:8080/healthcheck || exit 1

CMD ["./wserver"]
ENTRYPOINT ["/app/docker-entrypoint.sh"]
25 changes: 20 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ VERSION := $(shell \
GO_VERSION := $(shell go version | awk '{print $$3}')

REPO := github.com/brasilcep/api
IMAGE_NAME ?= brasilcep/api
IMAGE_TAG ?= dev
DOCKER_IMAGE := $(IMAGE_NAME):$(IMAGE_TAG)

.PHONY: build test lint docker-build docker-push docker-publish run-docker benchmark-docker

build:
@echo "Building version $(VERSION) (commit: $(COMMIT))"
Expand All @@ -28,16 +33,26 @@ lint:
golangci-lint run --timeout 5m

docker-build:
tar -czf data.tar.gz data/
docker build -t brasilcep/api:dev .
@echo "Building Docker image $(DOCKER_IMAGE)"
docker build \
--build-arg VERSION="$(VERSION)" \
--build-arg COMMIT="$(COMMIT)" \
--build-arg REPO="$(REPO)" \
-t "$(DOCKER_IMAGE)" .

docker-push:
docker push "$(DOCKER_IMAGE)"

docker-publish: docker-build
docker push "$(DOCKER_IMAGE)"

run-docker:
run-docker:
docker run --cpus="0.5" --memory="512m" \
-p 8080:8080 \
brasilcep/api:dev
"$(DOCKER_IMAGE)"

benchmark-docker:
docker compose up brasilcep-api -d
@echo "Waiting for the API to start and load data..."
sleep 120
docker compose up
docker compose up
Loading