diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 00000000..b03cbe4b --- /dev/null +++ b/.dockerignore @@ -0,0 +1,8 @@ +.git/ +assets/ +examples/ + +.dockerignore +config.yaml +Dockerfile +README.md diff --git a/.github/workflows/builder.yml b/.github/workflows/builder.yml index 60b0fb48..f8884b9b 100644 --- a/.github/workflows/builder.yml +++ b/.github/workflows/builder.yml @@ -30,22 +30,22 @@ jobs: - name: Build amd64 uses: home-assistant/builder@master with: - args: --amd64 --target build/hassio --version $TAG-amd64 --no-latest --docker-hub-check + args: --amd64 --target . --version $TAG-amd64 --no-latest --docker-hub-check - name: Build i386 uses: home-assistant/builder@master with: - args: --i386 --target build/hassio --version $TAG-i386 --no-latest --docker-hub-check + args: --i386 --target . --version $TAG-i386 --no-latest --docker-hub-check - name: Build aarch64 uses: home-assistant/builder@master with: - args: --aarch64 --target build/hassio --version $TAG-aarch64 --no-latest --docker-hub-check + args: --aarch64 --target . --version $TAG-aarch64 --no-latest --docker-hub-check - name: Build armv7 uses: home-assistant/builder@master with: - args: --armv7 --target build/hassio --version $TAG-armv7 --no-latest --docker-hub-check + args: --armv7 --target . --version $TAG-armv7 --no-latest --docker-hub-check - name: Docker manifest run: | diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 00000000..25346a0b --- /dev/null +++ b/Dockerfile @@ -0,0 +1,42 @@ +# https://github.com/hassio-addons/addon-base-python/releases +ARG BASE_VERSION="9.0.1" +# https://hub.docker.com/_/golang/tags?page=1&name=-alpine +ARG GO_VERSION="1.19.2" +# https://hub.docker.com/r/ngrok/ngrok/tags?page=1&name=-alpine +ARG NGROK_VERSION="3.1.0" + + +FROM ghcr.io/hassio-addons/base-python:${BASE_VERSION} AS base + + +FROM golang:${GO_VERSION}-alpine AS go + + +FROM ngrok/ngrok:${NGROK_VERSION}-alpine AS ngrok + + +# Build go2rtc binary +FROM go AS build + +WORKDIR /workspace + +COPY . . + +RUN CGO_ENABLED=0 go build -ldflags "-s -w" -trimpath + + +# Collect all files +FROM scratch AS rootfs + +COPY --from=build /workspace/go2rtc /usr/local/bin/ +COPY --from=ngrok /bin/ngrok /usr/local/bin/ +COPY ./docker/rootfs/ / + + +# Final stage +FROM base + +# Install ffmpeg +RUN apk add --no-cache ffmpeg + +COPY --from=rootfs / / diff --git a/build/hassio/Dockerfile b/build/hassio/Dockerfile deleted file mode 100644 index 312909f0..00000000 --- a/build/hassio/Dockerfile +++ /dev/null @@ -1,41 +0,0 @@ -ARG BUILD_FROM - -FROM $BUILD_FROM as build - -# 1. Build go2rtc -RUN apk add --no-cache git go - -RUN git clone https://github.com/AlexxIT/go2rtc \ - && cd go2rtc \ - && CGO_ENABLED=0 go build -ldflags "-s -w" -trimpath - -# 2. Download ngrok -ARG BUILD_ARCH - -# https://github.com/home-assistant/docker-base/blob/master/alpine/Dockerfile -RUN if [ "${BUILD_ARCH}" = "aarch64" ]; then BUILD_ARCH="arm64"; \ - elif [ "${BUILD_ARCH}" = "armv7" ]; then BUILD_ARCH="arm"; fi \ - && cd go2rtc \ - && curl $(curl -s "https://raw.githubusercontent.com/ngrok/docker-ngrok/main/releases.json" | jq -r ".${BUILD_ARCH}.url") -o ngrok.zip \ - && unzip ngrok - - - -# https://devopscube.com/reduce-docker-image-size/ -FROM $BUILD_FROM - -# 3. Copy go2rtc and ngrok to release -COPY --from=build /go2rtc/go2rtc /usr/local/bin -COPY --from=build /go2rtc/ngrok /usr/local/bin - -# 4. Install ffmpeg -# apk base OK: 22 MiB in 40 packages -# ffmpeg OK: 113 MiB in 110 packages -# python3 OK: 161 MiB in 114 packages -RUN apk add --no-cache ffmpeg python3 - -# 5. Copy run to release -COPY run.sh / -RUN chmod a+x /run.sh - -CMD [ "/run.sh" ] diff --git a/build/hassio/run.sh b/build/hassio/run.sh deleted file mode 100644 index d59564ca..00000000 --- a/build/hassio/run.sh +++ /dev/null @@ -1,14 +0,0 @@ -#!/usr/bin/with-contenv bashio - -set +e - -# set cwd for go2rtc (for config file, Hass integration, etc) -cd /config - -# add the feature to override go2rtc binary from Hass config folder -export PATH="/config:$PATH" - -while true; do - go2rtc - sleep 5 -done \ No newline at end of file diff --git a/build/hassio/config.yaml b/config.yaml similarity index 100% rename from build/hassio/config.yaml rename to config.yaml diff --git a/docker/rootfs/etc/services.d/go2rtc/finish b/docker/rootfs/etc/services.d/go2rtc/finish new file mode 100755 index 00000000..f73fe4ba --- /dev/null +++ b/docker/rootfs/etc/services.d/go2rtc/finish @@ -0,0 +1,12 @@ +#!/command/with-contenv bashio +# ============================================================================== +# Take down the S6 supervision tree when go2rtc fails +# ============================================================================== +# shellcheck shell=bash + +if [[ "${1}" -ne 0 ]] && [[ "${1}" -ne 256 ]]; then + bashio::log.warning "go2rtc crashed, halting add-on" + /run/s6/basedir/bin/halt +fi + +bashio::log.info "go2rtc stopped, restarting..." diff --git a/docker/rootfs/etc/services.d/go2rtc/run b/docker/rootfs/etc/services.d/go2rtc/run new file mode 100755 index 00000000..99fd8fc2 --- /dev/null +++ b/docker/rootfs/etc/services.d/go2rtc/run @@ -0,0 +1,23 @@ +#!/command/with-contenv bashio +# ============================================================================== +# Runs go2rtc +# ============================================================================== +# shellcheck shell=bash + +bashio::log.info 'Starting go2rtc...' + +declare config_path +declare binary_path + +config_path="/config" + +binary_path="/usr/local/bin/go2rtc" +if [[ -x "${config_path}/go2rtc" ]]; then + binary_path="${config_path}/go2rtc" + bashio::log.warning "Using go2rtc binary from '${binary_path}'" +fi + +# set cwd for go2rtc (for config file, Hass integration, etc) +cd "${config_path}" || bashio::exit.nok "Could not change working directory" + +exec "${binary_path}"