Skip to content

Commit 5c13bd6

Browse files
CopilotNeiland85
andcommitted
Fix Dockerfile.api permissions and PATH for non-root user execution
Co-authored-by: Neiland85 <164719485+Neiland85@users.noreply.github.com>
1 parent a4e80ba commit 5c13bd6

File tree

2 files changed

+22
-17
lines changed

2 files changed

+22
-17
lines changed

.dockerignore

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,13 @@ neurobank.db
4747
*~
4848
.DS_Store
4949

50-
# Documentation
51-
*.md
50+
# Documentation - exclude most but keep important ones
5251
docs/
52+
*.md
53+
!README.md
54+
!docker/README.md
55+
56+
# Dependencies
5357
*.txt
5458
!requirements.txt
5559
!requirements-dev.txt

docker/Dockerfile.api

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
# ============================================================================
55
# Stage 1: Builder - Install dependencies
66
# ============================================================================
7-
FROM python:3.11-slim as builder
7+
FROM python:3.11-slim AS builder
88

99
# Set working directory
1010
WORKDIR /build
@@ -16,20 +16,29 @@ RUN apt-get update && apt-get install -y --no-install-recommends \
1616
libpq-dev \
1717
&& rm -rf /var/lib/apt/lists/*
1818

19-
# Copy requirements and install Python dependencies
19+
# Copy requirements and install Python dependencies to /opt/venv
20+
RUN python -m venv /opt/venv
21+
ENV PATH="/opt/venv/bin:$PATH"
22+
2023
COPY requirements.txt .
2124
RUN pip install --no-cache-dir --upgrade pip setuptools wheel && \
22-
pip install --no-cache-dir --user -r requirements.txt
25+
pip install --no-cache-dir -r requirements.txt
2326

2427
# ============================================================================
2528
# Stage 2: Runtime - Minimal production image
2629
# ============================================================================
2730
FROM python:3.11-slim
2831

32+
# Build arguments for metadata
33+
ARG BUILD_DATE
34+
ARG VCS_REF
35+
2936
# Set labels for image metadata
3037
LABEL maintainer="NeuroBank Development Team <dev@neurobank.com>"
3138
LABEL description="NeuroBank FastAPI Banking System - Production API"
3239
LABEL version="1.0.0"
40+
LABEL org.opencontainers.image.created="${BUILD_DATE}"
41+
LABEL org.opencontainers.image.revision="${VCS_REF}"
3342

3443
# Set working directory
3544
WORKDIR /app
@@ -41,8 +50,8 @@ RUN apt-get update && apt-get install -y --no-install-recommends \
4150
&& rm -rf /var/lib/apt/lists/* \
4251
&& apt-get clean
4352

44-
# Copy Python dependencies from builder stage
45-
COPY --from=builder /root/.local /root/.local
53+
# Copy Python virtual environment from builder stage
54+
COPY --from=builder /opt/venv /opt/venv
4655

4756
# Copy application code
4857
COPY ./app ./app
@@ -63,7 +72,7 @@ ENV PYTHONPATH=/app \
6372
PYTHONDONTWRITEBYTECODE=1 \
6473
PORT=8000 \
6574
ENVIRONMENT=production \
66-
PATH=/root/.local/bin:$PATH
75+
PATH="/opt/venv/bin:$PATH"
6776

6877
# Expose port (will be overridden by $PORT in cloud environments)
6978
EXPOSE 8000
@@ -73,12 +82,4 @@ HEALTHCHECK --interval=30s --timeout=10s --start-period=10s --retries=3 \
7382
CMD curl -f http://localhost:${PORT:-8000}/health || exit 1
7483

7584
# Run the application with uvicorn
76-
# Use shell form to allow environment variable expansion
77-
CMD uvicorn app.main:app \
78-
--host 0.0.0.0 \
79-
--port ${PORT:-8000} \
80-
--workers ${WORKERS:-1} \
81-
--loop uvloop \
82-
--timeout-keep-alive 120 \
83-
--access-log \
84-
--log-level info
85+
CMD ["sh", "-c", "uvicorn app.main:app --host 0.0.0.0 --port ${PORT:-8000} --workers ${WORKERS:-1} --loop uvloop --timeout-keep-alive 120 --access-log --log-level info"]

0 commit comments

Comments
 (0)