Spaces:
Sleeping
Sleeping
Update Dockerfile
Browse files- Dockerfile +33 -29
Dockerfile
CHANGED
|
@@ -1,47 +1,50 @@
|
|
| 1 |
FROM python:3.11.11-slim-bullseye
|
| 2 |
|
| 3 |
-
# Install system dependencies
|
| 4 |
RUN apt-get update -qq \
|
| 5 |
-
&& apt-get -qqq install --no-install-recommends -y
|
|
|
|
| 6 |
&& apt-get clean \
|
| 7 |
&& rm -rf /var/lib/apt/lists/*
|
| 8 |
|
| 9 |
-
# Create non-root user
|
| 10 |
-
|
| 11 |
-
|
|
|
|
| 12 |
|
| 13 |
# Set working directory
|
| 14 |
WORKDIR /app
|
| 15 |
|
| 16 |
-
#
|
| 17 |
-
RUN
|
| 18 |
-
|
| 19 |
-
# Upgrade pip
|
| 20 |
-
RUN ./venv/bin/pip install --upgrade pip
|
| 21 |
-
|
| 22 |
-
# Clone LibreTranslate repository
|
| 23 |
-
RUN apt-get update -qq \
|
| 24 |
-
&& apt-get install -y git \
|
| 25 |
-
&& git clone https://github.com/LibreTranslate/LibreTranslate.git . \
|
| 26 |
&& apt-get remove -y git \
|
| 27 |
&& apt-get autoremove -y \
|
| 28 |
&& apt-get clean \
|
| 29 |
&& rm -rf /var/lib/apt/lists/*
|
| 30 |
|
| 31 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
RUN ./venv/bin/pip install Babel==2.12.1 \
|
| 33 |
&& ./venv/bin/python scripts/compile_locales.py \
|
| 34 |
&& ./venv/bin/pip install torch==2.2.0 --extra-index-url https://download.pytorch.org/whl/cpu \
|
| 35 |
&& ./venv/bin/pip install "numpy<2" \
|
| 36 |
-
&& ./venv/bin/pip install . \
|
| 37 |
&& ./venv/bin/pip install gunicorn \
|
| 38 |
&& ./venv/bin/pip cache purge
|
| 39 |
|
| 40 |
-
# Create
|
| 41 |
-
RUN mkdir -p /app/db /app/logs /tmp/prometheus_data \
|
| 42 |
-
&& chown -R libretranslate:libretranslate /app /tmp/prometheus_data
|
| 43 |
-
|
| 44 |
-
# Copy wsgi.py file (if not in the repo)
|
| 45 |
RUN echo 'from app.main import create_app\n\
|
| 46 |
\n\
|
| 47 |
def app(*args, **kwargs):\n\
|
|
@@ -54,18 +57,19 @@ def app(*args, **kwargs):\n\
|
|
| 54 |
setattr(args, k, v)\n\
|
| 55 |
return create_app(args)' > /app/wsgi.py
|
| 56 |
|
| 57 |
-
# Switch to non-root user
|
| 58 |
-
USER libretranslate
|
| 59 |
-
|
| 60 |
# Set environment variables
|
| 61 |
ENV PROMETHEUS_MULTIPROC_DIR=/tmp/prometheus_data
|
| 62 |
-
ENV
|
| 63 |
-
ENV
|
| 64 |
|
| 65 |
-
# Expose port
|
| 66 |
EXPOSE 7860
|
| 67 |
|
| 68 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
CMD ["./venv/bin/gunicorn", \
|
| 70 |
"--workers", "3", \
|
| 71 |
"--bind", "0.0.0.0:7860", \
|
|
|
|
| 1 |
FROM python:3.11.11-slim-bullseye
|
| 2 |
|
| 3 |
+
# Install system dependencies as root
|
| 4 |
RUN apt-get update -qq \
|
| 5 |
+
&& apt-get -qqq install --no-install-recommends -y \
|
| 6 |
+
pkg-config gcc g++ git curl \
|
| 7 |
&& apt-get clean \
|
| 8 |
&& rm -rf /var/lib/apt/lists/*
|
| 9 |
|
| 10 |
+
# Create non-root user with specific UID/GID
|
| 11 |
+
# Using UID/GID 1000 which is common for the first user
|
| 12 |
+
RUN groupadd -r -g 1000 libretranslate \
|
| 13 |
+
&& useradd -r -u 1000 -g 1000 -m -d /home/libretranslate -s /bin/bash libretranslate
|
| 14 |
|
| 15 |
# Set working directory
|
| 16 |
WORKDIR /app
|
| 17 |
|
| 18 |
+
# Clone repository as root, then change ownership
|
| 19 |
+
RUN git clone https://github.com/LibreTranslate/LibreTranslate.git . \
|
| 20 |
+
&& chown -R libretranslate:libretranslate /app \
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
&& apt-get remove -y git \
|
| 22 |
&& apt-get autoremove -y \
|
| 23 |
&& apt-get clean \
|
| 24 |
&& rm -rf /var/lib/apt/lists/*
|
| 25 |
|
| 26 |
+
# Create necessary directories with proper permissions
|
| 27 |
+
RUN mkdir -p /app/db /app/logs /tmp/prometheus_data \
|
| 28 |
+
&& chown -R libretranslate:libretranslate /app /tmp/prometheus_data \
|
| 29 |
+
&& chmod 755 /app /app/db /app/logs /tmp/prometheus_data
|
| 30 |
+
|
| 31 |
+
# Switch to non-root user for all subsequent operations
|
| 32 |
+
USER libretranslate
|
| 33 |
+
|
| 34 |
+
# Create virtual environment as non-root user
|
| 35 |
+
RUN python -m venv venv \
|
| 36 |
+
&& ./venv/bin/pip install --upgrade pip wheel
|
| 37 |
+
|
| 38 |
+
# Install Python dependencies as non-root user
|
| 39 |
RUN ./venv/bin/pip install Babel==2.12.1 \
|
| 40 |
&& ./venv/bin/python scripts/compile_locales.py \
|
| 41 |
&& ./venv/bin/pip install torch==2.2.0 --extra-index-url https://download.pytorch.org/whl/cpu \
|
| 42 |
&& ./venv/bin/pip install "numpy<2" \
|
| 43 |
+
&& ./venv/bin/pip install -e . \
|
| 44 |
&& ./venv/bin/pip install gunicorn \
|
| 45 |
&& ./venv/bin/pip cache purge
|
| 46 |
|
| 47 |
+
# Create wsgi.py as non-root user
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
RUN echo 'from app.main import create_app\n\
|
| 49 |
\n\
|
| 50 |
def app(*args, **kwargs):\n\
|
|
|
|
| 57 |
setattr(args, k, v)\n\
|
| 58 |
return create_app(args)' > /app/wsgi.py
|
| 59 |
|
|
|
|
|
|
|
|
|
|
| 60 |
# Set environment variables
|
| 61 |
ENV PROMETHEUS_MULTIPROC_DIR=/tmp/prometheus_data
|
| 62 |
+
ENV HOME=/home/libretranslate
|
| 63 |
+
ENV USER=libretranslate
|
| 64 |
|
| 65 |
+
# Expose port (as non-root, can only bind to ports > 1024)
|
| 66 |
EXPOSE 7860
|
| 67 |
|
| 68 |
+
# Health check running as non-root
|
| 69 |
+
HEALTHCHECK --interval=30s --timeout=3s --start-period=40s --retries=3 \
|
| 70 |
+
CMD ./venv/bin/python -c "import urllib.request; urllib.request.urlopen('http://localhost:7860/languages').read()"
|
| 71 |
+
|
| 72 |
+
# Run gunicorn as non-root user
|
| 73 |
CMD ["./venv/bin/gunicorn", \
|
| 74 |
"--workers", "3", \
|
| 75 |
"--bind", "0.0.0.0:7860", \
|