Skip to content

Commit 4957de3

Browse files
make non-root optional and configurable via the --user param
1 parent 60ed95e commit 4957de3

2 files changed

Lines changed: 62 additions & 36 deletions

File tree

Dockerfile

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -188,24 +188,24 @@ RUN apk add --no-cache git ca-certificates bind-tools tini jansson wget supervis
188188
ARG UID=1500
189189
ARG GID=1500
190190

191-
# To run as non-root, the user must be part of postgres, redis and node groups
191+
# Always create the non-root user to support runtime user switching
192+
# The container can be run as root (default) or as sourcebot user using docker run --user
192193
RUN addgroup -g $GID sourcebot && \
193194
adduser -D -u $UID -h /app -S sourcebot && \
194195
adduser sourcebot postgres && \
195196
adduser sourcebot redis && \
196197
adduser sourcebot node && \
197-
chown -R sourcebot /app && \
198198
mkdir /var/log/sourcebot && \
199199
chown sourcebot /var/log/sourcebot
200200

201-
COPY --chown=sourcebot:sourcebot package.json yarn.lock* .yarnrc.yml public.pem ./
202-
COPY --chown=sourcebot:sourcebot .yarn ./.yarn
201+
COPY package.json yarn.lock* .yarnrc.yml public.pem ./
202+
COPY .yarn ./.yarn
203203

204204
# Configure zoekt
205-
COPY --chown=sourcebot:sourcebot vendor/zoekt/install-ctags-alpine.sh .
205+
COPY vendor/zoekt/install-ctags-alpine.sh .
206206
RUN ./install-ctags-alpine.sh && rm install-ctags-alpine.sh
207-
RUN mkdir -p ${DATA_CACHE_DIR} && chown -R sourcebot ${DATA_CACHE_DIR}
208-
COPY --chown=sourcebot:sourcebot --from=zoekt-builder \
207+
RUN mkdir -p ${DATA_CACHE_DIR}
208+
COPY --from=zoekt-builder \
209209
/cmd/zoekt-git-index \
210210
/cmd/zoekt-indexserver \
211211
/cmd/zoekt-mirror-github \
@@ -218,18 +218,17 @@ COPY --chown=sourcebot:sourcebot --from=zoekt-builder \
218218
/usr/local/bin/
219219

220220
# Copy all of the things
221-
COPY --chown=sourcebot:sourcebot --from=web-builder /app/packages/web/public ./packages/web/public
222-
COPY --chown=sourcebot:sourcebot --from=web-builder /app/packages/web/.next/standalone ./
223-
COPY --chown=sourcebot:sourcebot --from=web-builder /app/packages/web/.next/static ./packages/web/.next/static
224-
225-
COPY --chown=sourcebot:sourcebot --from=backend-builder /app/node_modules ./node_modules
226-
COPY --chown=sourcebot:sourcebot --from=backend-builder /app/packages/backend ./packages/backend
221+
COPY --from=web-builder /app/packages/web/public ./packages/web/public
222+
COPY --from=web-builder /app/packages/web/.next/standalone ./
223+
COPY --from=web-builder /app/packages/web/.next/static ./packages/web/.next/static
227224

228-
COPY --chown=sourcebot:sourcebot --from=shared-libs-builder /app/node_modules ./node_modules
229-
COPY --chown=sourcebot:sourcebot --from=shared-libs-builder /app/packages/db ./packages/db
230-
COPY --chown=sourcebot:sourcebot --from=shared-libs-builder /app/packages/schemas ./packages/schemas
231-
COPY --chown=sourcebot:sourcebot --from=shared-libs-builder /app/packages/shared ./packages/shared
225+
COPY --from=backend-builder /app/node_modules ./node_modules
226+
COPY --from=backend-builder /app/packages/backend ./packages/backend
232227

228+
COPY --from=shared-libs-builder /app/node_modules ./node_modules
229+
COPY --from=shared-libs-builder /app/packages/db ./packages/db
230+
COPY --from=shared-libs-builder /app/packages/schemas ./packages/schemas
231+
COPY --from=shared-libs-builder /app/packages/shared ./packages/shared
233232

234233
# Fixes git "dubious ownership" issues when the volume is mounted with different permissions to the container.
235234
RUN git config --global safe.directory "*"
@@ -239,14 +238,17 @@ RUN mkdir -p /run/postgresql && \
239238
chown -R postgres:postgres /run/postgresql && \
240239
chmod 775 /run/postgresql
241240

242-
COPY --chown=sourcebot:sourcebot supervisord.conf /etc/supervisor/conf.d/supervisord.conf
243-
COPY --chown=sourcebot:sourcebot prefix-output.sh ./prefix-output.sh
241+
# Make app directory accessible to both root and sourcebot user
242+
RUN chown -R sourcebot:sourcebot /app
243+
244+
COPY supervisord.conf /etc/supervisor/conf.d/supervisord.conf
245+
COPY prefix-output.sh ./prefix-output.sh
244246
RUN chmod +x ./prefix-output.sh
245-
COPY --chown=sourcebot:sourcebot entrypoint.sh ./entrypoint.sh
247+
COPY entrypoint.sh ./entrypoint.sh
246248
RUN chmod +x ./entrypoint.sh
247249

