-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathDockerfile
More file actions
224 lines (171 loc) · 5.82 KB
/
Dockerfile
File metadata and controls
224 lines (171 loc) · 5.82 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
# Copyright (c) 2025 AccelByte Inc. All Rights Reserved.
# This is licensed software from AccelByte Inc, for limitations
# and restrictions contact your company contract manager.
# ----------------------------------------
# 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 PYTHON_VERSION=3.10
ARG GO_VERSION=1.24.10
# Configure apt and install packages
RUN apt-get update \
&& apt-get install -y --no-install-recommends \
#
# Install essential development tools
build-essential \
ca-certificates \
git \
unzip \
wget \
#
# Install Python and pip
python${PYTHON_VERSION} \
python${PYTHON_VERSION}-dev \
python${PYTHON_VERSION}-venv \
python3-pip \
#
# 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 Python symlinks
RUN ln -sf /usr/bin/python${PYTHON_VERSION} /usr/local/bin/python3 \
&& ln -sf /usr/bin/python${PYTHON_VERSION} /usr/local/bin/python \
&& ln -sf /usr/bin/pip3 /usr/local/bin/pip
# Install Python tools required for proto generation.
RUN pip install --no-cache-dir grpcio-tools==1.76.0 mypy-protobuf==4.0.0
# 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 -v google.golang.org/protobuf/cmd/protoc-gen-go@latest \
&& go install -v google.golang.org/grpc/cmd/protoc-gen-go-grpc@latest \
&& go install -v github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-grpc-gateway@v2.26.3 \
&& go install -v github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-openapiv2@v2.26.3
# Set working directory.
WORKDIR /build
# Copy proto sources and generator script.
COPY proto.sh .
COPY proto/ proto/
# Make script executable and run it.
RUN chmod +x proto.sh && \
./proto.sh
# ----------------------------------------
# Stage 2: gRPC Gateway Builder
# ----------------------------------------
FROM --platform=$BUILDPLATFORM golang:1.24 AS grpc-gateway-builder
ARG TARGETOS
ARG TARGETARCH
ARG GOOS=$TARGETOS
ARG GOARCH=$TARGETARCH
ARG CGO_ENABLED=0
# Set working directory.
WORKDIR /build
# Copy gateway go module files.
COPY gateway/go.mod gateway/go.sum ./
# Download dependencies.
RUN go mod download
# Copy application code.
COPY gateway/ .
# Copy generated protobuf files from stage 1.
RUN rm -rf pkg/pb
COPY --from=proto-builder /build/gateway/pkg/pb ./pkg/pb
# Build application code.
RUN go build -v -o /output/$TARGETOS/$TARGETARCH/grpc_gateway .
# ----------------------------------------
# Stage 3: gRPC Server Builder
# ----------------------------------------
FROM ubuntu:22.04 AS grpc-server-builder
ARG TARGETOS
ARG TARGETARCH
# Keeps Python from generating .pyc files in the container.
ENV PYTHONDONTWRITEBYTECODE=1
# Turns off buffering for easier container logging.
ENV PYTHONUNBUFFERED=1
# Install Python.
RUN apt update && \
apt install -y --no-install-recommends \
python3-venv && \
apt clean && \
rm -rf /var/lib/apt/lists/*
RUN useradd user
# Set working directory.
WORKDIR /build
# Create and activate virtual environment.
RUN python3 -m venv venv
ENV PATH="/build/venv/bin:$PATH"
# Install Python dependencies.
COPY requirements.txt .
RUN python3 -m pip install \
--no-cache-dir \
--requirement requirements.txt
# Copy apidocs code from stage 1.
COPY --from=proto-builder /build/gateway/apidocs ./apidocs
# Copy gateway code from stage 2.
COPY --from=grpc-gateway-builder /output/$TARGETOS/$TARGETARCH/grpc_gateway ./
# Copy other gateway code.
COPY gateway/third_party third_party/
# Copy application code.
COPY src/ .
# Copy generated protobuf files from stage 1.
COPY --from=proto-builder /build/src/ .
# Copy entrypoint script.
COPY wrapper.sh .
RUN chmod +x wrapper.sh
# Fix up python3 symlink for use in chiseled Ubuntu.
RUN ln -sf /usr/bin/python3 /build/venv/bin/python3
# ----------------------------------------
# Stage 4: Runtime Container
# ----------------------------------------
FROM ubuntu/python:3.10-22.04_stable
# Keeps Python from generating .pyc files in the container.
ENV PYTHONDONTWRITEBYTECODE=1
# Turns off buffering for easier container logging.
ENV PYTHONUNBUFFERED=1
# Set working directory.
WORKDIR /app
# Copy build from stage 2.
COPY --from=grpc-server-builder /usr/bin/bash /usr/bin/bash
COPY --from=grpc-server-builder /usr/bin/kill /usr/bin/kill
COPY --from=grpc-server-builder /usr/bin/sleep /usr/bin/sleep
COPY --from=grpc-server-builder /etc/passwd /etc/passwd
COPY --from=grpc-server-builder /etc/group /etc/group
COPY --from=grpc-server-builder /build/ .
USER user
# Activate virtual environment.
ENV PATH="/app/venv/bin:$PATH"
# Plugin Arch gRPC Server Port.
EXPOSE 6565
# gRPC Gateway Port.
EXPOSE 8000
# Prometheus /metrics Web Server Port.
EXPOSE 8080
# Entrypoint.
ENTRYPOINT ["bash", "/app/wrapper.sh"]