Fix: Resolve backend Docker build and runtime errors

This commit addresses two primary issues in the backend Docker setup:

1.  **Database Initialization Error (CGO_ENABLED):**
    The Go binary was previously compiled with `CGO_ENABLED=0`. This caused an error ("Binary was compiled with 'CGO_ENABLED=0', go-sqlite3 requires cgo to work") because `go-sqlite3` requires CGo.
    - Modified `Dockerfile.backend` to set `CGO_ENABLED=1` during the build.
    - Added `gcc`, `musl-dev`, and `sqlite-dev` to the builder stage dependencies for CGo compilation on Alpine.
    - Added `sqlite` to the final image stage for runtime library availability.

2.  **fail2ban-client Not Found Error:**
    The `DockerEntrypoint.sh` script attempted to start `fail2ban-client` without ensuring its presence in the image.
    - Added `fail2ban` to the `apk add` command in the final stage of `Dockerfile.backend`.
    - Updated `DockerEntrypoint.sh` to check if `fail2ban-client` is available before attempting to start it, preventing errors if it's not found (e.g., if `XUI_ENABLE_FAIL2BAN` is true but installation failed).

These changes should allow the backend container to build and start correctly, resolving the reported database and fail2ban errors. You will need to run `docker compose up -d --build` to apply these changes.
This commit is contained in:
google-labs-jules[bot] 2025-06-05 07:33:02 +00:00
parent 386081fba4
commit c971912ae3
2 changed files with 10 additions and 2 deletions

View file

@ -1,7 +1,13 @@
#!/bin/sh #!/bin/sh
# Start fail2ban # Start fail2ban
[ $XUI_ENABLE_FAIL2BAN == "true" ] && fail2ban-client -x start if [ "$XUI_ENABLE_FAIL2BAN" = "true" ]; then
if command -v fail2ban-client >/dev/null 2>&1; then
fail2ban-client -x start
else
echo "Warning: fail2ban-client not found, but XUI_ENABLE_FAIL2BAN is true."
fi
fi
# Run x-ui # Run x-ui
exec /app/x-ui exec /app/x-ui

View file

@ -5,6 +5,7 @@ WORKDIR /app
# Copy go.mod and go.sum and download dependencies # Copy go.mod and go.sum and download dependencies
COPY go.mod go.sum ./ COPY go.mod go.sum ./
RUN apk add --no-cache gcc musl-dev sqlite-dev
RUN go mod download RUN go mod download
# Copy the rest of the application source code # Copy the rest of the application source code
@ -15,7 +16,7 @@ COPY . .
# The original entrypoint seems to be related to x-ui.sh or DockerEntrypoint.sh # The original entrypoint seems to be related to x-ui.sh or DockerEntrypoint.sh
# We need to ensure the binary is built correctly. # We need to ensure the binary is built correctly.
# For 3x-ui, the main.go seems to be the entry point. # For 3x-ui, the main.go seems to be the entry point.
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o /app/x-ui main.go RUN CGO_ENABLED=1 GOOS=linux go build -a -installsuffix cgo -o /app/x-ui main.go
# Stage 2: Production environment # Stage 2: Production environment
FROM alpine:latest FROM alpine:latest
@ -33,6 +34,7 @@ COPY --from=builder /app/config/version /app/config/version
# Ensure necessary directories exist and have correct permissions if needed by the app # Ensure necessary directories exist and have correct permissions if needed by the app
# The original compose file mounts $PWD/db/:/etc/x-ui/ and $PWD/cert/:/root/cert/ # The original compose file mounts $PWD/db/:/etc/x-ui/ and $PWD/cert/:/root/cert/
# So, these paths should be available or created by the entrypoint script. # So, these paths should be available or created by the entrypoint script.
RUN apk add --no-cache sqlite fail2ban
RUN mkdir -p /etc/x-ui && \ RUN mkdir -p /etc/x-ui && \
mkdir -p /root/cert && \ mkdir -p /root/cert && \
chmod +x /app/x-ui.sh /app/DockerEntrypoint.sh /app/x-ui chmod +x /app/x-ui.sh /app/DockerEntrypoint.sh /app/x-ui