Dockerfile 12 KB

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