-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDockerfile
More file actions
149 lines (115 loc) · 3.92 KB
/
Dockerfile
File metadata and controls
149 lines (115 loc) · 3.92 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
# 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
# Configure apt and install packages
RUN apt-get update \
&& apt-get install -y --no-install-recommends \
#
# Install essential development tools
build-essential \
ca-certificates \
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) \
#
# 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 \
#
# 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 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 Server Builder
# ----------------------------------------
FROM ubuntu:22.04 AS grpc-server-builder
# 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 application code.
COPY src/ .
# Copy generated protobuf files from stage 1.
COPY --from=proto-builder /build/src/ .
# Fix up python3 symlink for use in chiseled Ubuntu.
RUN ln -sf /usr/bin/python3 /build/venv/bin/python3
# ----------------------------------------
# Stage 3: 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
# Prometheus /metrics Web Server Port.
EXPOSE 8080
# Entrypoint.
ENTRYPOINT ["python3", "-m", "app"]