Skip to content

Commit

Permalink
Improve the devcontainer experience (#3492)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
felipecrs committed Nov 20, 2022
1 parent 253061e commit 6582504
Show file tree
Hide file tree
Showing 11 changed files with 191 additions and 79 deletions.
43 changes: 39 additions & 4 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
@@ -1,23 +1,58 @@
{
"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",
"blanu.vscode-styled-jsx",
"bradlc.vscode-tailwindcss"
],
"settings": {
"remote.autoForwardPorts": false,
"python.linting.pylintEnabled": true,
"python.linting.enabled": true,
"python.formatting.provider": "black",
Expand Down
13 changes: 13 additions & 0 deletions .devcontainer/initialize.sh
Original file line number Diff line number Diff line change
@@ -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 changes: 17 additions & 0 deletions .devcontainer/post_create.sh
Original file line number Diff line number Diff line change
@@ -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
6 changes: 5 additions & 1 deletion .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,8 @@ core
*.mp4
*.jpg
*.db
*.ts
*.ts

web/dist/
web/node_modules/
web/.npm
6 changes: 3 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
28 changes: 25 additions & 3 deletions .github/workflows/pull_request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
69 changes: 56 additions & 13 deletions docker/Dockerfile → Dockerfile
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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 \
Expand Down Expand Up @@ -94,10 +94,12 @@ RUN --mount=type=bind,from=wheels,source=/wheels,target=/wheels \
fi \
# arch specific packages
&& if [ "${TARGETARCH}" = "amd64" ]; then \
echo 'deb https://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 https://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 \
Expand Down Expand Up @@ -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
Expand All @@ -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"]
24 changes: 11 additions & 13 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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

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: version
docker buildx build --tag frigate:latest --load .

local:
DOCKER_BUILDKIT=1 docker build -t frigate -f docker/Dockerfile .
build_web:
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
Expand Down
16 changes: 16 additions & 0 deletions config/config.yml.example
Original file line number Diff line number Diff line change
@@ -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
21 changes: 6 additions & 15 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down
Loading

0 comments on commit 6582504

Please sign in to comment.