Skip to content

Commit b6726c2

Browse files
committed
[Recognizer] Add docker image
1 parent d94378b commit b6726c2

File tree

12 files changed

+324
-20
lines changed

12 files changed

+324
-20
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
on:
2+
workflow_call:
3+
inputs:
4+
app-name:
5+
required: true
6+
type: string
7+
8+
env:
9+
REGISTRY: ghcr.io
10+
11+
jobs:
12+
build-publish-image:
13+
runs-on: ubuntu-latest
14+
steps:
15+
- uses: actions/checkout@v3
16+
- name: Set up Docker Buildx
17+
uses: docker/setup-buildx-action@v2
18+
- name: Login to Container Registry
19+
uses: docker/login-action@v2
20+
with:
21+
registry: ${{ env.REGISTRY }}
22+
username: ${{ github.actor }}
23+
password: ${{ secrets.GITHUB_TOKEN }}
24+
- name: Extract metadata (tags, labels) for Docker
25+
id: meta
26+
uses: docker/metadata-action@v4
27+
with:
28+
images: ${{ env.REGISTRY }}/${{ github.repository }}/${{ inputs.app-name }}
29+
tags: type=match,pattern=${{ inputs.app-name }}-v(\d.\d.\d)
30+
- name: Build and push Docker image
31+
uses: docker/build-push-action@v4
32+
with:
33+
context: ./${{ inputs.app-name }}
34+
platforms: linux/amd64
35+
push: true
36+
tags: ${{ steps.meta.outputs.tags }}
37+
cache-from: type=gha
38+
cache-to: type=gha,mode=max

