Dockerfile 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  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. # System dependencies for audio processing
  122. ARG DEPENDENCIES=" \
  123. libsox-dev \
  124. build-essential \
  125. cmake \
  126. libasound-dev \
  127. portaudio19-dev \
  128. libportaudio2 \
  129. libportaudiocpp0 \
  130. ffmpeg"
  131. # Install system dependencies with caching and cleanup
  132. RUN --mount=type=cache,target=/var/cache/apt,sharing=locked \
  133. --mount=type=cache,target=/var/lib/apt,sharing=locked \
  134. set -eux \
  135. && rm -f /etc/apt/apt.conf.d/docker-clean \
  136. && echo 'Binary::apt::APT::Keep-Downloaded-Packages "true";' >/etc/apt/apt.conf.d/keep-cache \
  137. && apt-get update \
  138. && apt-get install -y --no-install-recommends ${DEPENDENCIES} \
  139. && apt-get clean \
  140. && rm -rf /var/lib/apt/lists/*
  141. # Install specific uv version
  142. COPY --from=uv-bin /uv /uvx /bin/
  143. # RUN groupadd --gid ${USER_GID} ${USERNAME} \
  144. # && useradd --uid ${USER_UID} --gid ${USER_GID} -m ${USERNAME} \
  145. # && mkdir -p /app /home/${USERNAME}/.cache \
  146. # && chown -R ${USERNAME}:${USERNAME} /app /home/${USERNAME}/.cache
  147. # Create non-root user (or use existing user)
  148. RUN set -eux; \
  149. if getent group ${USER_GID} >/dev/null 2>&1; then \
  150. echo "Group ${USER_GID} already exists"; \
  151. else \
  152. groupadd -g ${USER_GID} ${USERNAME}; \
  153. fi; \
  154. if id -u ${USER_UID} >/dev/null 2>&1; then \
  155. echo "User ${USER_UID} already exists, using existing user"; \
  156. EXISTING_USER=$(id -un ${USER_UID}); \
  157. mkdir -p /app /home/${EXISTING_USER}/.cache; \
  158. chown -R ${USER_UID}:${USER_GID} /app /home/${EXISTING_USER}/.cache; \
  159. else \
  160. useradd -m -u ${USER_UID} -g ${USER_GID} ${USERNAME}; \
  161. mkdir -p /app /home/${USERNAME}/.cache; \
  162. chown -R ${USERNAME}:${USERNAME} /app /home/${USERNAME}/.cache; \
  163. fi
  164. # Create references directory with proper permissions for the non-root user
  165. RUN mkdir -p /app/references \
  166. && chown -R ${USER_UID}:${USER_GID} /app/references \
  167. && chmod 755 /app/references
  168. # Set working directory
  169. WORKDIR /app
  170. # Copy dependency files first for better caching
  171. COPY --chown=${USER_UID}:${USER_GID} pyproject.toml uv.lock README.md ./
  172. # Switch to non-root user for package installation
  173. USER ${USER_UID}:${USER_GID}
  174. # Install Python dependencies (cacheable by lockfiles)
  175. # Use a generic cache path that works regardless of username
  176. RUN --mount=type=cache,target=/tmp/uv-cache,uid=${USER_UID},gid=${USER_GID} \
  177. uv python pin ${PY_VER} \
  178. && uv sync -vv --extra ${UV_EXTRA} --frozen --no-install-project
  179. # Copy application code
  180. COPY --chown=${USER_UID}:${USER_GID} . .
  181. # Install the local package after copying source code
  182. RUN uv sync -vv --extra ${UV_EXTRA} --frozen --no-build-isolation
  183. # Create common entrypoint script
  184. RUN printf '%s\n' \
  185. '#!/bin/bash' \
  186. 'set -euo pipefail' \
  187. '' \
  188. '# Set user info from build args' \
  189. 'USER_UID='${USER_UID} \
  190. 'USER_GID='${USER_GID} \
  191. '' \
  192. '# Logging function' \
  193. 'log() { echo "[$(date +"%Y-%m-%d %H:%M:%S")] $*" >&2; }' \
  194. '' \
  195. '# Validate environment' \
  196. 'validate_env() {' \
  197. ' if [ ! -d "/app/checkpoints" ]; then' \
  198. ' log "WARNING: /app/checkpoints directory not found. Please mount your checkpoints."' \
  199. ' fi' \
  200. ' if [ ! -d "/app/references" ]; then' \
  201. ' log "WARNING: /app/references directory not found. Please mount your references."' \
  202. ' else' \
  203. ' # Check if we can write to references directory' \
  204. ' if [ ! -w "/app/references" ]; then' \
  205. ' log "ERROR: Cannot write to /app/references directory. Please ensure the mounted directory has proper permissions for user with UID ${USER_UID}."' \
  206. ' log "You can fix this by running: sudo chown -R ${USER_UID}:${USER_GID} /path/to/your/references"' \
  207. ' exit 1' \
  208. ' fi' \
  209. ' fi' \
  210. '}' \
  211. '' \
  212. '# Build device arguments' \
  213. 'build_device_args() {' \
  214. ' if [ "${BACKEND:-}" = "cpu" ]; then' \
  215. ' echo "--device cpu"' \
  216. ' fi' \
  217. '}' \
  218. '' \
  219. '# Build compile arguments' \
  220. 'build_compile_args() {' \
  221. ' if [ "${1:-}" = "compile" ] || [ "${COMPILE:-}" = "1" ] || [ "${COMPILE:-}" = "true" ]; then' \
  222. ' echo "--compile"' \
  223. ' shift' \
  224. ' fi' \
  225. ' echo "$@"' \
  226. '}' \
  227. '' \
  228. '# Health check function' \
  229. 'health_check() {' \
  230. ' local port=${1:-7860}' \
  231. ' local endpoint=${2:-/health}' \
  232. ' curl -f http://localhost:${port}${endpoint} 2>/dev/null || exit 1' \
  233. '}' \
  234. > /app/common.sh && chmod +x /app/common.sh
  235. ##############################################################
  236. # App stages
  237. ##############################################################
  238. # Gradio WebUI
  239. FROM app-base AS webui
  240. ENV PYTHONDONTWRITEBYTECODE=1 PYTHONUNBUFFERED=1
  241. ARG GRADIO_SERVER_NAME="0.0.0.0"
  242. ARG GRADIO_SERVER_PORT=7860
  243. ARG LLAMA_CHECKPOINT_PATH="checkpoints/s2-pro"
  244. ARG DECODER_CHECKPOINT_PATH="checkpoints/s2-pro/codec.pth"
  245. ARG DECODER_CONFIG_NAME="modded_dac_vq"
  246. # Expose port
  247. EXPOSE ${GRADIO_SERVER_PORT}
  248. # Set environment variables
  249. ENV GRADIO_SERVER_NAME=${GRADIO_SERVER_NAME}
  250. ENV GRADIO_SERVER_PORT=${GRADIO_SERVER_PORT}
  251. ENV LLAMA_CHECKPOINT_PATH=${LLAMA_CHECKPOINT_PATH}
  252. ENV DECODER_CHECKPOINT_PATH=${DECODER_CHECKPOINT_PATH}
  253. ENV DECODER_CONFIG_NAME=${DECODER_CONFIG_NAME}
  254. # Create webui entrypoint
  255. RUN printf '%s\n' \
  256. '#!/bin/bash' \
  257. 'source /app/common.sh' \
  258. '' \
  259. 'log "Starting Fish Speech WebUI..."' \
  260. 'validate_env' \
  261. '' \
  262. 'DEVICE_ARGS=$(build_device_args)' \
  263. 'COMPILE_ARGS=$(build_compile_args "$@")' \
  264. '' \
  265. 'log "Device args: ${DEVICE_ARGS:-none}"' \
  266. 'log "Compile args: ${COMPILE_ARGS}"' \
  267. 'log "Server: ${GRADIO_SERVER_NAME}:${GRADIO_SERVER_PORT}"' \
  268. '' \
  269. 'exec uv run tools/run_webui.py \' \
  270. ' --llama-checkpoint-path "${LLAMA_CHECKPOINT_PATH}" \' \
  271. ' --decoder-checkpoint-path "${DECODER_CHECKPOINT_PATH}" \' \
  272. ' --decoder-config-name "${DECODER_CONFIG_NAME}" \' \
  273. ' ${DEVICE_ARGS} ${COMPILE_ARGS}' \
  274. > /app/start_webui.sh && chmod +x /app/start_webui.sh
  275. # Health check
  276. HEALTHCHECK --interval=30s --timeout=10s --start-period=60s --retries=3 \
  277. CMD curl -f http://localhost:${GRADIO_SERVER_PORT}/health || exit 1
  278. ENTRYPOINT ["/app/start_webui.sh"]
  279. # API Server
  280. FROM app-base AS server
  281. ENV PYTHONDONTWRITEBYTECODE=1 PYTHONUNBUFFERED=1
  282. ARG API_SERVER_NAME="0.0.0.0"
  283. ARG API_SERVER_PORT=8080
  284. ARG LLAMA_CHECKPOINT_PATH="checkpoints/s2-pro"
  285. ARG DECODER_CHECKPOINT_PATH="checkpoints/s2-pro/codec.pth"
  286. ARG DECODER_CONFIG_NAME="modded_dac_vq"
  287. # Expose port
  288. EXPOSE ${API_SERVER_PORT}
  289. # Set environment variables
  290. ENV API_SERVER_NAME=${API_SERVER_NAME}
  291. ENV API_SERVER_PORT=${API_SERVER_PORT}
  292. ENV LLAMA_CHECKPOINT_PATH=${LLAMA_CHECKPOINT_PATH}
  293. ENV DECODER_CHECKPOINT_PATH=${DECODER_CHECKPOINT_PATH}
  294. ENV DECODER_CONFIG_NAME=${DECODER_CONFIG_NAME}
  295. # Create server entrypoint
  296. RUN printf '%s\n' \
  297. '#!/bin/bash' \
  298. 'source /app/common.sh' \
  299. '' \
  300. 'log "Starting Fish Speech API Server..."' \
  301. 'validate_env' \
  302. '' \
  303. 'DEVICE_ARGS=$(build_device_args)' \
  304. 'COMPILE_ARGS=$(build_compile_args "$@")' \
  305. '' \
  306. 'log "Device args: ${DEVICE_ARGS:-none}"' \
  307. 'log "Compile args: ${COMPILE_ARGS}"' \
  308. 'log "Server: ${API_SERVER_NAME}:${API_SERVER_PORT}"' \
  309. '' \
  310. 'exec uv run tools/api_server.py \' \
  311. ' --listen "${API_SERVER_NAME}:${API_SERVER_PORT}" \' \
  312. ' --llama-checkpoint-path "${LLAMA_CHECKPOINT_PATH}" \' \
  313. ' --decoder-checkpoint-path "${DECODER_CHECKPOINT_PATH}" \' \
  314. ' --decoder-config-name "${DECODER_CONFIG_NAME}" \' \
  315. ' ${DEVICE_ARGS} ${COMPILE_ARGS}' \
  316. > /app/start_server.sh && chmod +x /app/start_server.sh
  317. # Health check
  318. HEALTHCHECK --interval=30s --timeout=10s --start-period=60s --retries=3 \
  319. CMD curl -f http://localhost:${API_SERVER_PORT}/v1/health || exit 1
  320. ENTRYPOINT ["/app/start_server.sh"]
  321. # Development stage
  322. FROM app-base AS dev
  323. USER root
  324. # Install development tools
  325. RUN --mount=type=cache,target=/var/cache/apt,sharing=locked \
  326. --mount=type=cache,target=/var/lib/apt,sharing=locked \
  327. apt-get update \
  328. && apt-get install -y --no-install-recommends \
  329. vim \
  330. htop \
  331. strace \
  332. gdb \
  333. && apt-get clean \
  334. && rm -rf /var/lib/apt/lists/*
  335. USER ${USER_UID}:${USER_GID}
  336. # Install development dependencies
  337. RUN uv sync -vv --extra ${UV_EXTRA} --dev --no-build-isolation
  338. # Default to bash for development
  339. ENTRYPOINT ["/bin/bash"]