-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDockerfile
More file actions
291 lines (263 loc) · 7.48 KB
/
Dockerfile
File metadata and controls
291 lines (263 loc) · 7.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
# FFmpeg CUDA Docker Image with Production API
# 100% Working for RTX 4090 + WSL2 + Windows 11
# Production-ready with Gunicorn and comprehensive API
ARG CUDA=12.3.1
ARG OS=ubuntu22.04
ARG BUILDIMAGE=${CUDA}-devel-${OS}
ARG RUNIMAGE=${CUDA}-runtime-${OS}
FROM nvidia/cuda:${BUILDIMAGE} AS builder
ARG CUDA
ARG OS
# Set environment variables for CUDA and build
ENV DEBIAN_FRONTEND=noninteractive
ENV NVIDIA_VISIBLE_DEVICES=all
ENV NVIDIA_DRIVER_CAPABILITIES=compute,utility,video
ENV PATH="/usr/local/cuda/bin:${PATH}"
ENV LD_LIBRARY_PATH="/usr/local/cuda/lib64:${LD_LIBRARY_PATH}"
ENV PKG_CONFIG_PATH="/usr/local/lib/pkgconfig:/usr/lib/x86_64-linux-gnu/pkgconfig"
# Update system and install essential build dependencies
RUN apt-get update && \
apt-get -y --allow-change-held-packages full-upgrade && \
apt-get -qqy install \
automake \
autoconf \
bc \
build-essential \
cmake \
curl \
cython3 \
devscripts \
equivs \
git \
imagemagick \
intltool \
pkg-config \
python3-dev \
python3-pip \
unzip \
wget \
yasm \
nasm \
libtool \
&& rm -rf /var/lib/apt/lists/*
# Install core media libraries (essential for FFmpeg)
RUN apt-get update && \
apt-get -qqy install \
libaom-dev \
libass-dev \
libcodec2-dev \
libdav1d-dev \
libfdk-aac-dev \
libfreetype6-dev \
libfribidi-dev \
libfontconfig1-dev \
libgsm1-dev \
libmp3lame-dev \
libopencore-amrnb-dev \
libopencore-amrwb-dev \
libopenh264-dev \
libopenjp2-7-dev \
libopus-dev \
libspeex-dev \
libtheora-dev \
libtwolame-dev \
libvorbis-dev \
libvpx-dev \
libwebp-dev \
libx264-dev \
libx265-dev \
libxvidcore-dev \
&& rm -rf /var/lib/apt/lists/*
# Install additional video processing libraries
RUN apt-get update && \
apt-get -qqy install \
libvidstab-dev \
librubberband-dev \
libsoxr-dev \
libzimg-dev \
libzmq3-dev \
libzvbi-dev \
frei0r-plugins-dev \
ladspa-sdk \
libcaca-dev \
libpulse-dev \
librtmp-dev \
libshine-dev \
libsrt-gnutls-dev \
libssh-dev \
libtesseract-dev \
&& rm -rf /var/lib/apt/lists/*
# Install NVIDIA-specific libraries (NO VAAPI - NVIDIA only!)
RUN apt-get update && \
apt-get -qqy install \
libvdpau-dev \
&& rm -rf /var/lib/apt/lists/*
# Set FFmpeg and codec versions (tested stable combination)
ARG FFMPEG_VERSION=6.1.1
ARG NVCODEC_HEADERS_VERSION=12.1.14.0
# Create source directories and download sources
RUN rm -rf /opt/src/* && \
mkdir -p /opt/src/nv-codec-headers /opt/src/ffmpeg && \
git clone --depth 1 --branch n${NVCODEC_HEADERS_VERSION} https://git.videolan.org/git/ffmpeg/nv-codec-headers.git /opt/src/nv-codec-headers && \
git clone --depth 1 --branch n${FFMPEG_VERSION} https://git.ffmpeg.org/ffmpeg.git /opt/src/ffmpeg
# Install NVIDIA codec headers (essential for NVENC/NVDEC)
RUN cd /opt/src/nv-codec-headers && \
make && \
make install
# Configure FFmpeg with NVIDIA-only acceleration (NO VAAPI!)
# This configuration is specifically optimized for RTX 4090 + WSL2
RUN cd /opt/src/ffmpeg && \
./configure \
--prefix=/usr/local \
--enable-gpl \
--enable-nonfree \
--enable-version3 \
--enable-shared \
--disable-static \
--enable-cuda-nvcc \
--enable-cuvid \
--enable-nvenc \
--enable-nvdec \
--enable-vdpau \
--disable-vaapi \
--enable-libass \
--enable-libaom \
--enable-libcodec2 \
--enable-libdav1d \
--enable-libfdk-aac \
--enable-libfreetype \
--enable-libfribidi \
--enable-libfontconfig \
--enable-libgsm \
--enable-libmp3lame \
--enable-libopencore-amrnb \
--enable-libopencore-amrwb \
--enable-libopenh264 \
--enable-libopenjpeg \
--enable-libopus \
--enable-libspeex \
--enable-libtheora \
--enable-libtwolame \
--enable-libvidstab \
--enable-libvorbis \
--enable-libvpx \
--enable-libwebp \
--enable-libx264 \
--enable-libx265 \
--enable-libxvid \
--enable-libzimg \
--enable-libzmq \
--enable-libzvbi \
--enable-frei0r \
--enable-ladspa \
--enable-libcaca \
--enable-libpulse \
--enable-librtmp \
--enable-librubberband \
--enable-libshine \
--enable-libsoxr \
--enable-libsrt \
--enable-libssh \
--enable-libtesseract \
--extra-cflags="-I/usr/local/cuda/include -I/usr/local/include" \
--extra-ldflags="-L/usr/local/cuda/lib64 -L/usr/local/lib" \
--extra-libs="-lpthread -lm -lz"
# Compile FFmpeg (using all available cores for faster build)
RUN cd /opt/src/ffmpeg && \
make -j$(nproc)
# Install FFmpeg
RUN cd /opt/src/ffmpeg && \
make install && \
ldconfig
# Create runtime image (smaller final image)
FROM nvidia/cuda:${RUNIMAGE} AS runtime
# Set environment variables for runtime
ENV NVIDIA_VISIBLE_DEVICES=all
ENV NVIDIA_DRIVER_CAPABILITIES=compute,utility,video
ENV PATH="/usr/local/bin:/usr/local/cuda/bin:${PATH}"
ENV LD_LIBRARY_PATH="/usr/local/lib:/usr/local/cuda/lib64:${LD_LIBRARY_PATH}"
# Install runtime dependencies (only what's needed to run)
RUN apt-get update && \
apt-get -qqy install \
libass9 \
libaom3 \
libcodec2-1.0 \
libdav1d5 \
libfdk-aac2 \
libfreetype6 \
libfribidi0 \
libfontconfig1 \
libgsm1 \
libmp3lame0 \
libopencore-amrnb0 \
libopencore-amrwb0 \
libopenh264-6 \
libopenjp2-7 \
libopus0 \
libspeex1 \
libtheora0 \
libtwolame0 \
libvidstab1.1 \
libvorbis0a \
libvorbisenc2 \
libvpx7 \
libwebp7 \
libx264-163 \
libx265-199 \
libxvidcore4 \
libzimg2 \
libzmq5 \
libzvbi0 \
frei0r-plugins \
libcaca0 \
libpulse0 \
librtmp1 \
librubberband2 \
libshine3 \
libsoxr0 \
libsrt1.4-gnutls \
libssh-4 \
libtesseract4 \
libvdpau1 \
python3 \
python3-pip \
&& rm -rf /var/lib/apt/lists/*
# Copy FFmpeg binaries and libraries from builder
COPY --from=builder /usr/local/bin/ff* /usr/local/bin/
COPY --from=builder /usr/local/lib/lib* /usr/local/lib/
COPY --from=builder /usr/local/include/libav* /usr/local/include/
COPY --from=builder /usr/local/include/libsw* /usr/local/include/
COPY --from=builder /usr/local/lib/pkgconfig/libav* /usr/local/lib/pkgconfig/
COPY --from=builder /usr/local/lib/pkgconfig/libsw* /usr/local/lib/pkgconfig/
# Update library cache
RUN echo "/usr/local/lib" > /etc/ld.so.conf.d/ffmpeg.conf && \
echo "/usr/local/cuda/lib64" > /etc/ld.so.conf.d/cuda.conf && \
ldconfig
# Install Python packages for production API
RUN pip3 install flask gunicorn
# Create non-root user for security
RUN useradd -ms /bin/bash ffmpeguser
# Copy API files (these will be created separately)
COPY ffmpeg_api.py /home/ffmpeguser/
COPY start_api.sh /home/ffmpeguser/
COPY gunicorn.conf.py /home/ffmpeguser/
# Set proper ownership and permissions
RUN chown -R ffmpeguser:ffmpeguser /home/ffmpeguser && \
chmod +x /home/ffmpeguser/start_api.sh
# Switch to non-root user
USER ffmpeguser
WORKDIR /home/ffmpeguser
# Verify FFmpeg installation
RUN ffmpeg -version && \
echo "=== Hardware Accelerators ===" && \
ffmpeg -hwaccels && \
echo "=== NVENC Encoders ===" && \
ffmpeg -encoders | grep -i nvenc && \
echo "=== ✅ FFmpeg CUDA API Ready! ==="
# Expose port
EXPOSE 5000
# Health check
HEALTHCHECK --interval=30s --timeout=10s --start-period=5s --retries=3 \
CMD curl -f http://localhost:5000/health || exit 1
# Start the production API
CMD ["/home/ffmpeguser/start_api.sh"]