Skip to content

Commit

Permalink
enhancement: Docker image consolidation
Browse files Browse the repository at this point in the history
  • Loading branch information
pluja committed Nov 21, 2023
1 parent 7dc703c commit 8dca6cc
Show file tree
Hide file tree
Showing 12 changed files with 349 additions and 304 deletions.
37 changes: 37 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# General files
*.yml
*.log
*.bat
*.md
*.txt
.env*
Dockerfile
.gitignore
.eslintignore
.eslintrc.js
.prettierrc

# Directories
.github
.git
node_modules
.svelte-kit
__pycache__
.ruff_cache
.venv
.vscode
whisper_models
whishper_data
misc

# Recursive patterns
**/node_modules/
**/__pycache__/
**/.ruff_cache
**/.venv
**/.vscode
**/whisper_models
**/whishper_data
**/.github
**/.git
**/.svelte-kit
76 changes: 76 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
# YT-DLP Download and setup
FROM --platform=$BUILDPLATFORM golang:bookworm AS ytdlp_cache
ARG TARGETOS
ARG TARGETARCH
RUN apt update && apt install -y wget
RUN wget https://github.com/yt-dlp/yt-dlp/releases/latest/download/yt-dlp -O /usr/local/bin/yt-dlp
RUN chmod a+rx /usr/local/bin/yt-dlp

# Backend setup
FROM devopsworks/golang-upx:latest as backend-builder

ENV DEBIAN_FRONTEND noninteractive
WORKDIR /app
COPY ./backend /app
RUN go mod tidy
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o whishper . && \
upx whishper
RUN chmod a+rx whishper

# Frontend setup
FROM node:alpine as frontend
ENV PNPM_HOME="/pnpm"
ENV PATH="$PNPM_HOME:$PATH"
RUN corepack enable
COPY ./frontend /app
WORKDIR /app

FROM frontend AS frontend-prod-deps
RUN --mount=type=cache,id=pnpm,target=/pnpm/store pnpm install --prod --frozen-lockfile

FROM frontend AS frontend-build
RUN --mount=type=cache,id=pnpm,target=/pnpm/store pnpm install --frozen-lockfile
ENV BODY_SIZE_LIMIT=0
RUN pnpm run build

# Base container
FROM python:3.11-slim as base