.github/workflows/deploy-image.yml

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
on:
2+
workflow_call:
3+
inputs:
4+
app-name:
5+
required: true
6+
type: string
7+
secrets:
8+
ssh-host:
9+
required: true
10+
ssh-username:
11+
required: true
12+
ssh-priv-key:
13+
required: true
14+
15+
jobs:
16+
deploy-image:
17+
runs-on: ubuntu-latest
18+
steps:
19+
- uses: actions/checkout@v3
20+
- name: Deploy the new version
21+
uses: appleboy/[email protected]
22+
with:
23+
host: ${{ secrets.ssh-host }}
24+
username: ${{ secrets.ssh-username }}
25+
key: ${{ secrets.ssh-priv-key }}
26+
script: |
27+
docker run \
28+
-e SECRET_KEY_BASE=${{ secrets.RECOGNIZER_SECRET_KEY_BASE }} \
29+
-e PHX_HOST=${{ secrets.RECOGNIZER_PHX_HOST }} \
30+
--network host \
31+
ghcr.io/elixir-webrtc/apps/${{ inputs.app-name }}:${{ github.ref_name }#-v}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
name: Recognizer Docker
2+
3+
on:
4+
push:
5+
tags:
6+
- "recognizer-v*.*.*"
7+
8+
permissions:
9+
contents: read
10+
packages: write
11+
12+
jobs:
13+
build-publish-recognizer-image:
14+
uses: ./.github/workflows/build-publish-image.yml
15+
with:
16+
app-name: recognizer
17+
deploy-recognizer:
18+
# needs: build-publish-recognizer-image
19+
uses: ./.github/workflows/deploy-image.yml
20+
with:
21+
app-name: recognizer
22+
secrets:
23+
ssh-host: ${{ secrets.RECOGNIZER_SSH_HOST }}
24+
ssh-username: ${{ secrets.RECOGNIZER_SSH_USERNAME }}
25+
ssh-priv-key: ${{ secrets.RECOGNIZER_SSH_PRIV_KEY }}
26+
27+

recognizer/.dockerignore

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# This file excludes paths from the Docker build context.
2+
#
3+
# By default, Docker's build context includes all files (and folders) in the
4+
# current directory. Even if a file isn't copied into the container it is still sent to
5+
# the Docker daemon.
6+
#
7+
# There are multiple reasons to exclude files from the build context:
8+
#
9+
# 1. Prevent nested folders from being copied into the container (ex: exclude
10+
# /assets/node_modules when copying /assets)
11+
# 2. Reduce the size of the build context and improve build time (ex. /build, /deps, /doc)
12+
# 3. Avoid sending files containing sensitive information
13+
#
14+
# More information on using .dockerignore is available here:
15+
# https://docs.docker.com/engine/reference/builder/#dockerignore-file
16+
17+
.dockerignore
18+
19+
# Ignore git, but keep git HEAD and refs to access current commit hash if needed:
20+
#
21+
# $ cat .git/HEAD | awk '{print ".git/"$2}' | xargs cat
22+
# d0b8727759e1e0e7aa3d41707d12376e373d5ecc
23+
.git
24+
!.git/HEAD
25+
!.git/refs
26+
27+
# Common development/test artifacts
28+
/cover/
29+
/doc/
30+
/test/
31+
/tmp/
32+
.elixir_ls
33+
34+
# Mix artifacts
35+
/_build/
36+
/deps/
37+
*.ez
38+
39+
# Generated on crash by the VM
40+
erl_crash.dump
41+
42+
# Static artifacts - These should be fetched and built inside the Docker image
43+
/assets/node_modules/
44+
/priv/static/assets/
45+
/priv/static/cache_manifest.json

recognizer/Dockerfile

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
# Find eligible builder and runner images on Docker Hub. We use Ubuntu/Debian
2+
# instead of Alpine to avoid DNS resolution issues in production.
3+
#
4+
# https://hub.docker.com/r/hexpm/elixir/tags?page=1&name=ubuntu
5+
# https://hub.docker.com/_/ubuntu?tab=tags
6+
#
7+
# This file is based on these images:
8+
#
9+
# - https://hub.docker.com/r/hexpm/elixir/tags - for the build image
10+
# - https://hub.docker.com/_/debian?tab=tags&page=1&name=bullseye-20231009-slim - for the release image
11+
# - https://pkgs.org/ - resource for finding needed packages
12+
# - Ex: hexpm/elixir:1.16.0-erlang-26.2.1-debian-bullseye-20231009-slim
13+
#
14+
ARG ELIXIR_VERSION=1.17.2
15+
ARG OTP_VERSION=27.0.1
16+
ARG DEBIAN_VERSION=bookworm-20240701-slim
17+
18+
ARG BUILDER_IMAGE="hexpm/elixir:${ELIXIR_VERSION}-erlang-${OTP_VERSION}-debian-${DEBIAN_VERSION}"
19+
ARG RUNNER_IMAGE="debian:${DEBIAN_VERSION}"
20+
21+
FROM ${BUILDER_IMAGE} as builder
22+
23+
# install build dependencies
24+
RUN apt-get update -y && apt-get install -y \
25+
build-essential \
26+
libssl-dev \
27+
curl \
28+
pkg-config \
29+
git \
30+
libsrtp2-dev \
31+
libavcodec-dev \
32+
libavformat-dev \
33+
libavutil-dev \
34+
libswscale-dev \
35+
libavdevice-dev && \
36+
apt-get clean && \
37+
rm -f /var/lib/apt/lists/*_*
38+
39+
# prepare build dir
40+
WORKDIR /app
41+
42+
# install hex + rebar
43+
RUN mix local.hex --force && \
44+
mix local.rebar --force
45+
46+
# set build ENV
47+
ENV MIX_ENV="prod"
48+
49+
# install mix dependencies
50+
COPY mix.exs mix.lock ./
51+
RUN mix deps.get --only $MIX_ENV
52+
RUN mkdir config
53+
54+
# copy compile-time config files before we compile dependencies
55+
# to ensure any relevant config change will trigger the dependencies
56+
# to be re-compiled.
57+
COPY config/config.exs config/${MIX_ENV}.exs config/
58+
RUN mix deps.compile
59+
60+
COPY priv priv
61+
62+
COPY lib lib
63+
64+
COPY assets assets
65+
66+
# compile assets
67+
RUN mix assets.deploy
68+
69+
# Compile the release
70+
RUN mix compile
71+
72+
# Changes to config/runtime.exs don't require recompiling the code
73+
COPY config/runtime.exs config/
74+
75+
COPY rel rel
76+
RUN mix release
77+
78+
# start a new build stage so that the final image will only contain
79+
# the compiled release and other runtime necessities
80+
FROM ${RUNNER_IMAGE}
81+
82+
RUN apt-get update -y && \
83+
apt-get install -y libstdc++6 \
84+
openssl \
85+
libncurses5 \
86+
locales \
87+
ca-certificates \
88+
libsrtp2-dev \
89+
libavcodec-dev \
90+
libavformat-dev \
91+
libavutil-dev \
92+
libswscale-dev \
93+
libavdevice-dev \
94+
&& apt-get clean \
95+
&& rm -f /var/lib/apt/lists/*_*
96+
97+
# Set the locale
98+
RUN sed -i '/en_US.UTF-8/s/^# //g' /etc/locale.gen && locale-gen
99+
100+
ENV LANG en_US.UTF-8
101+
ENV LANGUAGE en_US:en
102+
ENV LC_ALL en_US.UTF-8
103+
104+
WORKDIR "/app"
105+
RUN chown nobody /app
106+
107+
# set runner ENV
108+
ENV MIX_ENV="prod"
109+
110+
# without setting this, bumblebee tries to use /nonexistent directory,
111+
# which does not exist and cannot be created
112+
ENV BUMBLEBEE_CACHE_DIR=/app/bin
113+
114+
# Only copy the final release from the build stage
115+
COPY --from=builder --chown=nobody:root /app/_build/${MIX_ENV}/rel/recognizer ./
116+
117+
USER nobody
118+
119+
# If using an environment that doesn't automatically reap zombie processes, it is
120+
# advised to add an init process such as tini via `apt-get install`
121+
# above and adding an entrypoint. See https://github.com/krallin/tini for details
122+
# ENTRYPOINT ["/tini", "--"]
123+
124+
CMD ["/app/bin/server"]

recognizer/README.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,29 @@ To start your Phoenix server:
88
* Start Phoenix endpoint with `mix phx.server` or inside IEx with `iex -S mix phx.server`
99

1010
Now you can visit [`localhost:5002`](http://localhost:5002) from your browser.
11+
12+
## Running with Docker
13+
14+
You can also run Recognizer using Docker.
15+
16+
Build image:
17+
18+
```
19+
docker build -t recognizer .
20+
```
21+
22+
and run:
23+
24+
```
25+
docker run -e SECRET_KEY_BASE="secret" -e PHX_HOST=localhost --network host recognizer
26+
```
27+
28+
Note that secret has to be at least 64 bytes long.
29+
You can generate one with `mix phx.gen.secret`.
30+
31+
If you are running on MacOS, instead of using `--network host` option, you have to explicitly publish ports:
32+
33+
```
34+
docker run -e SECRET_KEY_BASE="secert" -e PHX_HOST=localhost -p 4000:4000 -p 50000-50010/udp recognizer
35+
```
36+

recognizer/config/config.exs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ config :nx, default_backend: EXLA.Backend
5555

5656
config :recognizer, max_rooms: 5, max_session_time_s: 200
5757

58+
config :bundlex, :disable_precompiled_os_deps, apps: [:ex_libsrtp]
59+
5860
# Import environment specific config. This must remain at the bottom
5961
# of this file so it overrides the configuration defined above.
6062
import_config "#{config_env()}.exs"

recognizer/lib/recognizer/application.ex

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,13 @@ defmodule Recognizer.Application do
1616
end
1717

1818
defp commit() do
19-
case System.cmd("git", ["rev-parse", "--short", "HEAD"]) do
20-
{hash, 0} -> "(#{String.trim(hash)})"
21-
_ -> ""
19+
try do
20+
case System.cmd("git", ["rev-parse", "--short", "HEAD"]) do
21+
{hash, 0} -> "(#{String.trim(hash)})"
22+
_ -> ""
23+
end
24+
catch
25+
_, _ -> ""
2226
end
2327
end
2428

recognizer/mix.exs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,9 @@ defmodule Recognizer.MixProject do
5454
{:plug_cowboy, "~> 2.5"},
5555
{:ex_webrtc, "~> 0.3.0"},
5656
{:ex_webrtc_dashboard, "~> 0.3.0"},
57-
{:xav, "~> 0.3.0"},
58-
{:bumblebee, "~> 0.4.2"},
59-
{:exla, "~> 0.5"},
57+
{:xav, "~> 0.4.0"},
58+
{:bumblebee, "~> 0.5.3"},
59+
{:exla, "~> 0.7.1"},
6060

6161
# Dialyzer and credo
6262
{:dialyxir, ">= 0.0.0", only: :dev, runtime: false},

0 commit comments

Comments
 (0)