Skip to content

Commit e30aaad

Browse files
committed
Run container as non-root user
1 parent 576c935 commit e30aaad

2 files changed

Lines changed: 31 additions & 5 deletions

File tree

.gitignore

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
/**/.idea/
2-
/**/.pytest_cache/
3-
/**/__pycache__/
4-
/**/venv/
1+
.idea/
2+
.pytest_cache/
3+
__pycache__/
4+
venv/
5+
CLAUDE.md
6+
plan_*
7+

Dockerfile

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,37 @@
11
FROM tiangolo/uvicorn-gunicorn-fastapi:python3.9
22

3+
# Create a non-root user to run the application
4+
RUN groupadd -g 1000 zxbasic && \
5+
useradd -r -u 1000 -g zxbasic -m -d /home/zxbasic -s /bin/bash zxbasic
6+
37
WORKDIR /app
48

9+
# Copy requirements as root for installation
510
COPY ./requirements.txt /app/requirements.txt
611

12+
# Install dependencies as root
713
RUN apt-get update \
814
&& apt-get install gcc -y \
915
&& apt-get clean
1016

1117
RUN pip install -r /app/requirements.txt \
1218
&& rm -rf /root/.cache/pip
1319

14-
COPY . /app/
20+
# Copy application code and set ownership
21+
COPY --chown=zxbasic:zxbasic . /app/
22+
23+
# Create a directory for temporary files with proper permissions
24+
RUN mkdir -p /tmp/zxbasic && \
25+
chown -R zxbasic:zxbasic /tmp/zxbasic && \
26+
chmod 755 /tmp/zxbasic
27+
28+
# Ensure the app directory is owned by zxbasic user
29+
RUN chown -R zxbasic:zxbasic /app
30+
31+
# Switch to non-root user
32+
USER zxbasic
33+
34+
# Set environment variable for temp directory (optional - Python tempfile will use system default)
35+
# ENV TMPDIR=/tmp/zxbasic
36+
37+
# The container will run with the default command from the base image

0 commit comments

Comments
 (0)