fix: improve dockerfile

This commit is contained in:
Santiago Reig 2025-04-11 23:16:36 +02:00
parent a1bae33e06
commit 0c0b446c07
2 changed files with 85 additions and 25 deletions

View file

@ -1,33 +1,71 @@
FROM alpine:3.15
FROM python:3.13-alpine AS builder
#Base settings
ENV HOME /root
# Set working directory
WORKDIR /app
COPY requirements.txt /root/requirements.txt
# Copy requirements
COPY requirements.txt .
#Install ZeroNet
RUN apk --update --no-cache --no-progress add python3 python3-dev py3-pip gcc g++ autoconf automake libtool libffi-dev musl-dev make tor openssl \
&& pip3 install -r /root/requirements.txt \
&& apk del python3-dev gcc g++ autoconf automake libtool libffi-dev musl-dev make \
&& echo "ControlPort 9051" >> /etc/tor/torrc \
&& echo "CookieAuthentication 1" >> /etc/tor/torrc
# Install build dependencies
RUN apk --no-cache add \
build-base \
git \
autoconf automake libtool \
libffi-dev openssl-dev \
musl-dev
RUN python3 -V \
&& python3 -m pip list \
&& tor --version \
&& openssl version
# Install Python deps
RUN python -m venv /app/venv && \
. /app/venv/bin/activate && \
pip install --upgrade pip && \
pip install -r requirements.txt
#Add Zeronet source
COPY . /root
VOLUME /root/data
# -----------------------------
# Runtime image
FROM python:3.13-alpine
#Control if Tor proxy is started
ENV ENABLE_TOR true
# Create app directory
WORKDIR /app
WORKDIR /root
# Add non-root user
RUN addgroup -S zeronet && adduser -S -G zeronet zeronet
#Set upstart command
CMD (! ${ENABLE_TOR} || tor&) && python3 zeronet.py --ui_ip 0.0.0.0 --fileserver_port 26117
# Install runtime dependencies
RUN apk --no-cache add \
tor tini openssl wget
#Expose ports
EXPOSE 43110 26117
# Configure tor
RUN echo "ControlPort 9051" >> /etc/tor/torrc && \
echo "CookieAuthentication 1" >> /etc/tor/torrc
# Copy from builder
COPY --from=builder /app/venv /app/venv
# Copy application code
COPY --chown=zeronet:zeronet . /app
# Prepare directories
RUN mkdir -p /app/data /app/log && \
chown -R zeronet:zeronet /app/data /app/log && \
chmod 750 /app/data /app/log
# Set environment
ENV PATH="/app/venv/bin:$PATH" \
VIRTUAL_ENV="/app/venv" \
ENABLE_TOR=true \
UI_IP=0.0.0.0 \
UI_PORT=43110 \
FILESERVER_PORT=26117 \
ADDITIONAL_ARGS=""
# Switch to non-root user
USER zeronet
# Use Tini as init to handle signals gracefully
ENTRYPOINT ["/sbin/tini", "--"]
# The command the container runs with
CMD ["sh", "-c", "echo \"Python: $(python -V), Tor: $(tor --version | head -n1)\" && (! ${ENABLE_TOR} || tor&) && python zeronet.py --ui_ip ${UI_IP} --ui_port ${UI_PORT} --fileserver_port ${FILESERVER_PORT} ${ADDITIONAL_ARGS}"]
# Expose ports - using the environment variables
EXPOSE ${UI_PORT} ${FILESERVER_PORT}

22
docker-compose.yml Normal file
View file

@ -0,0 +1,22 @@
services:
zeronet:
build: .
user: zeronet:zeronet
volumes:
- ./data:/app/data
- ./log:/app/log
ports:
- "${UI_PORT:-43110}:${UI_PORT:-43110}"
- "${FILESERVER_PORT:-26117}:${FILESERVER_PORT:-26117}"
environment:
- ENABLE_TOR=true
- UI_IP=0.0.0.0
- UI_PORT=43110
- FILESERVER_PORT=26117
- ADDITIONAL_ARGS=
healthcheck:
test: ["CMD", "wget", "-q", "--spider", "http://localhost:${UI_PORT:-43110}/ZeroNet-Internal/Stats", "||", "exit", "1"]
interval: 1m
timeout: 10s
retries: 3
start_period: 30s