Skip to content

Commit 3517c13

Browse files
committed
feat: rewrite Dockerfiles and update README.md for efficiency
- Removed the compress stage from Dockerfile and instead utilized a pre-compressed ffmpeg package from ghcr.io repository. - Updated various Dockerfiles to use yt-dlp version 2024.04.09 from the previous version. - Switched all Dockerfiles to `ghcr.io/jim60105/static-ffmpeg-upx:7.0-1` for `ffmpeg`, `ffprobe`, and `dumb-init`, previously it used `mwader/static-ffmpeg`. - Reordered some sections in `README.md`. - Added image description and metadata labels to Dockerfiles. - In Dockerfiles, added more cleanup steps after installing packages using pip to reduce the image size. - Changed directory permissions in Dockerfiles and added `/licenses` for license files. - In Dockerfiles, replaced binary files effective permissions to 775 from 774. Signed-off-by: 陳鈞 <[email protected]>
1 parent 6ac7ebc commit 3517c13

File tree

5 files changed

+191
-94
lines changed

5 files changed

+191
-94
lines changed

Dockerfile

Lines changed: 3 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3,32 +3,6 @@ ARG UID=1001
33
ARG VERSION=EDGE
44
ARG RELEASE=0
55

6-
########################################
7-
# Compress stage
8-
########################################
9-
FROM alpine:3 as compress
10-
11-
# RUN mount cache for multi-arch: https://github.com/docker/buildx/issues/549#issuecomment-1788297892
12-
ARG TARGETARCH
13-
ARG TARGETVARIANT
14-
15-
# Compress ffmpeg and ffprobe
16-
# UPX skip small files: https://github.com/upx/upx/blob/5bef96806860382395d9681f3b0c69e0f7e853cf/src/p_unix.cpp#L80
17-
# UPX skip large files: https://github.com/upx/upx/blob/b0dc48316516d236664dfc5f1eb5f2de00fc0799/src/conf.h#L134
18-
RUN --mount=type=cache,id=apk-$TARGETARCH$TARGETVARIANT,sharing=locked,target=/var/cache/apk \
19-
--mount=from=mwader/static-ffmpeg:7.0-1,source=/ffmpeg,target=/tmp/ffmpeg,rw \
20-
--mount=from=mwader/static-ffmpeg:7.0-1,source=/ffprobe,target=/tmp/ffprobe,rw \
21-
apk update && apk add -u \
22-
-X "https://dl-cdn.alpinelinux.org/alpine/edge/community" \
23-
# Use upx to compress the ffmpeg binary
24-
upx dumb-init && \
25-
cp /tmp/ffmpeg / && \
26-
cp /tmp/ffprobe / && \
27-
upx --best --lzma /ffmpeg || true; \
28-
upx --best --lzma /ffprobe || true; \
29-
upx --best --lzma /usr/bin/dumb-init || true; \
30-
apk del upx
31-
326
########################################
337
# Final stage
348
########################################
@@ -51,9 +25,9 @@ COPY --link --chown=$UID:0 --chmod=775 LICENSE /licenses/Dockerfile.LICENSE
5125
COPY --link --chown=$UID:0 --chmod=775 yt-dlp/LICENSE /licenses/yt-dlp.LICENSE
5226

5327
RUN --mount=type=cache,id=apk-$TARGETARCH$TARGETVARIANT,sharing=locked,target=/var/cache/apk \
54-
--mount=from=compress,source=/ffmpeg,target=/ffmpeg,rw \
55-
--mount=from=compress,source=/ffprobe,target=/ffprobe,rw \
56-
--mount=from=compress,source=/usr/bin/dumb-init,target=/dumb-init,rw \
28+
--mount=from=ghcr.io/jim60105/static-ffmpeg-upx:7.0-1,source=/ffmpeg,target=/ffmpeg,rw \
29+
--mount=from=ghcr.io/jim60105/static-ffmpeg-upx:7.0-1,source=/ffprobe,target=/ffprobe,rw \
30+
--mount=from=ghcr.io/jim60105/static-ffmpeg-upx:7.0-1,source=/dumb-init,target=/dumb-init,rw \
5731
apk update && apk add -u \
5832
# These branches follows the yt-dlp release
5933
-X "https://dl-cdn.alpinelinux.org/alpine/edge/main" \