248-
249-
USER sourcebot
250+
# Default to root user, but can be overridden at runtime with --user flag
251+
# No USER directive = runs as root by default
250252

251253
EXPOSE 3000
252254
ENV PORT=3000

entrypoint.sh

Lines changed: 38 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,18 @@ set -e
55
# Disable auto-exporting of variables
66
set +a
77

8+
# Detect if running as root
9+
IS_ROOT=false
10+
if [ "$(id -u)" -eq 0 ]; then
11+
IS_ROOT=true
12+
fi
13+
14+
if [ "$IS_ROOT" = "true" ]; then
15+
echo -e "\e[34m[Info] Running as root user.\e[0m"
16+
else
17+
echo -e "\e[34m[Info] Running as non-root user.\e[0m"
18+
fi
19+
820
# If a CONFIG_PATH is set, resolve the environment overrides from the config file.
921
# The overrides will be written into variables scopped to the current shell. This is
1022
# required in case one of the variables used in this entrypoint is overriden (e.g.,
@@ -82,10 +94,14 @@ fi
8294

8395
# Check if DATABASE_DATA_DIR exists, if not initialize it
8496
if [ "$DATABASE_EMBEDDED" = "true" ] && [ ! -d "$DATABASE_DATA_DIR" ]; then
85-
echo -e "\e[34m[Info] Initializing database at $DATABASE_D\ATA_DIR...\e[0m"
97+
echo -e "\e[34m[Info] Initializing database at $DATABASE_DATA_DIR...\e[0m"
8698
mkdir -m 0750 -p $DATABASE_DATA_DIR
87-
88-
initdb -D "$DATABASE_DATA_DIR"
99+
if [ "$IS_ROOT" = "true" ]; then
100+
chown -R postgres:postgres "$DATABASE_DATA_DIR"
101+
su postgres -c "initdb -D $DATABASE_DATA_DIR"
102+
else
103+
initdb -D "$DATABASE_DATA_DIR"
104+
fi
89105
fi
90106

91107
# Create the redis data directory if it doesn't exist
@@ -181,8 +197,13 @@ echo "{\"version\": \"$NEXT_PUBLIC_SOURCEBOT_VERSION\", \"install_id\": \"$SOURC
181197

182198
# Start the database and wait for it to be ready before starting any other service
183199
if [ "$DATABASE_EMBEDDED" = "true" ]; then
184-
postgres -D "$DATABASE_DATA_DIR" &
185-
until pg_isready -h localhost -p 5432 -d sourcebot -U postgres; do
200+
if [ "$IS_ROOT" = "true" ]; then
201+
su postgres -c "postgres -D $DATABASE_DATA_DIR" &
202+
else
203+
postgres -D "$DATABASE_DATA_DIR" &
204+
fi
205+
206+
until pg_isready -h localhost -p 5432 -U postgres; do
186207
echo -e "\e[34m[Info] Waiting for the database to be ready...\e[0m"
187208
sleep 1
188209

@@ -191,13 +212,14 @@ if [ "$DATABASE_EMBEDDED" = "true" ]; then
191212
if ! pgrep -x "postgres" > /dev/null; then
192213
echo "postgres failed to run"
193214
exit 1
194-
break
195-
fi
215+
fi
196216
done
197-
198-
# Running as non-root we need to ensure the postgres account is created.
199-
psql -U postgres -tc "SELECT 1 FROM pg_roles WHERE rolname='postgres'" | grep -q 1 \
200-
|| createuser postgres -s
217+
218+
if [ "$IS_ROOT" = "false" ]; then
219+
# Running as non-root we need to ensure the postgres account is created.
220+
psql -U postgres -tc "SELECT 1 FROM pg_roles WHERE rolname='postgres'" | grep -q 1 \
221+
|| createuser postgres -s
222+
fi
201223

202224
# Check if the database already exists, and create it if it doesn't
203225
EXISTING_DB=$(psql -U postgres -tAc "SELECT 1 FROM pg_database WHERE datname = 'sourcebot'")
@@ -214,8 +236,10 @@ fi
214236
echo -e "\e[34m[Info] Running database migration...\e[0m"
215237
DATABASE_URL="$DATABASE_URL" yarn workspace @sourcebot/db prisma:migrate:prod
216238

217-
# Create the log directory
218-
mkdir -p /var/log/sourcebot
239+
# Create the log directory if it doesn't exist
240+
if [ ! -d "/var/log/sourcebot" ]; then
241+
mkdir -m 0750 -p /var/log/sourcebot
242+
fi
219243

220244
# Run supervisord
221-
exec supervisord -c /etc/supervisor/conf.d/supervisord.conf
245+
exec supervisord -c /etc/supervisor/conf.d/supervisord.conf

0 commit comments

Comments
 (0)