Как изменить пользователя root на пользовательского пользователя в DockerfilePython

Программы на Python
Anonymous
Как изменить пользователя root на пользовательского пользователя в Dockerfile

Сообщение Anonymous »

Я пытался сделать всех пользователей в моем файле Dockerfile пользовательскими, так как при запуске Collectstatic в моем приложении Django я получаю сообщение об ошибке [Errno 13] Разрешение отклонено: '/code/static/admin/ js/vendor/select2/i18n/pl.6031b4f16452.js.gz'. Я также хочу сделать это из соображений безопасности.
В настоящее время, когда я запускаю >docker-compose exec web ls -l /code/static, я получаю

Код: Выделить всё

total 16
drwxrwxrwx 1 root root  4096 Apr  5 05:42 admin
drwxrwxrwx 1 root root  4096 Sep 18 21:21 css
drwxrwxrwx 1 root root  4096 Sep 18 21:21 human
drwxrwxrwx 1 root root  4096 Sep 18 18:42 img
-rw-r--r-- 1 1234 1234 13091 Sep 18 21:21 staticfiles.json
drwxrwxrwx 1 root root  4096 Sep 18 21:21 transcribe
Вот мой Dockerfile:

Код: Выделить всё

# Pull base image
FROM python:3.11.4-slim-bullseye

# Set environment variables
ENV PIP_NO_CACHE_DIR off
ENV PIP_DISABLE_PIP_VERSION_CHECK 1
ENV PYTHONUNBUFFERED 1
ENV PYTHONDONTWRITEBYTECODE 1
ENV COLUMNS 80

#install Debian and other dependencies that are required to run python apps(eg. git, python-magic).
RUN apt-get update \
&& apt-get install -y --force-yes python3-pip ffmpeg git libmagic-dev libpq-dev gcc \
&& rm -rf /var/lib/apt/lists/*

# Set working directory for Docker image
WORKDIR /code/

# Install dependencies
COPY requirements.txt .
RUN pip install -r requirements.txt

# Copy project
COPY . .

# Create a custom non-root user
RUN useradd -m user-mani

# Grant necessary permissions to write directories and to user 'celery-user'
RUN mkdir -p /code/media /code/static && \
chown -R user-mani:user-mani /code/media /code/static

# Switch to the non-root user. All this avoids running Celery with root/superuser priviledges which is a security risk
USER user-mani

Всякий раз, когда я переупорядочиваю свой Dockerfile в соответствии с примерами передового опыта Docker и собираю свой образ, я получаю успешную сборку, но также и несколько сообщений об ошибках (я прикрепил их как изображение).Ошибка сборки 1

Подробнее здесь: https://stackoverflow.com/questions/790 ... dockerfile

Вернуться в «Python»