README.md

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,11 @@ It is important to note that the [Dockerfile](Dockerfile) always builds with [th
4040

4141
> [!NOTE]
4242
>
43-
> - The apk edge branch follows the latest release of yt-dlp.
44-
> - The `alpine.Dockerfile` installs yt-dlp from pip source, so the image size may slightly different compared to the `Dockerfile` even when they have the same version.
43+
> - The apk edge branch follows the latest release of yt-dlp.
44+
> - The `alpine.Dockerfile` installs yt-dlp from pip source, so the image size may slightly different compared to the `Dockerfile` even when they have the same version.
4545
4646
### Build Command
4747

48-
> [!NOTE]
49-
> If you are using an earlier version of the docker client, it is necessary to [enable the BuildKit mode](https://docs.docker.com/build/buildkit/#getting-started) when building the image. This is because I used the `COPY --link` feature which enhances the build performance and was introduced in Buildx v0.8.
50-
> With the Docker Engine 23.0 and Docker Desktop 4.19, Buildx has become the default build client. So you won't have to worry about this when using the latest version.
51-
5248
```bash
5349
docker build -t yt-dlp .
5450
docker build --build-arg BUILD_VERSION=2023.12.30 -f ./alpine.Dockerfile -t yt-dlp:alpine .
@@ -59,24 +55,17 @@ docker build --build-arg BUILD_VERSION=2023.12.30 -f ./distroless.Dockerfile -t
5955
> [!TIP]
6056
> I've notice that that both the UBI version and the Distroless version offer no advantages over the Alpine version. So _**please use the Alpine version**_ unless you have specific reasons not to. All of these base images are great, some of them were simply not that suitable for our project.
6157
58+
> [!NOTE]
59+
> If you are using an earlier version of the docker client, it is necessary to [enable the BuildKit mode](https://docs.docker.com/build/buildkit/#getting-started) when building the image. This is because I used the `COPY --link` feature which enhances the build performance and was introduced in Buildx v0.8.
60+
> With the Docker Engine 23.0 and Docker Desktop 4.19, Buildx has become the default build client. So you won't have to worry about this when using the latest version.
61+
6262
## LICENSE
6363

6464
> [!NOTE]
6565
> The main program, [yt-dlp/yt-dlp](https://github.com/yt-dlp/yt-dlp), is distributed under [Unlicense license](https://github.com/yt-dlp/yt-dlp/blob/master/LICENSE).
6666
> Please consult their repository for access to the source code and licenses.
6767
> The following is the license for the Dockerfiles and CI workflows in this repository.
6868
69-
> [!CAUTION]
70-
> A GPLv3 licensed Dockerfile means that you _**MUST**_ **distribute the source code with the same license**, if you
71-
>
72-
> - Re-distribute the image. (You can simply point to this GitHub repository if you doesn't made any code changes.)
73-
> - Distribute a image that uses code from this repository.
74-
> - Or **distribute a image based on this image**. (`FROM ghcr.io/jim60105/yt-dlp` in your Dockerfile)
75-
>
76-
> "Distribute" means to make the image available for other people to download, usually by pushing it to a public registry. If you are solely using it for your personal purposes, this has no impact on you.
77-
>
78-
> Please consult the [LICENSE](LICENSE) for more details.
79-
8069
<img src="https://github.com/jim60105/docker-yt-dlp/assets/16995691/f33f8175-af23-4a8a-ad69-efd17a7625f4" alt="gplv3" width="300" />
8170

8271
[GNU GENERAL PUBLIC LICENSE Version 3](LICENSE)
@@ -86,3 +75,14 @@ This program is free software: you can redistribute it and/or modify it under th
8675
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
8776

8877
You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.
78+
79+
> [!CAUTION]
80+
> A GPLv3 licensed Dockerfile means that you _**MUST**_ **distribute the source code with the same license**, if you
81+
>
82+
> - Re-distribute the image. (You can simply point to this GitHub repository if you doesn't made any code changes.)
83+
> - Distribute a image that uses code from this repository.
84+
> - Or **distribute a image based on this image**. (`FROM ghcr.io/jim60105/yt-dlp` in your Dockerfile)
85+
>
86+
> "Distribute" means to make the image available for other people to download, usually by pushing it to a public registry. If you are solely using it for your personal purposes, this has no impact on you.
87+
>
88+
> Please consult the [LICENSE](LICENSE) for more details.

alpine.Dockerfile

Lines changed: 53 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,94 @@
11
# syntax=docker/dockerfile:1
22
ARG UID=1001
3-
ARG BUILD_VERSION=2023.12.30
3+
ARG VERSION=2024.04.09
4+
ARG RELEASE=0
45

6+
########################################
7+
# Build stage
8+
########################################
59
FROM python:3.12-alpine as build
610

711
# RUN mount cache for multi-arch: https://github.com/docker/buildx/issues/549#issuecomment-1788297892
812
ARG TARGETARCH
913
ARG TARGETVARIANT
1014

11-
ARG BUILD_VERSION
12-
1315
WORKDIR /app
1416

1517
# Install under /root/.local
1618
ENV PIP_USER="true"
1719
ARG PIP_NO_WARN_SCRIPT_LOCATION=0
1820
ARG PIP_ROOT_USER_ACTION="ignore"
21+
ARG PIP_NO_COMPILE="true"
22+
ARG PIP_DISABLE_PIP_VERSION_CHECK="true"
1923

24+
ARG VERSION
2025
RUN --mount=type=cache,id=pip-$TARGETARCH$TARGETVARIANT,sharing=locked,target=/root/.cache/pip \
21-
pip3.12 install yt-dlp==$BUILD_VERSION && \
26+
pip3.12 install -U --force-reinstall pip setuptools wheel && \
27+
pip3.12 install yt-dlp==$VERSION && \
2228
# Cleanup
2329
find "/root/.local" -name '*.pyc' -print0 | xargs -0 rm -f || true ; \
2430
find "/root/.local" -type d -name '__pycache__' -print0 | xargs -0 rm -rf || true ;
2531

32+
########################################
33+
# Final stage
34+
########################################
2635
FROM python:3.12-alpine as final
2736

28-
ARG UID
29-
3037
RUN pip3.12 uninstall -y setuptools pip wheel && \
3138
rm -rf /root/.cache/pip
3239

33-
# Use dumb-init to handle signals
34-
RUN apk add -u --no-cache dumb-init
40+
# Create user
41+
ARG UID
42+
RUN adduser -H -g "" -D $UID -u $UID -G root
3543

36-
# ffmpeg (6.1 is broken, so override it)
37-
COPY --link --from=mwader/static-ffmpeg:6.1.1 /ffmpeg /usr/bin/
38-
COPY --link --from=mwader/static-ffmpeg:6.1.1 /ffprobe /usr/bin/
44+
# Create directories with correct permissions
45+
RUN install -d -m 775 -o $UID -g 0 /download && \
46+
install -d -m 775 -o $UID -g 0 /licenses
3947

40-
# Create user
41-
RUN addgroup -g $UID $UID && \
42-
adduser -g "" -D $UID -u $UID -G $UID
48+
# ffmpeg
49+
COPY --link --from=ghcr.io/jim60105/static-ffmpeg-upx:7.0-1 /ffmpeg /usr/bin/
50+
COPY --link --from=ghcr.io/jim60105/static-ffmpeg-upx:7.0-1 /ffprobe /usr/bin/
51+
52+
# dumb-init
53+
COPY --link --from=ghcr.io/jim60105/static-ffmpeg-upx:7.0-1 /dumb-init /usr/bin/
54+
55+
# Copy licenses (OpenShift Policy)
56+
COPY --link --chown=$UID:0 --chmod=775 LICENSE /licenses/Dockerfile.LICENSE
57+
COPY --link --chown=$UID:0 --chmod=775 yt-dlp/LICENSE /licenses/yt-dlp.LICENSE
4358

4459
# Copy dist and support arbitrary user ids (OpenShift best practice)
4560
# https://docs.openshift.com/container-platform/4.14/openshift_images/create-images.html#use-uid_create-images
46-
COPY --chown=$UID:0 --chmod=774 \
47-
--from=build /root/.local /home/$UID/.local
61+
COPY --link --chown=$UID:0 --chmod=775 --from=build /root/.local /home/$UID/.local
62+
4863
ENV PATH="/home/$UID/.local/bin:$PATH"
4964

5065
# Remove these to prevent the container from executing arbitrary commands
5166
RUN rm /bin/echo /bin/ln /bin/rm /bin/sh
5267

53-
# Run as non-root user
54-
USER $UID
5568
WORKDIR /download
69+
5670
VOLUME [ "/download" ]
5771

72+
USER $UID
73+
5874
STOPSIGNAL SIGINT
75+
76+
# Use dumb-init as PID 1 to handle signals properly
5977
ENTRYPOINT [ "dumb-init", "--", "yt-dlp", "--no-cache-dir" ]
60-
CMD ["--help"]
78+
CMD ["--help"]
79+
80+
ARG VERSION
81+
ARG RELEASE
82+
LABEL name="jim60105/docker-yt-dlp" \
83+
# Authors for yt-dlp
84+
vendor="yt-dlp" \
85+
# Maintainer for this docker image
86+
maintainer="jim60105" \
87+
# Dockerfile source repository
88+
url="https://github.com/jim60105/docker-yt-dlp" \
89+
version=${VERSION} \
90+
# This should be a number, incremented with each change
91+
release=${RELEASE} \
92+
io.k8s.display-name="yt-dlp" \
93+
summary="yt-dlp: A feature-rich command-line audio/video downloader." \
94+
description="yt-dlp is a feature-rich command-line audio/video downloader with support for thousands of sites. The project is a fork of youtube-dl based on the now inactive youtube-dlc. For more information about this tool, please visit the following website: https://github.com/yt-dlp/yt-dlp"

distroless.Dockerfile

Lines changed: 55 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,88 @@
11
# syntax=docker/dockerfile:1
2-
ARG BUILD_VERSION=2023.12.30
2+
ARG VERSION=2024.04.09
3+
ARG RELEASE=0
34

5+
########################################
6+
# Build stage
7+
########################################
48
FROM python:3.12-bookworm as build
59

610
# RUN mount cache for multi-arch: https://github.com/docker/buildx/issues/549#issuecomment-1788297892
711
ARG TARGETARCH
812
ARG TARGETVARIANT
913

10-
ARG BUILD_VERSION
11-
1214
WORKDIR /app
1315

1416
# Install under /root/.local
1517
ENV PIP_USER="true"
1618
ARG PIP_NO_WARN_SCRIPT_LOCATION=0
1719
ARG PIP_ROOT_USER_ACTION="ignore"
20+
ARG PIP_NO_COMPILE="true"
21+
ARG PIP_DISABLE_PIP_VERSION_CHECK="true"
1822

23+
ARG VERSION
1924
RUN --mount=type=cache,id=pip-$TARGETARCH$TARGETVARIANT,sharing=locked,target=/root/.cache/pip \
20-
pip3.12 install dumb-init yt-dlp==$BUILD_VERSION && \
25+
pip install -U --force-reinstall pip setuptools wheel && \
26+
pip install yt-dlp==$VERSION && \
2127
# Cleanup
2228
find "/root/.local" -name '*.pyc' -print0 | xargs -0 rm -f || true ; \
23-
find "/root/.local" -type d -name '__pycache__' -print0 | xargs -0 rm -rf || true ;
29+
find "/root/.local" -type d -name '__pycache__' -print0 | xargs -0 rm -rf || true ; \
30+
# Make an empty directory for final stage
31+
mkdir -p /newdir
2432

33+
########################################
34+
# Final stage
2535
# Distroless image use monty(1000) for non-root user
36+
########################################
2637
FROM al3xos/python-distroless:3.12-debian12 as final
2738

39+
ARG UID=1000
40+
41+
# Create directories with correct permissions
42+
COPY --link --chown=$UID:0 --chmod=775 --from=build /newdir /download
43+
COPY --link --chown=$UID:0 --chmod=775 --from=build /newdir /licenses
44+
2845
# ffmpeg
29-
COPY --link --from=mwader/static-ffmpeg:6.1.1 /ffmpeg /usr/bin/
30-
COPY --link --from=mwader/static-ffmpeg:6.1.1 /ffprobe /usr/bin/
46+
COPY --link --from=ghcr.io/jim60105/static-ffmpeg-upx:7.0-1 /ffmpeg /usr/bin/
47+
COPY --link --from=ghcr.io/jim60105/static-ffmpeg-upx:7.0-1 /ffprobe /usr/bin/
48+
49+
# dumb-init
50+
COPY --link --from=ghcr.io/jim60105/static-ffmpeg-upx:7.0-1 /dumb-init /usr/bin/
51+
52+
# Copy licenses (OpenShift Policy)
53+
COPY --link --chown=$UID:0 --chmod=775 LICENSE /licenses/Dockerfile.LICENSE
54+
COPY --link --chown=$UID:0 --chmod=775 yt-dlp/LICENSE /licenses/yt-dlp.LICENSE
3155

3256
# Copy dist and support arbitrary user ids (OpenShift best practice)
3357
# https://docs.openshift.com/container-platform/4.14/openshift_images/create-images.html#use-uid_create-images
34-
COPY --chown=1000:0 --chmod=774 \
35-
--from=build /root/.local /home/monty/.local
58+
COPY --link --chown=$UID:0 --chmod=775 --from=build /root/.local /home/monty/.local
59+
3660
ENV PATH="/home/monty/.local/bin:$PATH"
3761

3862
WORKDIR /download
63+
3964
VOLUME [ "/download" ]
4065

66+
USER $UID
67+
4168
STOPSIGNAL SIGINT
69+
70+
# Use dumb-init as PID 1 to handle signals properly
4271
ENTRYPOINT [ "dumb-init", "--", "yt-dlp", "--no-cache-dir" ]
43-
CMD ["--help"]
72+
CMD ["--help"]
73+
74+
ARG VERSION
75+
ARG RELEASE
76+
LABEL name="jim60105/docker-yt-dlp" \
77+
# Authors for yt-dlp
78+
vendor="yt-dlp" \
79+
# Maintainer for this docker image
80+
maintainer="jim60105" \
81+
# Dockerfile source repository
82+
url="https://github.com/jim60105/docker-yt-dlp" \
83+
version=${VERSION} \
84+
# This should be a number, incremented with each change
85+
release=${RELEASE} \
86+
io.k8s.display-name="yt-dlp" \
87+
summary="yt-dlp: A feature-rich command-line audio/video downloader." \
88+
description="yt-dlp is a feature-rich command-line audio/video downloader with support for thousands of sites. The project is a fork of youtube-dl based on the now inactive youtube-dlc. For more information about this tool, please visit the following website: https://github.com/yt-dlp/yt-dlp"

0 commit comments

Comments
 (0)