From c1af47eb26e1b62c01b1733b3d3d4660b7f50512 Mon Sep 17 00:00:00 2001 From: Samuel ORTION Date: Mon, 22 Aug 2022 18:49:40 +0200 Subject: [PATCH] docker: Update docker-compose and ./docker Dockerfiles --- .dockerignore | 3 + .env | 10 ++-- .env.example | 12 ++++ docker-compose.yml | 96 +++++++++++++++++++++----------- docker/nginx/Dockerfile | 28 ++++++---- docker/nginx/nginx.conf.template | 6 +- docker/php-fpm/Dockerfile | 8 +++ docker/symfony/Dockerfile | 34 +++-------- docs/DOCKER.md | 7 ++- 9 files changed, 126 insertions(+), 78 deletions(-) create mode 100644 .env.example create mode 100644 docker/php-fpm/Dockerfile diff --git a/.dockerignore b/.dockerignore index 1cac819..61caea9 100644 --- a/.dockerignore +++ b/.dockerignore @@ -5,3 +5,6 @@ var media daemon/systemd analyzer +vendor +node_modules +build \ No newline at end of file diff --git a/.env b/.env index cc93515..b842c9e 100644 --- a/.env +++ b/.env @@ -3,10 +3,10 @@ CUDA_VISIBLE_DEVICES="" # BirdNET-stream environment DATABASE_USER="birdnet" -DATABASE_PASSWORD="secret" +DATABASE_PASSWORD="AShoo1N" DATABASE_PORT="3306" # Change this if you have already a running MySQL server on the host -MYSQL_ROOT_PASSWORD="secret" +DATABASE_ROOT_PASSWORD="Ojiex5v" -RECORDS_FOLDER="./var/chunks" -CHARTS_FOLDER="./var/charts" -SERVER_NAME="birdnet.lan" \ No newline at end of file +RECORDS_FOLDER=/media/DATA/birdnet/records/ +CHARTS_FOLDER=/media/DATA/birdnet/charts/ +SERVER_NAME="birdnet.local" \ No newline at end of file diff --git a/.env.example b/.env.example new file mode 100644 index 0000000..2637633 --- /dev/null +++ b/.env.example @@ -0,0 +1,12 @@ +# BirdNET-Analyzer environment +CUDA_VISIBLE_DEVICES="" + +# BirdNET-stream environment +DATABASE_USER="birdnet" +DATABASE_PASSWORD="secret" # change this +DATABASE_PORT="3306" # change this if you have already a running MySQL server on the host +MYSQL_ROOT_PASSWORD="secret" # change this + +RECORDS_FOLDER="/media/data/birdnet/records" +CHARTS_FOLDER="/media/data/birdnet/charts" +SERVER_NAME="birdnet.local" \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml index d1acb62..2ba65ab 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -10,67 +10,101 @@ services: # environment: # - CHUNK_FOLDER=${CHUNK_FOLDER:-/media/birdnet/records} # volumes: - # - ./var/:/media/birdnet/records + # - ${RECORDS_FOLDER:-/media/birdnet/records}:${RECORS_FOLDER:-/media/birdnet/records} # # Allow container to access to the hosts microphone # devices: - # - /dev/snd - + # - /dev/snd:/dev/snd # analyzer: # container_name: birdnet_analyzer # build: # context: ./analyzer/ # dockerfile: ./Dockerfile + db: + container_name: birdnet_database + image: mariadb:latest + command: mysqld --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci + ports: + - '3306:3306' + networks: + birdnet_network: + environment: + MYSQL_ROOT_PASSWORD: ${DATABASE_ROOT_PASSWORD:-secret}' + MYSQL_DATABASE: ${DATABASE:-birdnet} + MYSQL_USER: ${DATABASE_USER:-birdnet} + MYSQL_PASSWORD: ${DATABASE_PASSWORD:-secret} + volumes: + - ./docker/database/init:/docker-entrypoint-initdb.d + restart: unless-stopped - # db: - # container_name: birdnet_database - # image: mariadb:latest - # command: mysqld --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci - # ports: - # - '3307:3306' - # networks: - # - birdnet_network - # environment: - # MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD:-secret}' - # MYSQL_DATABASE: ${DATABASE:-birdnet} - # MYSQL_USER: ${DATABASE_USER:-birdnet} - # MYSQL_PASSWORD: ${DATABASE_PASSWORD:-secret} - # volumes: - # - ./docker/database/init:/docker-entrypoint-initdb.d - # restart: unless-stopped + php-fpm: + container_name: birdnet_php-fpm + image: php:${PHP_VERSION:-8.1}-fpm + ports: + - ${PHP_FPM_PORT:-9000}:9000 + networks: + birdnet_network: + restart: unless-stopped + volumes: + - birdnet_app:${PROJECT_ROOT:-/opt/birdnet} symfony: container_name: birdnet_symfony - hostname: birdnet.symfony + networks: + birdnet_network: build: context: . dockerfile: ./docker/symfony/Dockerfile environment: + - DATABASE_DEFAULT_URL=${DATABASE_DEFAULT_URL:-mysql://${DATABASE_USER:-birdnet}:${DATABASE_PASSWORD:-secret}@${DATABASE_HOST:-db}:3306/birdnet_default} + - DATABASE_OBSERVATION_URL=${DATABASE_OBSERVATION_URL:-mysql://${DATABASE_USER:-birdnet}:${DATABASE_PASSWORD:-secret}@${DATABASE_HOST:-db}:3306/birdnet_observation} + - RECORDS_FOLDER=${RECORDS_FOLDER:-/media/birdnet/records} restart: unless-stopped volumes: - networks: - - birdnet_network + - birdnet_app:${PROJECT_ROOT:-/opt/birdnet} + - birdnet_recordings:${RECORDS_FOLDER:-/media/birdnet/records} depends_on: - db - + nginx: container_name: birdnet_nginx - hostname: birdnet.nginx build: context: . dockerfile: ./docker/nginx/Dockerfile + args: + - SERVER_NAME=${SERVER_NAME:-birnet.local} + - SYMFONY_PUBLIC=${SYMFONY_PUBLIC:-/opt/birdnet/www/public} + - CHARTS_FOLDER=${CHARTS_FOLDER:-/media/birdnet/charts} + - RECORDS_FOLDER=${RECORDS_FOLDER:-/media/birdnet/records} + - PHP_FPM_HOST=${PHP_FPM_HOST:-php-fpm} + - PHP_FPM_PORT=${PHP_FPM_PORT:-9000} ports: - - '81:80' - - '444:443' - environment: - - PHP_FPM_PORT=${PHP_FPM_PORT:-9001} - - PHP_FPM_HOST=${PHP_FPM_HOST:-birdnet.php-fpm} + - '80:80' + - '443:443' + volumes: + - birdnet_app:${RECORDS_FOLDER-/opt/birdnet} + - birdnet_recordings:${RECORDS_FOLDER-/media/data/records} networks: - - birdnet_network + birdnet_network: + ipv4_address: ${IP_ADDRESS:-172.25.0.101} + aliases: + - ${SERVER_NAME:-birdnet.local} restart: unless-stopped + depends_on: + - symfony + - php-fpm networks: birdnet_network: + ipam: + config: + - subnet: ${IP_SUBNET:-172.25.0.0/24} volumes: - birdnet: \ No newline at end of file + birdnet_app: + + birdnet_recordings: + driver_opts: + type: none + device: ${RECORDS_FOLDER:-/media/data/records} + o: bind \ No newline at end of file diff --git a/docker/nginx/Dockerfile b/docker/nginx/Dockerfile index 4cbf3f0..ab567b7 100644 --- a/docker/nginx/Dockerfile +++ b/docker/nginx/Dockerfile @@ -1,24 +1,30 @@ -FROM nginx:latest +FROM nginx -ENV PHP_FPM_PORT=${PHP_FPM_PORT:-9000} +ARG SERVER_NAME +ARG PROJECT_ROOT +ARG SYMFONY_PUBLIC +ARG CHARTS_FOLDER +ARG RECORDS_FOLDER +ARG PHP_FPM_HOST +ARG PHP_FPM_PORT -ENV SERVER_NAME=${SERVER_NAME:-birdnet.lan} -ENV PROJECT_ROOT=${PROJECT_ROOT:-/opt/birdnet} -ENV SYMFONY_PUBLIC=${SYMFONY_PUBLIC:-${PROJECT_ROOT}/www/public} +RUN apt-get update && apt-get upgrade -y \ + && apt-get install -y nginx-full USER root -COPY docker/nginx/nginx.conf.template /etc/nginx/sites-available/birdnet.conf -RUN ln -s /etc/nginx/sites-available/birdnet.conf /etc/nginx/sites-enabled/birdnet.conf \ - && sed -i "s||${SERVER_NAME}|g" /etc/nginx/sites-available/birdnet.conf \ +COPY "docker/nginx/nginx.conf.template" "/etc/nginx/sites-available/birdnet.conf" +RUN ln -s /etc/nginx/sites-available/birdnet.conf /etc/nginx/sites-enabled/birdnet.conf +RUN sed -i "s||${SERVER_NAME}|g" /etc/nginx/sites-available/birdnet.conf \ + && sed -i "s||${PHP_FPM_HOST}|g" /etc/nginx/sites-available/birdnet.conf \ + && sed -i "s||${PHP_FPM_PORT}|g" /etc/nginx/sites-available/birdnet.conf \ && sed -i "s||${SYMFONY_PUBLIC}|g" /etc/nginx/sites-available/birdnet.conf \ - && sed -i "s||${RECORDS_DIR}|g" /etc/nginx/sites-available/birdnet.conf \ - && sed -i "s||${CHARTS_DIR}|g" /etc/nginx/sites-available/birdnet.conf + && sed -i "s||${RECORDS_FOLDER}|g" /etc/nginx/sites-available/birdnet.conf \ + && sed -i "s||${CHARTS_FOLDER}|g" /etc/nginx/sites-available/birdnet.conf RUN mkdir -p /etc/nginx/certs/birdnet WORKDIR /etc/nginx/certs/birdnet RUN openssl req -x509 -newkey rsa:4096 -keyout privkey.pem -out fullchain.pem -sha256 -days 365 -nodes --subj "/CN=${SERVER_NAME}" RUN sed -i "s||/etc/nginx/certs/birdnet/fullchain.pem|g" /etc/nginx/sites-available/birdnet.conf \ && sed -i "s||/etc/nginx/certs/birdnet/privkey.pem|g" /etc/nginx/sites-available/birdnet.conf -RUN nginx -t EXPOSE 443 EXPOSE 80 diff --git a/docker/nginx/nginx.conf.template b/docker/nginx/nginx.conf.template index 03cc61f..b69328e 100644 --- a/docker/nginx/nginx.conf.template +++ b/docker/nginx/nginx.conf.template @@ -42,15 +42,11 @@ server { location ~ \.php$ { fastcgi_split_path_info ^(.+\.php)(/.+)$; - fastcgi_pass unix:/run/php/php8.1-fpm.sock; + fastcgi_pass :; fastcgi_index index.php; include fastcgi.conf; } - location /stream { - proxy_pass http://localhost:8000/stream; - } - access_log /var/log/nginx/birdnet-access.log; error_log /var/log/nginx/birdnet-error.log error; } diff --git a/docker/php-fpm/Dockerfile b/docker/php-fpm/Dockerfile new file mode 100644 index 0000000..388ef27 --- /dev/null +++ b/docker/php-fpm/Dockerfile @@ -0,0 +1,8 @@ +ARG PHP_VERSION=${PHP_VERSION:-8.1} + +FROM php:${PHP_VERSION}-fpm + +RUN apt-get update && apt-get upgrade -y +RUN docker-php-ext-install pdo_mysql + +EXPOSE 9000 \ No newline at end of file diff --git a/docker/symfony/Dockerfile b/docker/symfony/Dockerfile index b14f3f9..967362e 100644 --- a/docker/symfony/Dockerfile +++ b/docker/symfony/Dockerfile @@ -1,11 +1,12 @@ ARG PHP_VERSION=${PHP_VERSION:-8.1} + FROM php:${PHP_VERSION} -ENV PROJECT_ROOT=${PROJECT_ROOT:-/opt/birdnet} -ENV SYMFONY_PUBLIC=${SYMFONY_PUBLIC:-${PROJECT_ROOT}/www/public} + +ARG PROJECT_ROOT=${PROJECT_ROOT:-/opt/birdnet} +ARG NODE_VERSION=${NODE_VERSION:-16.17.0} RUN apt-get update && apt-get upgrade -y \ && apt-get install -y \ - nginx \ curl \ gzip \ git \ @@ -16,8 +17,7 @@ RUN apt-get update && apt-get upgrade -y \ RUN php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');" \ && php composer-setup.php --install-dir=/usr/local/bin --filename=composer - -ENV NODE_VERSION="16.17.0" +# Install nodejs and npm ENV NVM_DIR="/usr/local/nvm" RUN mkdir ${NVM_DIR} RUN curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash @@ -28,29 +28,13 @@ RUN . "$NVM_DIR/nvm.sh" \ && npm install -g yarn ENV PATH="$PATH:/usr/local/nvm/versions/node/v${NODE_VERSION}/bin" -WORKDIR ${PROJECT_ROOT} +COPY . ${PROJECT_ROOT} RUN chown -R www-data:www-data "${PROJECT_ROOT}" -# Install composer dependencies -USER www-data WORKDIR ${PROJECT_ROOT}/www +USER www-data +# Install composer packages RUN composer install # Install yarn dependencies RUN . "$NVM_DIR/nvm.sh" && yarn install && yarn build -USER root -COPY docker/www/nginx.conf.template /etc/nginx/sites-available/birdnet.conf -RUN ln -s /etc/nginx/sites-available/birdnet.conf /etc/nginx/sites-enabled/birdnet.conf \ - && sed -i "s||${SERVER_NAME}|g" /etc/nginx/sites-available/birdnet.conf \ - && sed -i "s||${SYMFONY_PUBLIC}|g" /etc/nginx/sites-available/birdnet.conf \ - && sed -i "s||${RECORDS_DIR}|g" /etc/nginx/sites-available/birdnet.conf \ - && sed -i "s||${CHARTS_DIR}|g" /etc/nginx/sites-available/birdnet.conf -RUN mkdir -p /etc/nginx/certs/birdnet -WORKDIR /etc/nginx/certs/birdnet -RUN openssl req -x509 -newkey rsa:4096 -keyout privkey.pem -out fullchain.pem -sha256 -days 365 -nodes --subj "/CN=${SERVER_NAME}" -RUN sed -i "s||/etc/nginx/certs/birdnet/fullchain.pem|g" /etc/nginx/sites-available/birdnet.conf \ - && sed -i "s||/etc/nginx/certs/birdnet/privkey.pem|g" /etc/nginx/sites-available/birdnet.conf -RUN nginx -t -EXPOSE 443 -EXPOSE 80 -CMD [""] -CMD ["nginx", "-g", "daemon off;"] \ No newline at end of file +CMD ["bash"] \ No newline at end of file diff --git a/docs/DOCKER.md b/docs/DOCKER.md index 79555c5..1baa8d6 100644 --- a/docs/DOCKER.md +++ b/docs/DOCKER.md @@ -31,7 +31,12 @@ git clone -b main https://github.com/UncleSamulus/BirdNET-stream.git . Then, run docker-compose: ```bash -docker-compose up +# Build image (first time only) +docker compose up --build +# Run +docker compose up -d +# Stop +docker compose down ``` ## Building and running each of the containers