From 7e0411f6bd11393db82067e033c074d216a00910 Mon Sep 17 00:00:00 2001 From: Sergey Krashevich Date: Fri, 22 Mar 2024 20:21:34 +0300 Subject: [PATCH 1/3] feat(docker): update Docker syntax, add entrypoint script, and ensure cross-platform compatibility - Updated Dockerfile and hardware.Dockerfile syntax to `docker/dockerfile:1.4` for enhanced features and performance. - Introduced `--link` option in COPY commands to optimize layer caching and reduce image size. - Added a new `entrypoint.sh` script for dynamic configuration and validation of environment variables, improving container startup flexibility. - Included `yq` utility in the base images to facilitate YAML file manipulation within the container, enhancing configuration management capabilities. - Specified `--platform=linux/amd64` for base images to ensure consistent cross-platform builds, addressing potential compatibility issues on multi-architecture setups. - Modified ENTRYPOINT to utilize the new entrypoint script, enabling pre-execution configuration checks and setting a default configuration if none is provided. - The addition of `yq` and changes to the entrypoint mechanism provide a more robust and user-friendly setup process, potentially affecting existing workflows that rely on manual configuration file adjustments. These changes aim to improve maintainability, efficiency, and user experience by leveraging newer Docker features, automating configuration tasks, and ensuring broader compatibility across different computing environments. --- Dockerfile | 13 +++++++------ entrypoint.sh | 21 +++++++++++++++++++++ hardware.Dockerfile | 19 ++++++++----------- 3 files changed, 36 insertions(+), 17 deletions(-) create mode 100644 entrypoint.sh diff --git a/Dockerfile b/Dockerfile index f28f4e2f..bd9dded0 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,4 @@ -# syntax=docker/dockerfile:labs +# syntax=docker/dockerfile:1.4 # 0. Prepare images ARG PYTHON_VERSION="3.11" @@ -32,7 +32,7 @@ RUN --mount=type=cache,target=/root/.cache/go-build CGO_ENABLED=0 go build -ldfl FROM scratch AS rootfs COPY --from=build /build/go2rtc /usr/local/bin/ -COPY --from=ngrok /bin/ngrok /usr/local/bin/ +COPY --link --from=ngrok /bin/ngrok /usr/local/bin/ # 3. Final image @@ -42,7 +42,7 @@ FROM base # and other common tools for the echo source. # alsa-plugins-pulse for ALSA support (+0MB) # font-droid for FFmpeg drawtext filter (+2MB) -RUN apk add --no-cache tini ffmpeg bash curl jq alsa-plugins-pulse font-droid +RUN apk add --no-cache tini ffmpeg bash curl jq alsa-plugins-pulse font-droid yq # Hardware Acceleration for Intel CPU (+50MB) ARG TARGETARCH @@ -54,10 +54,11 @@ RUN if [ "${TARGETARCH}" = "amd64" ]; then apk add --no-cache libva-intel-driver # Hardware: AMD and NVidia VDPAU (not sure about this) # RUN libva-vdpau-driver mesa-vdpau-gallium (+150MB total) -COPY --from=rootfs / / +COPY --link --from=rootfs / / -ENTRYPOINT ["/sbin/tini", "--"] +COPY --chmod=755 entrypoint.sh /entrypoint.sh + +ENTRYPOINT ["/sbin/tini", "--", "/entrypoint.sh"] VOLUME /config WORKDIR /config -CMD ["go2rtc", "-config", "/config/go2rtc.yaml"] diff --git a/entrypoint.sh b/entrypoint.sh new file mode 100644 index 00000000..2856b702 --- /dev/null +++ b/entrypoint.sh @@ -0,0 +1,21 @@ +#!/bin/sh + +CONFIG_PATH=${CONFIG_PATH:-/config/go2rtc.yaml} +PORT=${PORT:-1984} + +# Check if yq is available +if ! command -v yq > /dev/null; then + echo "yq command not found. Please install yq." + exit 1 +fi + +# Create config file with default content if it doesn't exist +if [ ! -f "$CONFIG_PATH" ]; then + mkdir -p "$(dirname "$CONFIG_PATH")" + if ! yq -o yaml e -n ".api.listen = \":$PORT\"" > "$CONFIG_PATH"; then + echo "Failed to create default config file at $CONFIG_PATH" + exit 1 + fi +fi + +exec go2rtc -config "$CONFIG_PATH" "$@" \ No newline at end of file diff --git a/hardware.Dockerfile b/hardware.Dockerfile index 238ede69..d55b9e37 100644 --- a/hardware.Dockerfile +++ b/hardware.Dockerfile @@ -1,4 +1,4 @@ -# syntax=docker/dockerfile:labs +# syntax=docker/dockerfile:1.4 # 0. Prepare images # only debian 13 (trixie) has latest ffmpeg @@ -7,9 +7,9 @@ ARG DEBIAN_VERSION="trixie-slim" ARG GO_VERSION="1.21-bookworm" ARG NGROK_VERSION="3" -FROM debian:${DEBIAN_VERSION} AS base +FROM --platform=linux/amd64 debian:${DEBIAN_VERSION} AS base FROM golang:${GO_VERSION} AS go -FROM ngrok/ngrok:${NGROK_VERSION} AS ngrok +FROM --platform=linux/amd64 ngrok/ngrok:${NGROK_VERSION} AS ngrok # 1. Build go2rtc binary @@ -29,12 +29,12 @@ RUN --mount=type=cache,target=/root/.cache/go-build go mod download COPY . . RUN --mount=type=cache,target=/root/.cache/go-build CGO_ENABLED=0 go build -ldflags "-s -w" -trimpath - +RUN --mount=type=cache,target=/root/.cache/go-build CGO_ENABLED=0 go install -ldflags "-s -w" -trimpath github.com/mikefarah/yq/v4@v4.42.1 # 2. Collect all files FROM scratch AS rootfs -COPY --link --from=build /build/go2rtc /usr/local/bin/ +COPY --link --from=build /build/go2rtc /go/bin/yq /usr/local/bin/ COPY --link --from=ngrok /bin/ngrok /usr/local/bin/ # 3. Final image @@ -56,14 +56,11 @@ RUN --mount=type=cache,target=/var/cache/apt,sharing=locked --mount=type=cache,t libasound2-plugins COPY --link --from=rootfs / / +COPY --chmod=755 entrypoint.sh /entrypoint.sh - - -ENTRYPOINT ["/usr/bin/tini", "--"] +ENTRYPOINT ["/usr/bin/tini", "--", "/entrypoint.sh"] VOLUME /config WORKDIR /config # https://github.com/NVIDIA/nvidia-docker/wiki/Installation-(Native-GPU-Support) ENV NVIDIA_VISIBLE_DEVICES all -ENV NVIDIA_DRIVER_CAPABILITIES compute,video,utility - -CMD ["go2rtc", "-config", "/config/go2rtc.yaml"] +ENV NVIDIA_DRIVER_CAPABILITIES compute,video,utility \ No newline at end of file From 85392fc7cf9005151ffa32ac997b427cbf2f503f Mon Sep 17 00:00:00 2001 From: Sergey Krashevich Date: Tue, 7 May 2024 15:21:32 +0300 Subject: [PATCH 2/3] refactor(docker): remove yq dependency and simplify config creation - Removed `yq` installation from Dockerfile to reduce image size. - Simplified default configuration file creation in entrypoint.sh by directly writing YAML content, eliminating the need for `yq`. - Removed `yq` installation step from hardware.Dockerfile and adjusted file copying accordingly. --- Dockerfile | 2 +- entrypoint.sh | 16 ++++------------ hardware.Dockerfile | 3 +-- 3 files changed, 6 insertions(+), 15 deletions(-) diff --git a/Dockerfile b/Dockerfile index bd9dded0..54780473 100644 --- a/Dockerfile +++ b/Dockerfile @@ -42,7 +42,7 @@ FROM base # and other common tools for the echo source. # alsa-plugins-pulse for ALSA support (+0MB) # font-droid for FFmpeg drawtext filter (+2MB) -RUN apk add --no-cache tini ffmpeg bash curl jq alsa-plugins-pulse font-droid yq +RUN apk add --no-cache tini ffmpeg bash curl jq alsa-plugins-pulse font-droid # Hardware Acceleration for Intel CPU (+50MB) ARG TARGETARCH diff --git a/entrypoint.sh b/entrypoint.sh index 2856b702..017ff539 100644 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -3,19 +3,11 @@ CONFIG_PATH=${CONFIG_PATH:-/config/go2rtc.yaml} PORT=${PORT:-1984} -# Check if yq is available -if ! command -v yq > /dev/null; then - echo "yq command not found. Please install yq." - exit 1 -fi - -# Create config file with default content if it doesn't exist +# Create the config file with default settings if it doesn't exist if [ ! -f "$CONFIG_PATH" ]; then mkdir -p "$(dirname "$CONFIG_PATH")" - if ! yq -o yaml e -n ".api.listen = \":$PORT\"" > "$CONFIG_PATH"; then - echo "Failed to create default config file at $CONFIG_PATH" - exit 1 - fi + echo "api:" > "$CONFIG_PATH" + echo -e "\tlisten: ':$PORT'" >> "$CONFIG_PATH" fi -exec go2rtc -config "$CONFIG_PATH" "$@" \ No newline at end of file +exec go2rtc -config "$CONFIG_PATH" "$@" diff --git a/hardware.Dockerfile b/hardware.Dockerfile index 7fa8b6dd..295e5f95 100644 --- a/hardware.Dockerfile +++ b/hardware.Dockerfile @@ -29,12 +29,11 @@ RUN --mount=type=cache,target=/root/.cache/go-build go mod download COPY . . RUN --mount=type=cache,target=/root/.cache/go-build CGO_ENABLED=0 go build -ldflags "-s -w" -trimpath -RUN --mount=type=cache,target=/root/.cache/go-build CGO_ENABLED=0 go install -ldflags "-s -w" -trimpath github.com/mikefarah/yq/v4@v4.42.1 # 2. Collect all files FROM scratch AS rootfs -COPY --link --from=build /build/go2rtc /go/bin/yq /usr/local/bin/ +COPY --link --from=build /build/go2rtc /usr/local/bin/ COPY --link --from=ngrok /bin/ngrok /usr/local/bin/ # 3. Final image From 8840a18cdd921490bb6d91376ed95dd3f25f3dc4 Mon Sep 17 00:00:00 2001 From: Sergey Krashevich Date: Tue, 7 May 2024 15:30:46 +0300 Subject: [PATCH 3/3] fix(entrypoint): streamline config initialization and execution Simplify the entrypoint script by removing the creation of a default configuration file if it doesn't exist. Instead, directly pass the default API listen port configuration to the `go2rtc` command, while still allowing custom configurations through the existing `CONFIG_PATH`. This change ensures that the service can start with default settings without needing to write them to a file, simplifying deployment and configuration management. --- entrypoint.sh | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/entrypoint.sh b/entrypoint.sh index 017ff539..aa0090cc 100644 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -3,11 +3,8 @@ CONFIG_PATH=${CONFIG_PATH:-/config/go2rtc.yaml} PORT=${PORT:-1984} -# Create the config file with default settings if it doesn't exist if [ ! -f "$CONFIG_PATH" ]; then mkdir -p "$(dirname "$CONFIG_PATH")" - echo "api:" > "$CONFIG_PATH" - echo -e "\tlisten: ':$PORT'" >> "$CONFIG_PATH" fi -exec go2rtc -config "$CONFIG_PATH" "$@" +exec go2rtc -config "{ api: { listen: \":$PORT\" } }" -config "$CONFIG_PATH" "$@"