Skip to content

Commit b31f64a

Browse files
committed
[+] add Dockerfile
1 parent 4f419f0 commit b31f64a

File tree

2 files changed

+64
-2
lines changed

2 files changed

+64
-2
lines changed

Dockerfile

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# When building an image it is recommended to provide version arguments, e.g.
2+
# docker build --no-cache -t cybertecpostgresql/pg_timetable:<tagname> \
3+
# --build-arg COMMIT=`git show -s --format=%H HEAD` \
4+
# --build-arg VERSION=`git describe --tags --abbrev=0` \
5+
# --build-arg DATE=`git show -s --format=%cI HEAD` .
6+
FROM golang:alpine AS builder
7+
8+
ARG LDFLAGS
9+
10+
# Set necessary environmet variables needed for our image
11+
ENV GO111MODULE=on \
12+
CGO_ENABLED=0 \
13+
GOOS=linux \
14+
GOARCH=amd64
15+
16+
# Move to working directory /build
17+
WORKDIR /build
18+
19+
# Copy and download dependency using go mod
20+
COPY go.mod .
21+
COPY go.sum .
22+
RUN go mod download
23+
24+
# Copy the code into the container
25+
COPY . .
26+
27+
# Build the application
28+
RUN go build -buildvcs=false \
29+
-ldflags="$LDFLAGS" \
30+
-o pg_etcd \
31+
./cmd/pg_etcd
32+
33+
# Update certificates
34+
RUN apk update && apk upgrade && apk add --no-cache ca-certificates
35+
RUN update-ca-certificates
36+
37+
FROM alpine
38+
39+
# Copy the binary and certificates into the container
40+
COPY --from=builder /build/pg_etcd /
41+
COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/
42+
43+
# Command to run the executable
44+
ENTRYPOINT ["/pg_etcd"]

Makefile

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,22 @@
44

55
# Build variables
66
BINARY_NAME=pg_etcd
7+
DOCKER_IMAGE=cybertecpostgresql/$(BINARY_NAME)
78
VERSION?=dev
89
BUILD_DIR=.
9-
LDFLAGS=-ldflags="-X main.version=$(VERSION)"
10+
LDFLAGS:=-X main.version=$(VERSION) -X main.date=$(shell date -u +%Y-%m-%dT%H:%M:%SZ) -X main.commit=$(shell git rev-parse --short HEAD)
1011

1112
# Default target
1213
.DEFAULT_GOAL := help
1314

15+
foo:
16+
@echo $(LDFLAGS)
17+
1418
## Build the binary
1519
build:
1620
@echo "Building $(BINARY_NAME)..."
1721
@mkdir -p $(BUILD_DIR)
18-
@go build $(LDFLAGS) -o $(BUILD_DIR)/$(BINARY_NAME) ./cmd/pg_etcd
22+
@go build -ldflags="$(LDFLAGS)" -o $(BUILD_DIR)/$(BINARY_NAME) ./cmd/pg_etcd
1923

2024
## Run tests
2125
test:
@@ -57,6 +61,20 @@ build-all:
5761
@GOOS=darwin GOARCH=arm64 go build $(LDFLAGS) -o $(BUILD_DIR)/$(BINARY_NAME)-darwin-arm64 ./cmd/pg_etcd
5862
@GOOS=windows GOARCH=amd64 go build $(LDFLAGS) -o $(BUILD_DIR)/$(BINARY_NAME)-windows-amd64.exe ./cmd/pg_etcd
5963

64+
## Build Docker image
65+
docker-build:
66+
@echo "Building Docker image..."
67+
@docker build -t $(DOCKER_IMAGE):$(VERSION) --build-arg LDFLAGS="$(LDFLAGS)" .
68+
@docker tag $(DOCKER_IMAGE):$(VERSION) $(DOCKER_IMAGE):latest
69+
@echo "Docker image $(DOCKER_IMAGE):$(VERSION) built successfully."
70+
71+
## Push Docker image to registry
72+
docker-push: docker-build
73+
@echo "Pushing Docker image to registry..."
74+
@docker push $(DOCKER_IMAGE):$(VERSION)
75+
@docker push $(DOCKER_IMAGE):latest
76+
@echo "Docker images pushed successfully."
77+
6078
## Run the binary (requires PostgreSQL and etcd)
6179
run: build
6280
@echo "Running $(BINARY_NAME)..."

0 commit comments

Comments
 (0)