diff --git a/Dockerfile b/Dockerfile index 9c00d14ec..9300d5d68 100644 --- a/Dockerfile +++ b/Dockerfile @@ -155,10 +155,6 @@ ENV NVIDIA_DRIVER_CAPABILITIES="compute,video,utility" ENV PATH="/usr/lib/btbn-ffmpeg/bin:/usr/local/go2rtc/bin:/usr/local/nginx/sbin:${PATH}" -# TODO: remove after a new verion of s6-overlay is released. See: -# https://github.com/just-containers/s6-overlay/issues/460#issuecomment-1327127006 -ENV S6_SERVICES_READYTIME=50 - # Install dependencies RUN --mount=type=bind,source=docker/install_deps.sh,target=/deps/install_deps.sh \ /deps/install_deps.sh @@ -175,11 +171,31 @@ EXPOSE 1935 EXPOSE 8554 EXPOSE 8555 +# Fails if cont-init.d fails +ENV S6_BEHAVIOUR_IF_STAGE2_FAILS=2 +# Wait indefinitely for cont-init.d to finish before starting services +ENV S6_CMD_WAIT_FOR_SERVICES=1 +ENV S6_CMD_WAIT_FOR_SERVICES_MAXTIME=0 +# Give services (including Frigate) 30 seconds to stop before killing them +# But this is not working currently because of: +# https://github.com/just-containers/s6-overlay/issues/503 +ENV S6_SERVICES_GRACETIME=30000 +# Configure logging to prepend timestamps, log to stdout, keep 1 archive and rotate on 10MB +ENV S6_LOGGING_SCRIPT="T 1 n1 s10000000 T" +# TODO: remove after a new version of s6-overlay is released. See: +# https://github.com/just-containers/s6-overlay/issues/460#issuecomment-1327127006 +ENV S6_SERVICES_READYTIME=50 + ENTRYPOINT ["/init"] +CMD [] # Frigate deps with Node.js and NPM for devcontainer FROM deps AS devcontainer +# Do not start the actual Frigate service on devcontainer as it will be started by VSCode +# But start a fake service for simulating the logs +COPY docker/fake_frigate_run /etc/services.d/frigate/run + # Install Node 16 RUN apt-get update \ && apt-get install wget -y \ @@ -224,5 +240,3 @@ FROM deps WORKDIR /opt/frigate/ COPY --from=rootfs / / - -CMD ["with-contenv", "python3", "-u", "-m", "frigate"] diff --git a/Makefile b/Makefile index 3ec99eb16..54182f68c 100644 --- a/Makefile +++ b/Makefile @@ -30,8 +30,11 @@ build: version amd64 arm64 armv7 push: build docker buildx build --push --platform linux/arm/v7,linux/arm64/v8,linux/amd64 --tag $(IMAGE_REPO):${GITHUB_REF_NAME}-$(COMMIT_HASH) . +run: local + docker run --rm --publish=5000:5000 --volume=${PWD}/config/config.yml:/config/config.yml frigate:latest + run_tests: local - docker run --rm --entrypoint=python3 frigate:latest -u -m unittest - docker run --rm --entrypoint=python3 frigate:latest -u -m mypy --config-file frigate/mypy.ini frigate + docker run --rm --workdir=/opt/frigate --entrypoint= frigate:latest python3 -u -m unittest + docker run --rm --workdir=/opt/frigate --entrypoint= frigate:latest python3 -u -m mypy --config-file frigate/mypy.ini frigate .PHONY: run_tests diff --git a/docker/fake_frigate_run b/docker/fake_frigate_run new file mode 100755 index 000000000..a4415b616 --- /dev/null +++ b/docker/fake_frigate_run @@ -0,0 +1,8 @@ +#!/command/with-contenv bash +# shellcheck shell=bash +# Start the fake Frigate service + +while true; do + echo "The fake Frigate service is running..." + sleep 5s +done diff --git a/docker/rootfs/etc/cont-init.d/prepare-logs.sh b/docker/rootfs/etc/cont-init.d/prepare-logs.sh new file mode 100755 index 000000000..0d8d73ce2 --- /dev/null +++ b/docker/rootfs/etc/cont-init.d/prepare-logs.sh @@ -0,0 +1,11 @@ +#!/command/with-contenv bash +# shellcheck shell=bash +# Prepare the logs folder for s6-log + +set -o errexit -o nounset -o pipefail + +dirs=(/dev/shm/logs/frigate /dev/shm/logs/go2rtc /dev/shm/logs/nginx) + +mkdir -p "${dirs[@]}" +chown nobody:nogroup "${dirs[@]}" +chmod 02755 "${dirs[@]}" diff --git a/docker/rootfs/etc/services.d/frigate/finish b/docker/rootfs/etc/services.d/frigate/finish new file mode 100755 index 000000000..e219ed8b4 --- /dev/null +++ b/docker/rootfs/etc/services.d/frigate/finish @@ -0,0 +1,16 @@ +#!/command/with-contenv bash +# shellcheck shell=bash +# Take down the S6 supervision tree when the service exits + +set -o errexit -o nounset -o pipefail + +# Prepare exit code +if [[ "${1}" -eq 256 ]]; then + exit_code="$((128 + ${2}))" +else + exit_code="${1}" +fi + +# Make the container exit with the same exit code as the service +echo "${exit_code}" > /run/s6-linux-init-container-results/exitcode +exec /run/s6/basedir/bin/halt diff --git a/docker/rootfs/etc/services.d/frigate/log/run b/docker/rootfs/etc/services.d/frigate/log/run new file mode 100755 index 000000000..c10284862 --- /dev/null +++ b/docker/rootfs/etc/services.d/frigate/log/run @@ -0,0 +1,4 @@ +#!/command/with-contenv bash +# shellcheck shell=bash + +exec logutil-service /dev/shm/logs/frigate diff --git a/docker/rootfs/etc/services.d/frigate/run b/docker/rootfs/etc/services.d/frigate/run new file mode 100755 index 000000000..c543ff0ff --- /dev/null +++ b/docker/rootfs/etc/services.d/frigate/run @@ -0,0 +1,11 @@ +#!/command/with-contenv bash +# shellcheck shell=bash +# Start the Frigate service + +set -o errexit -o nounset -o pipefail + +cd /opt/frigate + +# Replace the bash process with the Frigate process, redirecting stderr to stdout +exec 2>&1 +exec python3 -u -m frigate diff --git a/docker/rootfs/etc/services.d/go2rtc/finish b/docker/rootfs/etc/services.d/go2rtc/finish index 3a9da25ce..c1b77fccc 100755 --- a/docker/rootfs/etc/services.d/go2rtc/finish +++ b/docker/rootfs/etc/services.d/go2rtc/finish @@ -1,6 +1,7 @@ #!/command/with-contenv bash # shellcheck shell=bash -# Take down the S6 supervision tree when the process fails +# Take down the S6 supervision tree when the service fails, or restart it +# otherwise if [[ "${1}" -ne 0 && "${1}" -ne 256 ]]; then exec /run/s6/basedir/bin/halt diff --git a/docker/rootfs/etc/services.d/go2rtc/log/run b/docker/rootfs/etc/services.d/go2rtc/log/run new file mode 100755 index 000000000..96a204b9d --- /dev/null +++ b/docker/rootfs/etc/services.d/go2rtc/log/run @@ -0,0 +1,4 @@ +#!/command/with-contenv bash +# shellcheck shell=bash + +exec logutil-service /dev/shm/logs/go2rtc diff --git a/docker/rootfs/etc/services.d/go2rtc/run b/docker/rootfs/etc/services.d/go2rtc/run index 6ae2918e5..9b41e517b 100755 --- a/docker/rootfs/etc/services.d/go2rtc/run +++ b/docker/rootfs/etc/services.d/go2rtc/run @@ -1,8 +1,8 @@ #!/command/with-contenv bash # shellcheck shell=bash +# Start the go2rtc service -# https://gist.github.com/mohanpedala/1e2ff5661761d3abd0385e8223e16425?permalink_comment_id=3945021 -set -euo pipefail +set -o errexit -o nounset -o pipefail if [[ -f "/config/frigate-go2rtc.yaml" ]]; then config_path="/config/frigate-go2rtc.yaml" @@ -10,4 +10,6 @@ else config_path="/usr/local/go2rtc/go2rtc.yaml" fi +# Replace the bash process with the go2rtc process, redirecting stderr to stdout +exec 2>&1 exec go2rtc -config="${config_path}" diff --git a/docker/rootfs/etc/services.d/nginx/finish b/docker/rootfs/etc/services.d/nginx/finish index 3a9da25ce..c1b77fccc 100755 --- a/docker/rootfs/etc/services.d/nginx/finish +++ b/docker/rootfs/etc/services.d/nginx/finish @@ -1,6 +1,7 @@ #!/command/with-contenv bash # shellcheck shell=bash -# Take down the S6 supervision tree when the process fails +# Take down the S6 supervision tree when the service fails, or restart it +# otherwise if [[ "${1}" -ne 0 && "${1}" -ne 256 ]]; then exec /run/s6/basedir/bin/halt diff --git a/docker/rootfs/etc/services.d/nginx/log/run b/docker/rootfs/etc/services.d/nginx/log/run new file mode 100755 index 000000000..50057d1d7 --- /dev/null +++ b/docker/rootfs/etc/services.d/nginx/log/run @@ -0,0 +1,4 @@ +#!/command/with-contenv bash +# shellcheck shell=bash + +exec logutil-service /dev/shm/logs/nginx diff --git a/docker/rootfs/etc/services.d/nginx/run b/docker/rootfs/etc/services.d/nginx/run index 18de12f59..f9978b6b2 100755 --- a/docker/rootfs/etc/services.d/nginx/run +++ b/docker/rootfs/etc/services.d/nginx/run @@ -1,4 +1,7 @@ #!/command/with-contenv bash # shellcheck shell=bash +# Start the NGINX service +# Replace the bash process with the NGINX process, redirecting stderr to stdout +exec 2>&1 exec nginx diff --git a/docker/rootfs/usr/local/bin/frigate b/docker/rootfs/usr/local/bin/frigate new file mode 100644 index 000000000..7e431b11c --- /dev/null +++ b/docker/rootfs/usr/local/bin/frigate @@ -0,0 +1,5 @@ +#!/command/with-contenv bash +# shellcheck shell=bash + +exec 2>&1 +exec python3 -u -m frigate "${@}" diff --git a/docker/rootfs/usr/local/nginx/conf/nginx.conf b/docker/rootfs/usr/local/nginx/conf/nginx.conf index e69eb739e..5a5a4944f 100644 --- a/docker/rootfs/usr/local/nginx/conf/nginx.conf +++ b/docker/rootfs/usr/local/nginx/conf/nginx.conf @@ -2,7 +2,7 @@ daemon off; user root; worker_processes 1; -error_log /usr/local/nginx/logs/error.log warn; +error_log /dev/stdout warn; pid /var/run/nginx.pid; events { @@ -17,7 +17,7 @@ http { '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; - access_log /usr/local/nginx/logs/access.log main; + access_log /dev/stdout main; sendfile on;