Skip to content

Commit 6582504

Browse files
authored
Improve the devcontainer experience (blakeblackshear#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
1 parent 253061e commit 6582504

File tree

11 files changed

+191
-79
lines changed

11 files changed

+191
-79
lines changed

.devcontainer/devcontainer.json

+39-4
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,58 @@
11
{
2-
"name": "Frigate Dev",
2+
"name": "Frigate Devcontainer",
33
"dockerComposeFile": "../docker-compose.yml",
4-
"service": "dev",
5-
"workspaceFolder": "/lab/frigate",
4+
"service": "devcontainer",
5+
"workspaceFolder": "/workspace/frigate",
6+
"initializeCommand": ".devcontainer/initialize.sh",
7+
"postCreateCommand": ".devcontainer/post_create.sh",
8+
"overrideCommand": false,
9+
"remoteUser": "vscode",
10+
"features": {
11+
"ghcr.io/devcontainers/features/common-utils:1": {}
12+
},
13+
"forwardPorts": [5000, 5001, 5173, 1935, 8554, 8555],
14+
"portsAttributes": {
15+
"5000": {
16+
"label": "NGINX",
17+
"onAutoForward": "silent"
18+
},
19+
"5001": {
20+
"label": "Frigate API",
21+
"onAutoForward": "silent"
22+
},
23+
"5173": {
24+
"label": "Vite Server",
25+
"onAutoForward": "silent"
26+
},
27+
"1935": {
28+
"label": "RTMP",
29+
"onAutoForward": "silent"
30+
},
31+
"8554": {
32+
"label": "gortc RTSP",
33+
"onAutoForward": "silent"
34+
},
35+
"8555": {
36+
"label": "go2rtc WebRTC",
37+
"onAutoForward": "silent"
38+
}
39+
},
640
"extensions": [
41+
"ms-python.vscode-pylance",
742
"ms-python.python",
843
"visualstudioexptteam.vscodeintellicode",
944
"mhutchie.git-graph",
1045
"ms-azuretools.vscode-docker",
1146
"streetsidesoftware.code-spell-checker",
1247
"esbenp.prettier-vscode",
13-
"ms-python.vscode-pylance",
1448
"dbaeumer.vscode-eslint",
1549
"mikestead.dotenv",
1650
"csstools.postcss",
1751
"blanu.vscode-styled-jsx",
1852
"bradlc.vscode-tailwindcss"
1953
],
2054
"settings": {
55+
"remote.autoForwardPorts": false,
2156
"python.linting.pylintEnabled": true,
2257
"python.linting.enabled": true,
2358
"python.formatting.provider": "black",

.devcontainer/initialize.sh

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/bin/bash
2+
3+
set -euo pipefail
4+
5+
# These folders needs to be created and owned by the host user
6+
mkdir -p debug web/dist
7+
8+
if [[ -f "config/config.yml" ]]; then
9+
echo "config/config.yml already exists, skipping initialization" >&2
10+
else
11+
echo "initializing config/config.yml" >&2
12+
cp -fv config/config.yml.example config/config.yml
13+
fi

.devcontainer/post_create.sh

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#!/bin/bash
2+
3+
set -euxo pipefail
4+
5+
# Frigate normal container runs as root, so it have permission to create
6+
# the folders. But the devcontainer runs as the host user, so we need to
7+
# create the folders and give the host user permission to write to them.
8+
sudo mkdir -p /media/frigate
9+
sudo chown -R "$(id -u):$(id -g)" /media/frigate
10+
11+
make version
12+
13+
cd web
14+
15+
npm install
16+
17+
npm run build

.dockerignore

+5-1
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,8 @@ core
99
*.mp4
1010
*.jpg
1111
*.db
12-
*.ts
12+
*.ts
13+
14+
web/dist/
15+
web/node_modules/
16+
web/.npm

.github/workflows/ci.yml

+3-3
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ jobs:
2626
registry: ghcr.io
2727
username: ${{ github.actor }}
2828
password: ${{ secrets.GITHUB_TOKEN }}
29-
- name: Build web
30-
run: make build_web
3129
- name: Build image
32-
run: make push
30+
run: make push
31+
env:
32+
IMAGE_REPO: ghcr.io/${{ github.actor }}/frigate

.github/workflows/pull_request.yml

+25-3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,30 @@ env:
66
DEFAULT_PYTHON: 3.9
77

88
jobs:
9+
build_devcontainer:
10+
runs-on: ubuntu-latest
11+
name: Build Devcontainer
12+
# The Dockerfile contains features that requires buildkit, and since the
13+
# devcontainer cli uses docker-compose to build the image, the only way to
14+
# ensure docker-compose uses buildkit is to explicitly enable it.
15+
env:
16+
DOCKER_BUILDKIT: "1"
17+
steps:
18+
- uses: actions/checkout@v3
19+
- uses: actions/setup-node@master
20+
with:
21+
node-version: 16.x
22+
- name: Install devcontainer cli
23+
run: npm install --global @devcontainers/cli
24+
- name: Build devcontainer
25+
run: devcontainer build --workspace-folder .
26+
# It would be nice to also test the following commands, but for some
27+
# reason they don't work even though in VS Code devcontainer works.
28+
# - name: Start devcontainer
29+
# run: devcontainer up --workspace-folder .
30+
# - name: Run devcontainer scripts
31+
# run: devcontainer run-user-commands --workspace-folder .
32+
933
web_lint:
1034
name: Web - Lint
1135
runs-on: ubuntu-latest
@@ -36,7 +60,7 @@ jobs:
3660

3761
python_checks:
3862
runs-on: ubuntu-latest
39-
name: Python checks
63+
name: Python Checks
4064
steps:
4165
- name: Check out the repository
4266
uses: actions/checkout@v3
@@ -70,8 +94,6 @@ jobs:
7094
uses: docker/setup-qemu-action@v2
7195
- name: Set up Docker Buildx
7296
uses: docker/setup-buildx-action@v2
73-
- name: Create Version Module
74-
run: make version
7597
- name: Build
7698
run: make
7799
- name: Run mypy

docker/Dockerfile Dockerfile

+56-13
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# syntax=docker/dockerfile:1.2
22

3-
FROM blakeblackshear/frigate-nginx:1.0.2 as nginx
3+
FROM blakeblackshear/frigate-nginx:1.0.2 AS nginx
44

5-
FROM debian:11 as wheels
5+
FROM debian:11 AS wheels
66
ARG TARGETARCH
77

88
ENV DEBIAN_FRONTEND=noninteractive
@@ -44,8 +44,8 @@ RUN pip3 install -r requirements.txt
4444
COPY requirements-wheels.txt /requirements-wheels.txt
4545
RUN pip3 wheel --wheel-dir=/wheels -r requirements-wheels.txt
4646

47-
# Frigate Container
48-
FROM debian:11-slim
47+
# Frigate deps (ffmpeg, python, nginx, go2rtc, s6-overlay, etc)
48+
FROM debian:11-slim AS deps
4949
ARG TARGETARCH
5050

5151
# https://askubuntu.com/questions/972516/debian-frontend-environment-variable
@@ -65,7 +65,7 @@ RUN --mount=type=bind,from=wheels,source=/wheels,target=/wheels \
6565
gnupg \
6666
wget \
6767
procps \
68-
unzip tzdata libxml2 xz-utils \
68+
unzip locales tzdata libxml2 xz-utils \
6969
python3-pip \
7070
# add raspberry pi repo
7171
&& apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 9165938D90FDDD2E \
@@ -94,10 +94,12 @@ RUN --mount=type=bind,from=wheels,source=/wheels,target=/wheels \
9494
fi \
9595
# arch specific packages
9696
&& if [ "${TARGETARCH}" = "amd64" ]; then \
97-
echo 'deb http://deb.debian.org/debian testing main non-free' >> /etc/apt/sources.list.d/deb.list \
97+
# Use debian testing repo only for hwaccel packages
98+
echo 'deb http://deb.debian.org/debian testing main non-free' > /etc/apt/sources.list.d/debian-testing.list \
9899
&& apt-get -qq update \
99100
&& apt-get -qq install --no-install-recommends --no-install-suggests -y \
100-
mesa-va-drivers libva-drm2 intel-media-va-driver-non-free i965-va-driver libmfx1; \
101+
mesa-va-drivers libva-drm2 intel-media-va-driver-non-free i965-va-driver libmfx1 \
102+
&& rm -f /etc/apt/sources.list.d/debian-testing.list; \
101103
fi \
102104
&& if [ "${TARGETARCH}" = "arm64" ]; then \
103105
apt-get -qq install --no-install-recommends --no-install-suggests -y \
@@ -133,12 +135,6 @@ COPY labelmap.txt /labelmap.txt
133135
RUN wget -q https://github.com/google-coral/test_data/raw/release-frogfish/ssdlite_mobiledet_coco_qat_postprocess_edgetpu.tflite -O /edgetpu_model.tflite
134136
RUN wget -q https://github.com/google-coral/test_data/raw/release-frogfish/ssdlite_mobiledet_coco_qat_postprocess.tflite -O /cpu_model.tflite
135137

136-
WORKDIR /opt/frigate/
137-
ADD frigate frigate/
138-
ADD migrations migrations/
139-
140-
COPY web/dist web/
141-
142138
COPY docker/rootfs/ /
143139

144140
# s6-overlay
@@ -156,4 +152,51 @@ EXPOSE 8555
156152

157153
ENTRYPOINT ["/init"]
158154

155+
# Frigate deps with Node.js and NPM
156+
FROM deps AS deps-node
157+
158+
# Install Node 16
159+
RUN wget -qO- https://deb.nodesource.com/setup_16.x | bash - \
160+
&& apt-get install -y nodejs \
161+
&& npm install -g npm@9
162+
163+
# Devcontainer
164+
FROM deps-node AS devcontainer
165+
166+
WORKDIR /workspace/frigate
167+
168+
RUN apt-get update \
169+
&& apt-get install make -y \
170+
&& rm -rf /var/lib/apt/lists/*
171+
172+
RUN --mount=type=bind,source=./requirements-dev.txt,target=/workspace/frigate/requirements-dev.txt \
173+
pip3 install -r requirements-dev.txt
174+
175+
CMD ["sleep", "infinity"]
176+
177+
178+
# Frigate web build
179+
FROM deps-node AS web-build
180+
181+
WORKDIR /work
182+
COPY web/package.json web/package-lock.json ./
183+
RUN npm install
184+
185+
COPY web/ ./
186+
RUN npm run build
187+
188+
# Frigate web dist files
189+
FROM scratch AS web-dist
190+
191+
COPY --from=web-build /work/dist/ /
192+
193+
194+
# Frigate final container
195+
FROM deps
196+
197+
WORKDIR /opt/frigate/
198+
COPY frigate frigate/
199+
COPY migrations migrations/
200+
COPY --from=web-dist / web/
201+
159202
CMD ["python3", "-u", "-m", "frigate"]

Makefile

+11-13
Original file line numberDiff line numberDiff line change
@@ -2,35 +2,33 @@ default_target: local
22

33
COMMIT_HASH := $(shell git log -1 --pretty=format:"%h"|tail -1)
44
VERSION = 0.12.0
5+
IMAGE_OWNER ?= ghcr.io/blakeblackshear/frigate
56
CURRENT_UID := $(shell id -u)
67
CURRENT_GID := $(shell id -g)
78

89
version:
9-
echo "VERSION=\"$(VERSION)-$(COMMIT_HASH)\"" > frigate/version.py
10+
echo 'VERSION = "$(VERSION)-$(COMMIT_HASH)"' > frigate/version.py
1011

11-
build_web:
12-
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"
13-
14-
nginx_frigate:
15-
docker buildx build --push --platform linux/arm/v7,linux/arm64/v8,linux/amd64 --tag blakeblackshear/frigate-nginx:1.0.2 --file docker/Dockerfile.nginx .
12+
local: version
13+
docker buildx build --tag frigate:latest --load .
1614

17-
local:
18-
DOCKER_BUILDKIT=1 docker build -t frigate -f docker/Dockerfile .
15+
build_web:
16+
docker buildx build --target web-dist --output web/dist .
1917

2018
amd64:
21-
docker buildx build --platform linux/amd64 --tag blakeblackshear/frigate:$(VERSION)-$(COMMIT_HASH) --file docker/Dockerfile .
19+
docker buildx build --platform linux/amd64 --tag $(IMAGE_REPO):$(VERSION)-$(COMMIT_HASH) .
2220

2321
arm64:
24-
docker buildx build --platform linux/arm64 --tag blakeblackshear/frigate:$(VERSION)-$(COMMIT_HASH) --file docker/Dockerfile .
22+
docker buildx build --platform linux/arm64 --tag $(IMAGE_REPO):$(VERSION)-$(COMMIT_HASH) .
2523

2624
armv7:
27-
docker buildx build --platform linux/arm/v7 --tag blakeblackshear/frigate:$(VERSION)-$(COMMIT_HASH) --file docker/Dockerfile .
25+
docker buildx build --platform linux/arm/v7 --tag $(IMAGE_REPO):$(VERSION)-$(COMMIT_HASH) .
2826

2927
build: version amd64 arm64 armv7
30-
docker buildx build --platform linux/arm/v7,linux/arm64/v8,linux/amd64 --tag blakeblackshear/frigate:$(VERSION)-$(COMMIT_HASH) --file docker/Dockerfile .
28+
docker buildx build --platform linux/arm/v7,linux/arm64/v8,linux/amd64 --tag $(IMAGE_REPO):$(VERSION)-$(COMMIT_HASH) .
3129

3230
push: build
33-
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 .
31+
docker buildx build --push --platform linux/arm/v7,linux/arm64/v8,linux/amd64 --tag $(IMAGE_REPO):${GITHUB_REF_NAME}-$(COMMIT_HASH) .
3432

3533
run_tests: frigate
3634
docker run --rm --entrypoint=python3 frigate:latest -u -m unittest

config/config.yml.example

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
mqtt:
2+
host: mqtt
3+
4+
cameras:
5+
test:
6+
ffmpeg:
7+
inputs:
8+
- path: /media/frigate/car-stopping.mp4
9+
input_args: -re -stream_loop -1 -fflags +genpts
10+
roles:
11+
- detect
12+
- rtmp
13+
detect:
14+
height: 1080
15+
width: 1920
16+
fps: 5

docker-compose.yml

+6-15
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
version: "3"
22
services:
3-
dev:
4-
container_name: frigate-dev
5-
user: vscode
3+
devcontainer:
4+
container_name: frigate-devcontainer
65
# add groups from host for render, plugdev, video
76
group_add:
87
- "109" # render
@@ -12,25 +11,17 @@ services:
1211
shm_size: "256mb"
1312
build:
1413
context: .
15-
dockerfile: docker/Dockerfile.dev
14+
target: devcontainer
1615
devices:
1716
- /dev/bus/usb:/dev/bus/usb
18-
- /dev/dri:/dev/dri # for intel hwaccel, needs to be updated for your hardware
17+
# - /dev/dri:/dev/dri # for intel hwaccel, needs to be updated for your hardware
1918
volumes:
19+
- .:/workspace/frigate:cached
20+
- ./web/dist:/opt/frigate/web:cached
2021
- /etc/localtime:/etc/localtime:ro
21-
- .:/lab/frigate:cached
2222
- ./config/config.yml:/config/config.yml:ro
2323
- ./debug:/media/frigate
2424
- /dev/bus/usb:/dev/bus/usb
25-
ports:
26-
- "1935:1935"
27-
- "3000:3000"
28-
- "5000:5000"
29-
- "5001:5001"
30-
- "8080:8080"
31-
- "8554:8554"
32-
entrypoint: ["sudo", "/init"]
33-
command: /bin/sh -c "while sleep 1000; do :; done"
3425
mqtt:
3526
container_name: mqtt
3627
image: eclipse-mosquitto:1.6

0 commit comments

Comments
 (0)