Repository navigation
Expand file tree
/
Copy pathDockerfile.api
More file actions
116 lines (88 loc) · 3.5 KB
/
Copy pathDockerfile.api
File metadata and controls
116 lines (88 loc) · 3.5 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
ARG ARCH
# Clone the fronted code
FROM alpine/git:v2.47.2 as fronted-code
ARG UI_VERSION
RUN git clone -b ${UI_VERSION} https://github.com/neutree-ai/ui.git
# Build the fronted
FROM node:22.13.1 as fronted-base
WORKDIR /app/refine
COPY --from=fronted-code /git/ui/ /app/refine/
# Fetch the fronted dependencies
FROM fronted-base as fronted-deps
RUN \
if [ -f yarn.lock ]; then yarn --frozen-lockfile; \
elif [ -f package-lock.json ]; then npm ci; \
elif [ -f pnpm-lock.yaml ]; then yarn global add pnpm && pnpm i --frozen-lockfile; \
else echo "Lockfile not found." && exit 1; \
fi
# Build the fronted static files
FROM fronted-base as fronted-builder
ARG DEFAULT_CLUSTER_VERSION
ENV VITE_DEFAULT_CLUSTER_VERSION=${DEFAULT_CLUSTER_VERSION}
ENV NODE_ENV production
COPY --from=fronted-deps /app/refine/node_modules ./node_modules
COPY . .
RUN yarn build
# Build the api binary
FROM golang:1.23 as go-builder
WORKDIR /workspace
# Copy the Go Modules manifests
COPY go.mod go.mod
COPY go.sum go.sum
# Cache deps before building and copying source so that we don't need to re-download as much
# and so that source changes don't invalidate our downloaded layer
RUN go mod download
# Copy the sources
COPY ./ ./
# Build
ARG GO_BUILD_ARGS
RUN CGO_ENABLED=0 GOOS=linux GOARCH=${ARCH} \
go build "${GO_BUILD_ARGS}" \
-o neutree-api cmd/neutree-api/neutree-api.go
FROM --platform=linux/${ARCH} ubuntu:22.04
ENV NODE_ENV production
ARG DEFAULT_CLUSTER_VERSION
ENV VITE_DEFAULT_CLUSTER_VERSION=${DEFAULT_CLUSTER_VERSION}
# Copy requirements file and install dependencies
COPY requirements.txt .
RUN apt-get update && apt-get install tini util-linux python3 python3-dev nfs-common python3-pip build-essential libssl-dev libffi-dev -y && \
pip install -U --no-cache-dir -r requirements.txt && \
apt-get clean && rm -rf /var/lib/apt/lists/*
# Copy api binary
WORKDIR /
COPY --from=go-builder /workspace/neutree-api .
# Copy the fronted static files
RUN mkdir -p /public
COPY --from=fronted-builder /app/refine/dist /public
# Create entrypoint script to start rpcbind, rpc.statd and neutree-api
RUN cat > /entrypoint.sh <<'EOF'
#!/bin/bash
set -e
# Set nofile ulimit explicitly to avoid rpcbind memory allocation issues
ulimit -Sn 65536 2>/dev/null || echo "Warning: failed to set soft nofile limit to 65536" >&2
ulimit -Hn 65536 2>/dev/null || echo "Warning: failed to set hard nofile limit to 65536" >&2
echo "Effective nofile limits: soft=$(ulimit -Sn) hard=$(ulimit -Hn)"
# Start rpcbind in background
/sbin/rpcbind -w
echo "rpcbind started"
# Start rpc.statd in background
/sbin/rpc.statd
echo "rpc.statd started"
# Wait a moment for services to be ready
sleep 1
# Start main application (replace current process).
#
# tini runs as PID 1 so orphaned grandchildren are reaped. Anything that
# daemonizes below us (the rpcbind/rpc.statd started above, and the SSH
# ControlMaster that OpenSSH backgrounds for ControlPersist) is reparented
# to PID 1 on exit. Without an init there, those processes stay zombies
# forever: the main binary only waits on its own direct children, and a
# zombie pins its signal_struct, which in turn pins the scheduler autogroup
# created by the setsid() such a daemon performs. That autogroup costs
# sizeof(cfs_rq)+sizeof(sched_entity) per *possible* CPU (hundreds of KB on
# large hosts) of unreclaimable, non-cgroup-accounted kernel memory, so the
# leak escapes the pod memory limit and exhausts the node.
exec /usr/bin/tini -- /neutree-api "$@"
EOF
RUN chmod +x /entrypoint.sh
ENTRYPOINT ["/entrypoint.sh"]