Mars-ViT-demo / Dockerfile
jfang's picture
Update Dockerfile
d63d05e verified
# --- Stage 1: Build frontend production assets ---
FROM node:20-alpine AS frontend-build
WORKDIR /app/frontend
# Copy package files and install node dependencies
COPY frontend/package.json frontend/package-lock.json* ./
RUN npm install
# Copy all frontend source code and build assets
COPY frontend/ .
RUN npm run build
# --- Stage 2: Build and run backend ---
FROM python:3.9-slim
LABEL maintainer="Your Name <your.email@example.com>"
# Install OS packages required by Python and for healthchecks
RUN apt-get update && apt-get install -y curl && rm -rf /var/lib/apt/lists/*
# Create non-root user for security
RUN useradd -m -u 1000 appuser
WORKDIR /app
# Install backend dependencies
COPY backend/requirements.txt ./backend/requirements.txt
RUN pip install --no-cache-dir -r backend/requirements.txt \
&& pip install --no-cache-dir huggingface-hub
# Copy the backend code and data
COPY backend/app ./backend/app
COPY backend/data ./backend/data
# Create embeddings directory and download dataset files
RUN mkdir -p backend/data/embeddings/mars-mae-vit-b-2m \
&& huggingface-cli download jfang/mars-vit-embedding-test samples.db --repo-type dataset --local-dir backend/data/embeddings/mars-mae-vit-b-2m \
&& huggingface-cli download jfang/mars-vit-embedding-test embeddings.index --repo-type dataset --local-dir backend/data/embeddings/mars-mae-vit-b-2m \
&& chown -R appuser:appuser backend/data/embeddings
# Copy the built frontend assets into backend/static
RUN mkdir -p backend/static
COPY --from=frontend-build /app/frontend/dist/ ./backend/static
# Set port as required by HF Spaces
ENV PORT=7860
EXPOSE 7860
# Ensure the files are owned by our non-root user
RUN chown -R appuser:appuser /app
USER appuser
# Start the FastAPI app on port 7860
CMD ["uvicorn", "backend.app.main:app", "--host", "0.0.0.0", "--port", "7860"]