Dockerfile 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. # docker/Dockerfile
  2. # IMPORTANT: The docker images do not contain the checkpoints. You need to mount the checkpoints to the container.
  3. # Build the image:
  4. # docker build \
  5. # --platform linux/amd64 \
  6. # -f docker/Dockerfile \
  7. # --build-arg BACKEND=[cuda, cpu] \
  8. # --target [webui, server] \
  9. # -t fish-speech-[webui, server]:[cuda, cpu] .
  10. # e.g. for building the webui:
  11. # docker build \
  12. # --platform linux/amd64 \
  13. # -f docker/Dockerfile \
  14. # --build-arg BACKEND=cuda \
  15. # --target webui \
  16. # -t fish-speech-webui:cuda .
  17. # e.g. for building the server:
  18. # docker build \
  19. # --platform linux/amd64 \
  20. # -f docker/Dockerfile \
  21. # --build-arg BACKEND=cuda \
  22. # --target server \
  23. # -t fish-speech-server:cuda .
  24. # Multi-platform build:
  25. # docker buildx build \
  26. # --platform linux/amd64,linux/arm64 \
  27. # -f docker/Dockerfile \
  28. # --build-arg BACKEND=cpu \
  29. # --target webui \
  30. # -t fish-speech-webui:cpu .
  31. # Running the image interactively:
  32. # docker run \
  33. # --gpus all \
  34. # -v /path/to/fish-speech/checkpoints:/app/checkpoints \
  35. # -e COMPILE=1 \ ... or -e COMPILE=0 \
  36. # -it fish-speech-[webui, server]:[cuda, cpu]
  37. # E.g. running the webui:
  38. # docker run \
  39. # --gpus all \
  40. # -v ./checkpoints:/app/checkpoints \
  41. # -e COMPILE=1 \
  42. # -p 7860:7860 \
  43. # fish-speech-webui:cuda
  44. # E.g. running the server:
  45. # docker run \
  46. # --gpus all \
  47. # -v ./checkpoints:/app/checkpoints \
  48. # -p 8080:8080 \
  49. # -it fish-speech-server:cuda
  50. # Select the specific cuda version (see https://hub.docker.com/r/nvidia/cuda/)
  51. ARG CUDA_VER=12.9.0
  52. # Adapt the uv extra to fit the cuda version (one of [cu126, cu128, cu129])
  53. ARG UV_EXTRA=cu129
  54. ARG BACKEND=cuda
  55. ARG UBUNTU_VER=24.04
  56. ARG PY_VER=3.12
  57. ARG UV_VERSION=0.8.15
  58. # Create non-root user early for security
  59. ARG USERNAME=fish
  60. ARG USER_UID=1000
  61. ARG USER_GID=1000
  62. ##############################################################
  63. # Base stage per backend
  64. ##############################################################
  65. # --- CUDA (x86_64) ---
  66. FROM nvidia/cuda:${CUDA_VER}-cudnn-runtime-ubuntu${UBUNTU_VER} AS base-cuda
  67. ENV DEBIAN_FRONTEND=noninteractive
  68. # Install system dependencies in a single layer with cleanup
  69. RUN --mount=type=cache,target=/var/cache/apt,sharing=locked \
  70. --mount=type=cache,target=/var/lib/apt,sharing=locked \
  71. set -eux \
  72. && rm -f /etc/apt/apt.conf.d/docker-clean \
  73. && echo 'Binary::apt::APT::Keep-Downloaded-Packages "true";' >/etc/apt/apt.conf.d/keep-cache \
  74. && apt-get update \
  75. && apt-get install -y --no-install-recommends \
  76. python3-pip \
  77. python3-dev \
  78. git \
  79. ca-certificates \
  80. curl \
  81. && apt-get clean \
  82. && rm -rf /var/lib/apt/lists/*
  83. # --- CPU-only (portable x86_64) ---
  84. FROM python:${PY_VER}-slim AS base-cpu
  85. ENV UV_EXTRA=cpu
  86. # Install system dependencies in a single layer with cleanup
  87. RUN --mount=type=cache,target=/var/cache/apt,sharing=locked \
  88. --mount=type=cache,target=/var/lib/apt,sharing=locked \
  89. set -eux \
  90. && rm -f /etc/apt/apt.conf.d/docker-clean \
  91. && echo 'Binary::apt::APT::Keep-Downloaded-Packages "true";' >/etc/apt/apt.conf.d/keep-cache \
  92. && apt-get update \
  93. && apt-get install -y --no-install-recommends \
  94. git \
  95. ca-certificates \
  96. curl \
  97. && apt-get clean \
  98. && rm -rf /var/lib/apt/lists/*
  99. ##############################################################
  100. # UV stage
  101. ##############################################################
  102. ARG UV_VERSION
  103. FROM ghcr.io/astral-sh/uv:${UV_VERSION} AS uv-bin
  104. ##############################################################
  105. # Shared app base stage
  106. ##############################################################
  107. FROM base-${BACKEND} AS app-base
  108. ARG PY_VER
  109. ARG BACKEND
  110. ARG USERNAME
  111. ARG USER_UID
  112. ARG USER_GID
  113. ARG UV_VERSION
  114. ARG UV_EXTRA
  115. ENV BACKEND=${BACKEND} \
  116. DEBIAN_FRONTEND=noninteractive \
  117. PYTHONDONTWRITEBYTECODE=1 \
  118. PYTHONUNBUFFERED=1 \
  119. UV_HTTP_TIMEOUT=3600 \
  120. UV_INDEX_URL=http://mirrors.aliyun.com/pypi/simple/ \
  121. UV_DEFAULT_INDEX=http://mirrors.aliyun.com/pypi/simple/ \
  122. PYTORCH_CUDA_ALLOC_CONF=expandable_segments:True
  123. # System dependencies for audio processing
  124. ARG DEPENDENCIES=" \
  125. libsox-dev \
  126. build-essential \
  127. cmake \
  128. libasound-dev \
  129. portaudio19-dev \
  130. libportaudio2 \
  131. libportaudiocpp0 \
  132. ffmpeg"
  133. # Install system dependencies with caching and cleanup
  134. RUN --mount=type=cache,target=/var/cache/apt,sharing=locked \
  135. --mount=type=cache,target=/var/lib/apt,sharing=locked \
  136. set -eux \
  137. && rm -f /etc/apt/apt.conf.d/docker-clean \
  138. && echo 'Binary::apt::APT::Keep-Downloaded-Packages "true";' >/etc/apt/apt.conf.d/keep-cache \
  139. && apt-get update \
  140. && apt-get install -y --no-install-recommends ${DEPENDENCIES} \
  141. && apt-get clean \
  142. && rm -rf /var/lib/apt/lists/*
  143. # Install specific uv version
  144. COPY --from=uv-bin /uv /uvx /bin/
  145. # RUN groupadd --gid ${USER_GID} ${USERNAME} \
  146. # && useradd --uid ${USER_UID} --gid ${USER_GID} -m ${USERNAME} \
  147. # && mkdir -p /app /home/${USERNAME}/.cache \
  148. # && chown -R ${USERNAME}:${USERNAME} /app /home/${USERNAME}/.cache
  149. # Create non-root user (or use existing user)
  150. RUN set -eux; \
  151. if getent group ${USER_GID} >/dev/null 2>&1; then \
  152. echo "Group ${USER_GID} already exists"; \
  153. else \
  154. groupadd -g ${USER_GID} ${USERNAME}; \
  155. fi; \
  156. if id -u ${USER_UID} >/dev/null 2>&1; then \
  157. echo "User ${USER_UID} already exists, using existing user"; \
  158. EXISTING_USER=$(id -un ${USER_UID}); \
  159. mkdir -p /app /home/${EXISTING_USER}/.cache; \
  160. chown -R ${USER_UID}:${USER_GID} /app /home/${EXISTING_USER}/.cache; \
  161. else \
  162. useradd -m -u ${USER_UID} -g ${USER_GID} ${USERNAME}; \
  163. mkdir -p /app /home/${USERNAME}/.cache; \
  164. chown -R ${USERNAME}:${USERNAME} /app /home/${USERNAME}/.cache; \
  165. fi
  166. # Create references directory with proper permissions for the non-root user
  167. RUN mkdir -p /app/references \
  168. && chown -R ${USER_UID}:${USER_GID} /app/references \
  169. && chmod 755 /app/references
  170. # Set working directory
  171. WORKDIR /app
  172. # Copy dependency files first for better caching
  173. COPY --chown=${USER_UID}:${USER_GID} pyproject.toml uv.lock README.md ./
  174. # Switch to non-root user for package installation
  175. USER ${USER_UID}:${USER_GID}
  176. # Install Python dependencies (cacheable by lockfiles)
  177. # Use a generic cache path that works regardless of username
  178. RUN --mount=type=cache,target=/tmp/uv-cache,uid=${USER_UID},gid=${USER_GID} \
  179. uv python pin ${PY_VER} \
  180. && uv sync -vv --extra ${UV_EXTRA} --frozen --no-install-project
  181. # Copy application code
  182. COPY --chown=${USER_UID}:${USER_GID} . .
  183. # Install the local package after copying source code
  184. RUN uv sync -vv --extra ${UV_EXTRA} --frozen --no-build-isolation
  185. # Create common entrypoint script
  186. RUN printf '%s\n' \
  187. '#!/bin/bash' \
  188. 'set -euo pipefail' \
  189. '' \
  190. '# Set user info from build args' \
  191. 'USER_UID='${USER_UID} \
  192. 'USER_GID='${USER_GID} \
  193. '' \
  194. '# Logging function' \
  195. 'log() { echo "[$(date +"%Y-%m-%d %H:%M:%S")] $*" >&2; }' \
  196. '' \
  197. '# Validate environment' \
  198. 'validate_env() {' \
  199. ' if [ ! -d "/app/checkpoints" ]; then' \
  200. ' log "WARNING: /app/checkpoints directory not found. Please mount your checkpoints."' \
  201. ' fi' \
  202. ' if [ ! -d "/app/references" ]; then' \
  203. ' log "WARNING: /app/references directory not found. Please mount your references."' \
  204. ' else' \
  205. ' # Check if we can write to references directory' \
  206. ' if [ ! -w "/app/references" ]; then' \
  207. ' log "ERROR: Cannot write to /app/references directory. Please ensure the mounted directory has proper permissions for user with UID ${USER_UID}."' \
  208. ' log "You can fix this by running: sudo chown -R ${USER_UID}:${USER_GID} /path/to/your/references"' \
  209. ' exit 1' \
  210. ' fi' \
  211. ' fi' \
  212. '}' \
  213. '' \
  214. '# Build device arguments' \
  215. 'build_device_args() {' \
  216. ' if [ "${BACKEND:-}" = "cpu" ]; then' \
  217. ' echo "--device cpu"' \
  218. ' fi' \
  219. '}' \
  220. '' \
  221. '# Build compile arguments' \
  222. 'build_compile_args() {' \
  223. ' if [ "${1:-}" = "compile" ] || [ "${COMPILE:-}" = "1" ] || [ "${COMPILE:-}" = "true" ]; then' \
  224. ' echo "--compile"' \
  225. ' shift' \
  226. ' fi' \
  227. ' echo "$@"' \
  228. '}' \
  229. '' \
  230. '# Health check function' \
  231. 'health_check() {' \
  232. ' local port=${1:-7860}' \
  233. ' local endpoint=${2:-/health}' \
  234. ' curl -f http://localhost:${port}${endpoint} 2>/dev/null || exit 1' \
  235. '}' \
  236. > /app/common.sh && chmod +x /app/common.sh
  237. ##############################################################
  238. # App stages
  239. ##############################################################
  240. # Gradio WebUI
  241. FROM app-base AS webui
  242. ENV PYTHONDONTWRITEBYTECODE=1 PYTHONUNBUFFERED=1
  243. ARG GRADIO_SERVER_NAME="0.0.0.0"
  244. ARG GRADIO_SERVER_PORT=7860
  245. ARG LLAMA_CHECKPOINT_PATH="checkpoints/s2-pro"
  246. ARG DECODER_CHECKPOINT_PATH="checkpoints/s2-pro/codec.pth"
  247. ARG DECODER_CONFIG_NAME="modded_dac_vq"
  248. # Expose port
  249. EXPOSE ${GRADIO_SERVER_PORT}
  250. # Set environment variables
  251. ENV GRADIO_SERVER_NAME=${GRADIO_SERVER_NAME}
  252. ENV GRADIO_SERVER_PORT=${GRADIO_SERVER_PORT}
  253. ENV LLAMA_CHECKPOINT_PATH=${LLAMA_CHECKPOINT_PATH}
  254. ENV DECODER_CHECKPOINT_PATH=${DECODER_CHECKPOINT_PATH}
  255. ENV DECODER_CONFIG_NAME=${DECODER_CONFIG_NAME}
  256. # Create webui entrypoint
  257. RUN printf '%s\n' \
  258. '#!/bin/bash' \
  259. 'source /app/common.sh' \
  260. '' \
  261. 'log "Starting Fish Speech WebUI..."' \
  262. 'validate_env' \
  263. '' \
  264. 'DEVICE_ARGS=$(build_device_args)' \
  265. 'COMPILE_ARGS=$(build_compile_args "$@")' \
  266. '' \
  267. 'log "Device args: ${DEVICE_ARGS:-none}"' \
  268. 'log "Compile args: ${COMPILE_ARGS}"' \
  269. 'log "Server: ${GRADIO_SERVER_NAME}:${GRADIO_SERVER_PORT}"' \
  270. '' \
  271. 'exec uv -v run tools/run_webui.py \' \
  272. ' --llama-checkpoint-path "${LLAMA_CHECKPOINT_PATH}" \' \
  273. ' --decoder-checkpoint-path "${DECODER_CHECKPOINT_PATH}" \' \
  274. ' --decoder-config-name "${DECODER_CONFIG_NAME}" \' \
  275. ' ${DEVICE_ARGS} ${COMPILE_ARGS}' \
  276. > /app/start_webui.sh && chmod +x /app/start_webui.sh
  277. # Health check
  278. HEALTHCHECK --interval=30s --timeout=10s --start-period=60s --retries=3 \
  279. CMD curl -f http://localhost:${GRADIO_SERVER_PORT}/health || exit 1
  280. ENTRYPOINT ["/app/start_webui.sh"]
  281. # API Server
  282. FROM app-base AS server
  283. ENV PYTHONDONTWRITEBYTECODE=1 PYTHONUNBUFFERED=1
  284. ARG API_SERVER_NAME="0.0.0.0"
  285. ARG API_SERVER_PORT=8080
  286. ARG LLAMA_CHECKPOINT_PATH="checkpoints/s2-pro"
  287. ARG DECODER_CHECKPOINT_PATH="checkpoints/s2-pro/codec.pth"
  288. ARG DECODER_CONFIG_NAME="modded_dac_vq"
  289. # Expose port
  290. EXPOSE ${API_SERVER_PORT}
  291. # Set environment variables
  292. ENV API_SERVER_NAME=${API_SERVER_NAME}
  293. ENV API_SERVER_PORT=${API_SERVER_PORT}
  294. ENV LLAMA_CHECKPOINT_PATH=${LLAMA_CHECKPOINT_PATH}
  295. ENV DECODER_CHECKPOINT_PATH=${DECODER_CHECKPOINT_PATH}
  296. ENV DECODER_CONFIG_NAME=${DECODER_CONFIG_NAME}
  297. # Create server entrypoint
  298. RUN printf '%s\n' \
  299. '#!/bin/bash' \
  300. 'source /app/common.sh' \
  301. '' \
  302. 'log "Starting Fish Speech API Server..."' \
  303. 'validate_env' \
  304. '' \
  305. 'DEVICE_ARGS=$(build_device_args)' \
  306. 'COMPILE_ARGS=$(build_compile_args "$@")' \
  307. '' \
  308. 'log "Device args: ${DEVICE_ARGS:-none}"' \
  309. 'log "Compile args: ${COMPILE_ARGS}"' \
  310. 'log "Server: ${API_SERVER_NAME}:${API_SERVER_PORT}"' \
  311. '' \
  312. 'exec uv run tools/api_server.py \' \
  313. ' --listen "${API_SERVER_NAME}:${API_SERVER_PORT}" \' \
  314. ' --llama-checkpoint-path "${LLAMA_CHECKPOINT_PATH}" \' \
  315. ' --decoder-checkpoint-path "${DECODER_CHECKPOINT_PATH}" \' \
  316. ' --decoder-config-name "${DECODER_CONFIG_NAME}" \' \
  317. ' ${DEVICE_ARGS} ${COMPILE_ARGS}' \
  318. > /app/start_server.sh && chmod +x /app/start_server.sh
  319. # Health check
  320. HEALTHCHECK --interval=30s --timeout=10s --start-period=60s --retries=3 \
  321. CMD curl -f http://localhost:${API_SERVER_PORT}/v1/health || exit 1
  322. ENTRYPOINT ["/app/start_server.sh"]
  323. # Development stage
  324. FROM app-base AS dev
  325. USER root
  326. # Install development tools
  327. RUN --mount=type=cache,target=/var/cache/apt,sharing=locked \
  328. --mount=type=cache,target=/var/lib/apt,sharing=locked \
  329. apt-get update \
  330. && apt-get install -y --no-install-recommends \
  331. vim \
  332. htop \
  333. strace \
  334. gdb \
  335. && apt-get clean \
  336. && rm -rf /var/lib/apt/lists/*
  337. USER ${USER_UID}:${USER_GID}
  338. # Install development dependencies
  339. RUN uv sync -vv --extra ${UV_EXTRA} --dev --no-build-isolation
  340. # Default to bash for development
  341. ENTRYPOINT ["/bin/bash"]