Skip to content

Commit edde7e0

Browse files
Fixed "python image runs with root as the default user"
Added non-root user creation** (`appuser` with UID/GID 1000) - **Set proper file ownership** for all application directories - **Switched container execution** to non-root user with `USER appuser` - **Fixed permission issues** for static files directory
1 parent 36993b6 commit edde7e0

File tree

3 files changed

+25
-9
lines changed

3 files changed

+25
-9
lines changed

Dockerfile

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
FROM python:3.10
2+
3+
# Create non-root user
4+
RUN groupadd --gid 1000 appuser \
5+
&& useradd --uid 1000 --gid appuser --shell /bin/bash --create-home appuser
6+
27
COPY requirements.txt /app/
38
COPY apimanager/ /app/apimanager/
49
COPY static/ /app/static/
@@ -7,6 +12,11 @@ COPY .github/local_settings_container.py /app/apimanager/apimanager/local_settin
712
RUN pip install -r /app/requirements.txt
813
WORKDIR /app
914
RUN ./apimanager/manage.py migrate
15+
16+
# Set proper ownership and switch to non-root user
17+
RUN chown -R appuser:appuser /app
18+
USER appuser
19+
1020
WORKDIR /app/apimanager
1121
EXPOSE 8000
1222
CMD ["gunicorn", "--bind", ":8000", "--config", "../gunicorn.conf.py", "apimanager.wsgi"]

development/Dockerfile.dev

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
FROM python:3.10
22

3+
# Create non-root user
4+
RUN groupadd --gid 1000 appuser \
5+
&& useradd --uid 1000 --gid appuser --shell /bin/bash --create-home appuser
6+
37
# Set environment variables
48
ENV PYTHONDONTWRITEBYTECODE 1
59
ENV PYTHONUNBUFFERED 1
@@ -29,14 +33,20 @@ COPY demo/ /app/demo/
2933
COPY gunicorn.conf.py /app/
3034

3135
# Create necessary directories
32-
RUN mkdir -p /app/logs /app/static /app/db
36+
RUN mkdir -p /app/logs /app/static /app/db /static-collected
3337

34-
# Copy development local settings and entrypoint script to /usr/local/bin
35-
COPY development/local_settings_dev.py /usr/local/bin/local_settings_dev.py
38+
# Copy development local settings directly to the correct location
39+
COPY development/local_settings_dev.py /app/apimanager/apimanager/local_settings.py
40+
# Copy entrypoint script to /usr/local/bin
3641
COPY development/docker-entrypoint-dev.sh /usr/local/bin/docker-entrypoint-dev.sh
3742

38-
# Set proper permissions
39-
RUN chmod +x /app/apimanager/manage.py /usr/local/bin/docker-entrypoint-dev.sh
43+
# Set proper permissions and ownership
44+
RUN chmod +x /app/apimanager/manage.py /usr/local/bin/docker-entrypoint-dev.sh \
45+
&& chown -R appuser:appuser /app \
46+
&& chown -R appuser:appuser /static-collected
47+
48+
# Switch to non-root user
49+
USER appuser
4050

4151
# Expose port
4252
EXPOSE 8000

development/docker-entrypoint-dev.sh

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,6 @@
55

66
set -e
77

8-
# Copy development local settings if it doesn't exist or force override
9-
echo "Setting up development local_settings.py..."
10-
cp /usr/local/bin/local_settings_dev.py /app/apimanager/apimanager/local_settings.py
11-
128
# Wait for database to be ready
139
echo "Waiting for database to be ready..."
1410
while ! pg_isready -h 127.0.0.1 -p 5434 -U apimanager -q; do

0 commit comments

Comments
 (0)