RUN export DEBIAN_FRONTEND=noninteractive \
&& apt-get -qq update \
&& apt-get -qq install --no-install-recommends \
ffmpeg curl nodejs nginx supervisor \
&& rm -rf /var/lib/apt/lists/*

# Python service setup
COPY ./transcription-api /app/transcription
WORKDIR /app/transcription
RUN pip3 install -r requirements.txt
RUN pip3 install python-multipart

# Node.js service setup
ENV BODY_SIZE_LIMIT=0
COPY ./frontend /app/frontend
COPY --from=frontend-build /app/build /app/frontend
COPY --from=frontend-prod-deps /app/node_modules /app/frontend/node_modules

# Golang service setup
COPY --from=backend-builder /app/whishper /bin/whishper
RUN chmod a+rx /bin/whishper
COPY --from=ytdlp_cache /usr/local/bin/yt-dlp /bin/yt-dlp

# Nginx setup
COPY ./nginx.conf /etc/nginx/nginx.conf

# Set workdir and entrypoint
WORKDIR /app
RUN mkdir /app/uploads

# Cleanup to make the image smaller
RUN apt-get clean && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* /usr/share/doc/* ~/.cache /var/cache

COPY ./supervisord.conf /etc/supervisor/conf.d/supervisord.conf
ENTRYPOINT ["supervisord", "-c", "/etc/supervisor/conf.d/supervisord.conf"]

# Expose ports for each service and Nginx
EXPOSE 8080 3000 5000 80
94 changes: 94 additions & 0 deletions Dockerfile.gpu
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
# YT-DLP Download and setup
FROM --platform=$BUILDPLATFORM golang:bookworm AS ytdlp_cache
ARG TARGETOS
ARG TARGETARCH
RUN apt update && apt install -y wget
RUN wget https://github.com/yt-dlp/yt-dlp/releases/latest/download/yt-dlp -O /usr/local/bin/yt-dlp
RUN chmod a+rx /usr/local/bin/yt-dlp

# Backend setup
FROM devopsworks/golang-upx:latest as backend-builder

ENV DEBIAN_FRONTEND noninteractive
WORKDIR /app
COPY ./backend /app
RUN go mod tidy
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o whishper . && \
upx whishper
RUN chmod a+rx whishper

# Frontend setup
FROM node:alpine as frontend
ENV PNPM_HOME="/pnpm"
ENV PATH="$PNPM_HOME:$PATH"
RUN corepack enable
COPY ./frontend /app
WORKDIR /app

FROM frontend AS frontend-prod-deps
RUN --mount=type=cache,id=pnpm,target=/pnpm/store pnpm install --prod --frozen-lockfile

FROM frontend AS frontend-build
RUN --mount=type=cache,id=pnpm,target=/pnpm/store pnpm install --frozen-lockfile
ENV BODY_SIZE_LIMIT=0
RUN pnpm run build

# Base container
FROM nvidia/cuda:11.8.0-cudnn8-runtime-ubuntu22.04 as base

ENV PYTHON_VERSION=3.10

RUN export DEBIAN_FRONTEND=noninteractive \
&& apt-get -qq update \
&& apt-get -qq install --no-install-recommends \
python${PYTHON_VERSION} \
python${PYTHON_VERSION}-venv \
python3-pip \
ffmpeg \
curl wget xz-utils \
ffmpeg nginx supervisor \
&& rm -rf /var/lib/apt/lists/*

RUN ln -s -f /usr/bin/python${PYTHON_VERSION} /usr/bin/python3 && \
ln -s -f /usr/bin/python${PYTHON_VERSION} /usr/bin/python && \
ln -s -f /usr/bin/pip3 /usr/bin/pip

# Python service setup
COPY ./transcription-api /app/transcription
WORKDIR /app/transcription
RUN pip install -r requirements.txt && \
pip install python-multipart && \
pip3 install torch --index-url https://download.pytorch.org/whl/cu118

# Node.js service setup
RUN wget https://nodejs.org/dist/v20.9.0/node-v20.9.0-linux-x64.tar.xz && \
tar -xf node-v20.9.0-linux-x64.tar.xz && \
mv node-v20.9.0-linux-x64 /usr/local/lib/node && \
rm node-v20.9.0-linux-x64.tar.xz
ENV PATH="/usr/local/lib/node/bin:${PATH}"

ENV BODY_SIZE_LIMIT=0
COPY ./frontend /app/frontend
COPY --from=frontend-build /app/build /app/frontend
COPY --from=frontend-prod-deps /app/node_modules /app/frontend/node_modules

# Golang service setup
COPY --from=backend-builder /app/whishper /bin/whishper
RUN chmod a+rx /bin/whishper
COPY --from=ytdlp_cache /usr/local/bin/yt-dlp /bin/yt-dlp

# Nginx setup
COPY ./nginx.conf /etc/nginx/nginx.conf

# Set workdir and entrypoint
WORKDIR /app
RUN mkdir /app/uploads

# Cleanup to make the image smaller
RUN apt-get clean && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* /usr/share/doc/* ~/.cache /var/cache

COPY ./supervisord.conf /etc/supervisor/conf.d/supervisord.conf
ENTRYPOINT ["supervisord", "-c", "/etc/supervisor/conf.d/supervisord.conf"]

# Expose ports for each service and Nginx
EXPOSE 8080 3000 5000 80
1 change: 0 additions & 1 deletion backend/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ ARG TARGETARCH
WORKDIR /app
COPY --from=builder /app/whishper ./whishper
RUN chmod a+rx ./whishper

COPY --from=ytdlp_cache /usr/local/bin/yt-dlp /bin/yt-dlp

RUN mkdir /app/uploads
Expand Down
3 changes: 2 additions & 1 deletion backend/database/mongo.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ func NewMongoDb() *MongoDb {
}

// Set auth method to PLAIN if using FerretDB
if os.Getenv("FERRETDB_MONGO_URL") != "" {
if os.Getenv("FERRETDB_ENABLED") != "" {
log.Printf("FerretDB enabled, setting auth mechanism to PLAIN...")
credentials.AuthMechanism = "PLAIN"
}

Expand Down
2 changes: 1 addition & 1 deletion backend/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func main() {

listenAddr := flag.String("addr", ":8080", "server listen address")
uploadDir := flag.String("updir", "/app/uploads", "upload directory")
asrEndpoint := flag.String("asr", "transcription-api:8000", "asr endpoint, i.e. localhost:9888")
asrEndpoint := flag.String("asr", "127.0.0.1:8000", "asr endpoint, i.e. localhost:9888")
dbHost := flag.String("db", "mongo:27017", "database endpoint host, i.e. localhost:27017")
dbUser := flag.String("dbuser", "root", "database user")
dbPass := flag.String("dbpass", "example", "database password")
Expand Down
31 changes: 6 additions & 25 deletions docker-compose.dev.yml
Original file line number Diff line number Diff line change
@@ -1,33 +1,14 @@
version: '3.9'

services:
transcription-api-gpu:
build:
context: ./transcription-api
dockerfile: Dockerfile.gpu
services:
translate-cpu:
ports:
- 8000:8000

transcription-api-cpu:
build:
context: ./transcription-api
dockerfile: Dockerfile
ports:
- 8000:8000

translate:
- 5000:5000

translate-gpu:
ports:
- 5000:5000

mongo:
ports:
- 27017:27017

#whishper-backend:
# build: ./backend
# ports:
# - 8080:8080

#whishper-frontend:
# build:
# context: ./frontend
- 27017:27017
Loading

0 comments on commit 8dca6cc

Please sign in to comment.