Improve the devcontainer experience (#3492)

* Make it easier to run the devcontainer

* Some more improvements

* Tidy up few other things

* Better name stages

* Fix CI

* Setup everything with one click

* Allow to set IMAGE_OWNER

* Change IMAGE_OWNER to IMAGE_REPO

* Fix CI with IMAGE_REPO

* Fix nodejs installation

* Test devcontainer build as part of CI

* Build devcontainer in its own job

* Fix devcontainer cli installation

* Fix devcontainer build

* Fix devcontainer build in CI again

* Enable buildkit only

* Increase coverage of devcontainer test

* Fix devcontainer start in CI

* Ensure latest version of docker compose is used

* Fix install compose action

* Disable CI stuff which does not work until we fix them
This commit is contained in:
Felipe Santos 2022-11-20 10:34:12 -03:00 committed by GitHub
parent 253061ee5f
commit 65825040a3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 191 additions and 79 deletions

View File

@ -1,16 +1,50 @@
{
"name": "Frigate Dev",
"name": "Frigate Devcontainer",
"dockerComposeFile": "../docker-compose.yml",
"service": "dev",
"workspaceFolder": "/lab/frigate",
"service": "devcontainer",
"workspaceFolder": "/workspace/frigate",
"initializeCommand": ".devcontainer/initialize.sh",
"postCreateCommand": ".devcontainer/post_create.sh",
"overrideCommand": false,
"remoteUser": "vscode",
"features": {
"ghcr.io/devcontainers/features/common-utils:1": {}
},
"forwardPorts": [5000, 5001, 5173, 1935, 8554, 8555],
"portsAttributes": {
"5000": {
"label": "NGINX",
"onAutoForward": "silent"
},
"5001": {
"label": "Frigate API",
"onAutoForward": "silent"
},
"5173": {
"label": "Vite Server",
"onAutoForward": "silent"
},
"1935": {
"label": "RTMP",
"onAutoForward": "silent"
},
"8554": {
"label": "gortc RTSP",
"onAutoForward": "silent"
},
"8555": {
"label": "go2rtc WebRTC",
"onAutoForward": "silent"
}
},
"extensions": [
"ms-python.vscode-pylance",
"ms-python.python",
"visualstudioexptteam.vscodeintellicode",
"mhutchie.git-graph",
"ms-azuretools.vscode-docker",
"streetsidesoftware.code-spell-checker",
"esbenp.prettier-vscode",
"ms-python.vscode-pylance",
"dbaeumer.vscode-eslint",
"mikestead.dotenv",
"csstools.postcss",
@ -18,6 +52,7 @@
"bradlc.vscode-tailwindcss"
],
"settings": {
"remote.autoForwardPorts": false,
"python.linting.pylintEnabled": true,
"python.linting.enabled": true,
"python.formatting.provider": "black",

13
.devcontainer/initialize.sh Executable file
View File

@ -0,0 +1,13 @@
#!/bin/bash
set -euo pipefail
# These folders needs to be created and owned by the host user
mkdir -p debug web/dist
if [[ -f "config/config.yml" ]]; then
echo "config/config.yml already exists, skipping initialization" >&2
else
echo "initializing config/config.yml" >&2
cp -fv config/config.yml.example config/config.yml
fi

17
.devcontainer/post_create.sh Executable file
View File

@ -0,0 +1,17 @@
#!/bin/bash
set -euxo pipefail
# Frigate normal container runs as root, so it have permission to create
# the folders. But the devcontainer runs as the host user, so we need to
# create the folders and give the host user permission to write to them.
sudo mkdir -p /media/frigate
sudo chown -R "$(id -u):$(id -g)" /media/frigate
make version
cd web
npm install
npm run build

View File

@ -9,4 +9,8 @@ core
*.mp4
*.jpg
*.db
*.ts
*.ts
web/dist/
web/node_modules/
web/.npm

View File

@ -26,7 +26,7 @@ jobs:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Build web
run: make build_web
- name: Build image
run: make push
run: make push
env:
IMAGE_REPO: ghcr.io/${{ github.actor }}/frigate

View File

@ -6,6 +6,30 @@ env:
DEFAULT_PYTHON: 3.9
jobs:
build_devcontainer:
runs-on: ubuntu-latest
name: Build Devcontainer
# The Dockerfile contains features that requires buildkit, and since the
# devcontainer cli uses docker-compose to build the image, the only way to
# ensure docker-compose uses buildkit is to explicitly enable it.
env:
DOCKER_BUILDKIT: "1"
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@master
with:
node-version: 16.x
- name: Install devcontainer cli
run: npm install --global @devcontainers/cli
- name: Build devcontainer
run: devcontainer build --workspace-folder .
# It would be nice to also test the following commands, but for some
# reason they don't work even though in VS Code devcontainer works.
# - name: Start devcontainer
# run: devcontainer up --workspace-folder .
# - name: Run devcontainer scripts
# run: devcontainer run-user-commands --workspace-folder .
web_lint:
name: Web - Lint
runs-on: ubuntu-latest
@ -36,7 +60,7 @@ jobs:
python_checks:
runs-on: ubuntu-latest
name: Python checks
name: Python Checks
steps:
- name: Check out the repository
uses: actions/checkout@v3
@ -70,8 +94,6 @@ jobs:
uses: docker/setup-qemu-action@v2
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2
- name: Create Version Module
run: make version
- name: Build
run: make
- name: Run mypy

View File

@ -1,8 +1,8 @@
# syntax=docker/dockerfile:1.2
FROM blakeblackshear/frigate-nginx:1.0.2 as nginx
FROM blakeblackshear/frigate-nginx:1.0.2 AS nginx
FROM debian:11 as wheels
FROM debian:11 AS wheels
ARG TARGETARCH
ENV DEBIAN_FRONTEND=noninteractive
@ -44,8 +44,8 @@ RUN pip3 install -r requirements.txt
COPY requirements-wheels.txt /requirements-wheels.txt
RUN pip3 wheel --wheel-dir=/wheels -r requirements-wheels.txt
# Frigate Container
FROM debian:11-slim
# Frigate deps (ffmpeg, python, nginx, go2rtc, s6-overlay, etc)
FROM debian:11-slim AS deps
ARG TARGETARCH
# https://askubuntu.com/questions/972516/debian-frontend-environment-variable
@ -65,7 +65,7 @@ RUN --mount=type=bind,from=wheels,source=/wheels,target=/wheels \
gnupg \
wget \
procps \
unzip tzdata libxml2 xz-utils \
unzip locales tzdata libxml2 xz-utils \
python3-pip \
# add raspberry pi repo
&& apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 9165938D90FDDD2E \
@ -94,10 +94,12 @@ RUN --mount=type=bind,from=wheels,source=/wheels,target=/wheels \
fi \
# arch specific packages
&& if [ "${TARGETARCH}" = "amd64" ]; then \
echo 'deb http://deb.debian.org/debian testing main non-free' >> /etc/apt/sources.list.d/deb.list \
# Use debian testing repo only for hwaccel packages
echo 'deb http://deb.debian.org/debian testing main non-free' > /etc/apt/sources.list.d/debian-testing.list \
&& apt-get -qq update \
&& apt-get -qq install --no-install-recommends --no-install-suggests -y \
mesa-va-drivers libva-drm2 intel-media-va-driver-non-free i965-va-driver libmfx1; \
mesa-va-drivers libva-drm2 intel-media-va-driver-non-free i965-va-driver libmfx1 \
&& rm -f /etc/apt/sources.list.d/debian-testing.list; \
fi \
&& if [ "${TARGETARCH}" = "arm64" ]; then \
apt-get -qq install --no-install-recommends --no-install-suggests -y \
@ -133,12 +135,6 @@ COPY labelmap.txt /labelmap.txt
RUN wget -q https://github.com/google-coral/test_data/raw/release-frogfish/ssdlite_mobiledet_coco_qat_postprocess_edgetpu.tflite -O /edgetpu_model.tflite
RUN wget -q https://github.com/google-coral/test_data/raw/release-frogfish/ssdlite_mobiledet_coco_qat_postprocess.tflite -O /cpu_model.tflite
WORKDIR /opt/frigate/
ADD frigate frigate/
ADD migrations migrations/
COPY web/dist web/
COPY docker/rootfs/ /
# s6-overlay
@ -156,4 +152,51 @@ EXPOSE 8555
ENTRYPOINT ["/init"]
# Frigate deps with Node.js and NPM
FROM deps AS deps-node
# Install Node 16
RUN wget -qO- https://deb.nodesource.com/setup_16.x | bash - \
&& apt-get install -y nodejs \
&& npm install -g npm@9
# Devcontainer
FROM deps-node AS devcontainer
WORKDIR /workspace/frigate
RUN apt-get update \
&& apt-get install make -y \
&& rm -rf /var/lib/apt/lists/*
RUN --mount=type=bind,source=./requirements-dev.txt,target=/workspace/frigate/requirements-dev.txt \
pip3 install -r requirements-dev.txt
CMD ["sleep", "infinity"]
# Frigate web build
FROM deps-node AS web-build
WORKDIR /work
COPY web/package.json web/package-lock.json ./
RUN npm install
COPY web/ ./
RUN npm run build
# Frigate web dist files
FROM scratch AS web-dist
COPY --from=web-build /work/dist/ /
# Frigate final container
FROM deps
WORKDIR /opt/frigate/
COPY frigate frigate/
COPY migrations migrations/
COPY --from=web-dist / web/
CMD ["python3", "-u", "-m", "frigate"]

View File

@ -2,35 +2,33 @@ default_target: local
COMMIT_HASH := $(shell git log -1 --pretty=format:"%h"|tail -1)
VERSION = 0.12.0
IMAGE_OWNER ?= ghcr.io/blakeblackshear/frigate
CURRENT_UID := $(shell id -u)
CURRENT_GID := $(shell id -g)
version:
echo "VERSION=\"$(VERSION)-$(COMMIT_HASH)\"" > frigate/version.py
echo 'VERSION = "$(VERSION)-$(COMMIT_HASH)"' > frigate/version.py
local: version
docker buildx build --tag frigate:latest --load .
build_web:
docker run -e npm_config_cache=/web/.npm --volume ${PWD}/web:/web -w /web --group-add $(CURRENT_GID) -u $(CURRENT_UID):$(CURRENT_GID) node:16 /bin/bash -c "npm install && npm run build"
nginx_frigate:
docker buildx build --push --platform linux/arm/v7,linux/arm64/v8,linux/amd64 --tag blakeblackshear/frigate-nginx:1.0.2 --file docker/Dockerfile.nginx .
local:
DOCKER_BUILDKIT=1 docker build -t frigate -f docker/Dockerfile .
docker buildx build --target web-dist --output web/dist .
amd64:
docker buildx build --platform linux/amd64 --tag blakeblackshear/frigate:$(VERSION)-$(COMMIT_HASH) --file docker/Dockerfile .
docker buildx build --platform linux/amd64 --tag $(IMAGE_REPO):$(VERSION)-$(COMMIT_HASH) .
arm64:
docker buildx build --platform linux/arm64 --tag blakeblackshear/frigate:$(VERSION)-$(COMMIT_HASH) --file docker/Dockerfile .
docker buildx build --platform linux/arm64 --tag $(IMAGE_REPO):$(VERSION)-$(COMMIT_HASH) .
armv7:
docker buildx build --platform linux/arm/v7 --tag blakeblackshear/frigate:$(VERSION)-$(COMMIT_HASH) --file docker/Dockerfile .
docker buildx build --platform linux/arm/v7 --tag $(IMAGE_REPO):$(VERSION)-$(COMMIT_HASH) .
build: version amd64 arm64 armv7
docker buildx build --platform linux/arm/v7,linux/arm64/v8,linux/amd64 --tag blakeblackshear/frigate:$(VERSION)-$(COMMIT_HASH) --file docker/Dockerfile .
docker buildx build --platform linux/arm/v7,linux/arm64/v8,linux/amd64 --tag $(IMAGE_REPO):$(VERSION)-$(COMMIT_HASH) .
push: build
docker buildx build --push --platform linux/arm/v7,linux/arm64/v8,linux/amd64 --tag ghcr.io/blakeblackshear/frigate:${GITHUB_REF_NAME}-$(COMMIT_HASH) --file docker/Dockerfile .
docker buildx build --push --platform linux/arm/v7,linux/arm64/v8,linux/amd64 --tag $(IMAGE_REPO):${GITHUB_REF_NAME}-$(COMMIT_HASH) .
run_tests: frigate
docker run --rm --entrypoint=python3 frigate:latest -u -m unittest

16
config/config.yml.example Normal file
View File

@ -0,0 +1,16 @@
mqtt:
host: mqtt
cameras:
test:
ffmpeg:
inputs:
- path: /media/frigate/car-stopping.mp4
input_args: -re -stream_loop -1 -fflags +genpts
roles:
- detect
- rtmp
detect:
height: 1080
width: 1920
fps: 5

View File

@ -1,8 +1,7 @@
version: "3"
services:
dev:
container_name: frigate-dev
user: vscode
devcontainer:
container_name: frigate-devcontainer
# add groups from host for render, plugdev, video
group_add:
- "109" # render
@ -12,25 +11,17 @@ services:
shm_size: "256mb"
build:
context: .
dockerfile: docker/Dockerfile.dev
target: devcontainer
devices:
- /dev/bus/usb:/dev/bus/usb
- /dev/dri:/dev/dri # for intel hwaccel, needs to be updated for your hardware
# - /dev/dri:/dev/dri # for intel hwaccel, needs to be updated for your hardware
volumes:
- .:/workspace/frigate:cached
- ./web/dist:/opt/frigate/web:cached
- /etc/localtime:/etc/localtime:ro
- .:/lab/frigate:cached
- ./config/config.yml:/config/config.yml:ro
- ./debug:/media/frigate
- /dev/bus/usb:/dev/bus/usb
ports:
- "1935:1935"
- "3000:3000"
- "5000:5000"
- "5001:5001"
- "8080:8080"
- "8554:8554"
entrypoint: ["sudo", "/init"]
command: /bin/sh -c "while sleep 1000; do :; done"
mqtt:
container_name: mqtt
image: eclipse-mosquitto:1.6

View File

@ -1,27 +0,0 @@
FROM frigate:latest
ARG USERNAME=vscode
ARG USER_UID=1000
ARG USER_GID=$USER_UID
# Create the user
RUN groupadd --gid $USER_GID $USERNAME \
&& useradd --uid $USER_UID --gid $USER_GID -m $USERNAME -s /bin/bash \
#
# [Optional] Add sudo support. Omit if you don't need to install software after connecting.
&& apt-get update \
&& apt-get install -y sudo \
&& echo $USERNAME ALL=\(root\) NOPASSWD:ALL > /etc/sudoers.d/$USERNAME \
&& chmod 0440 /etc/sudoers.d/$USERNAME
RUN apt-get update \
&& apt-get install -y git curl vim htop
COPY requirements-dev.txt /opt/frigate/requirements-dev.txt
RUN pip3 install -r requirements-dev.txt
# Install Node 16
RUN curl -sL https://deb.nodesource.com/setup_16.x | bash - \
&& apt-get install -y nodejs
RUN npm install -g npm@latest