-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDockerfile
More file actions
130 lines (106 loc) · 3.41 KB
/
Copy pathDockerfile
File metadata and controls
130 lines (106 loc) · 3.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# ----------------------------------------
# Stage 1: Protoc Code Generation
# ----------------------------------------
FROM --platform=$BUILDPLATFORM ubuntu:22.04 AS proto-builder
# Avoid warnings by switching to noninteractive
ENV DEBIAN_FRONTEND=noninteractive
ARG PROTOC_VERSION=21.9
ARG GO_VERSION=1.25.1
# Configure apt and install packages
RUN apt-get update \
&& apt-get -y install --no-install-recommends \
#
# Install essential development tools
build-essential \
ca-certificates \
git \
unzip \
wget \
#
# Detect architecture for downloads
&& ARCH_SUFFIX=$(case "$(uname -m)" in \
x86_64) echo "x86_64" ;; \
aarch64) echo "aarch_64" ;; \
*) echo "x86_64" ;; \
esac) \
&& GOARCH_SUFFIX=$(case "$(uname -m)" in \
x86_64) echo "amd64" ;; \
aarch64) echo "arm64" ;; \
*) echo "amd64" ;; \
esac) \
#
# Install Protocol Buffers compiler
&& wget -O protoc.zip https://github.com/protocolbuffers/protobuf/releases/download/v${PROTOC_VERSION}/protoc-${PROTOC_VERSION}-linux-${ARCH_SUFFIX}.zip \
&& unzip protoc.zip -d /usr/local \
&& rm protoc.zip \
&& chmod +x /usr/local/bin/protoc \
#
# Install Go
&& wget -O go.tar.gz https://go.dev/dl/go${GO_VERSION}.linux-${GOARCH_SUFFIX}.tar.gz \
&& tar -C /usr/local -xzf go.tar.gz \
&& rm go.tar.gz \
#
# Clean up
&& apt-get autoremove -y \
&& apt-get clean -y \
&& rm -rf /var/lib/apt/lists/*
# Set up Go environment
ENV GOROOT=/usr/local/go
ENV GOPATH=/go
ENV PATH=$GOPATH/bin:$GOROOT/bin:$PATH
# Install protoc Go tools and plugins
RUN go install google.golang.org/protobuf/cmd/protoc-gen-go@latest \
&& go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@latest \
&& go install github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-grpc-gateway@latest \
&& go install github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-openapiv2@latest
# Set working directory
WORKDIR /build
# Copy proto sources and generator script
COPY proto.sh .
COPY proto/ proto/
# Generate protobuf files.
RUN chmod +x proto.sh && \
./proto.sh
# ----------------------------------------
# Stage 2: gRPC Server Builder
# ----------------------------------------
FROM --platform=$BUILDPLATFORM golang:1.25 AS builder
ARG TARGETOS
ARG TARGETARCH
ARG GOOS=$TARGETOS
ARG GOARCH=$TARGETARCH
ARG CGO_ENABLED=0
# Set working directory
WORKDIR /build
# Copy and download the dependencies for application
COPY go.mod go.sum ./
RUN go mod download
# Copy application code
COPY . .
# Copy generated protobuf files from stage 1
COPY --from=proto-builder /build/pkg/pb pkg/pb
# Build the Go application binary for the target OS and architecture
RUN go build -v -modcacherw -o /output/$TARGETOS/$TARGETARCH/service .
# ----------------------------------------
# Stage 3: Runtime Container
# ----------------------------------------
FROM alpine:3.22
# Set the value for the target OS and architecture.
ARG TARGETOS
ARG TARGETARCH
# Set working directory.
WORKDIR /app
RUN mkdir -p gateway/apidocs
# Copy api docs and build
COPY --from=proto-builder /build/gateway/apidocs gateway/apidocs
COPY --from=builder /output/$TARGETOS/$TARGETARCH/service service
COPY third_party third_party
COPY migrations migrations
# Plugin Arch gRPC Server Port
EXPOSE 6565
# gRPC Gateway Port
EXPOSE 8000
# Prometheus /metrics Web Server Port
EXPOSE 8080
# Entrypoint
CMD [ "/app/service" ]