Merge pull request #3 from SergeantPanda/main

Adds the ability to run everything in one docker container.

Should make it easier to run in VMs and LXC containers as well.
This commit is contained in:
Dispatcharr 2025-02-22 23:05:57 -06:00 committed by GitHub
commit 3a15cf6b7f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 205 additions and 3 deletions

27
.dockerignore Executable file
View file

@ -0,0 +1,27 @@
**/__pycache__
**/.venv
**/.classpath
**/.dockerignore
**/.env
**/.git
**/.gitignore
**/.project
**/.settings
**/.toolstarget
**/.vs
**/.vscode
**/*.*proj.user
**/*.dbmdl
**/*.jfm
**/bin
**/charts
**/docker-compose*
**/compose*
**/Dockerfile*
**/node_modules
**/npm-debug.log
**/obj
**/secrets.dev.yaml
**/values.dev.yaml
LICENSE
README.md

0
.gitattributes vendored Normal file → Executable file
View file

3
.gitignore vendored Normal file → Executable file
View file

@ -1,4 +1,7 @@
.DS_Store
__pycache__/
**/__pycache__/
.vscode/launch.json
.vscode/tasks.json
*.pyc

5
.vscode/settings.json vendored Executable file
View file

@ -0,0 +1,5 @@
{
"docker.imageBuildContextPath": "${workspaceFolder}"
//"docker.imageBuildContextPath": "/MainPool/Main/Vital_Data/Scripts/Dispatcharr/Dispatcharr"
}

View file

@ -68,8 +68,8 @@ DATABASES = {
'NAME': os.environ.get('POSTGRES_DB', 'dispatcharr'),
'USER': os.environ.get('POSTGRES_USER', 'dispatch'),
'PASSWORD': os.environ.get('POSTGRES_PASSWORD', 'secret'),
'HOST': os.environ.get('POSTGRES_HOST', 'db'),
'PORT': os.environ.get('POSTGRES_PORT', 5432),
'HOST': os.environ.get('POSTGRES_HOST', 'localhost'),
'PORT': int(os.environ.get('POSTGRES_PORT', 5432)),
}
}
@ -108,4 +108,4 @@ MEDIA_ROOT = BASE_DIR / 'media'
MEDIA_URL = '/media/'
#SERVER_IP = "10.0.0.107"
SERVER_IP = "127.0.0.1"

45
docker/DockerfileAIO Executable file
View file

@ -0,0 +1,45 @@
FROM python:3.10-slim
# Add PostgreSQL repository
RUN apt-get update && apt-get install -y wget gnupg2 && \
echo "deb http://apt.postgresql.org/pub/repos/apt/ bookworm-pgdg main" > /etc/apt/sources.list.d/pgdg.list && \
wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | apt-key add -
# Install required packages
RUN apt-get update && apt-get install -y \
ffmpeg \
streamlink \
vlc \
libpq-dev \
gcc \
postgresql-14 \
postgresql-contrib-14 \
redis-server \
&& rm -rf /var/lib/apt/lists/*
# Set the working directory
WORKDIR /app
# Copy requirements.txt from the parent directory
COPY requirements.txt /app/
# Install Python dependencies
RUN pip install --no-cache-dir -r /app/requirements.txt
# Copy the application source code from the parent directory
COPY . /app/
# Set environment variables
ENV DJANGO_SETTINGS_MODULE=dispatcharr.settings
ENV PYTHONUNBUFFERED=1
ENV SSL_CERT_FILE=/etc/ssl/certs/ca-certificates.crt
# Copy the entrypoint script
COPY docker/entrypoint.sh /app/entrypoint.sh
RUN chmod +x /app/entrypoint.sh
# Expose the port
EXPOSE 9191
# Command to run the startup script
CMD ["/app/entrypoint.sh"]

View file

@ -49,6 +49,8 @@ services:
db:
image: postgres:14
container_name: dispatcharr_db
ports:
- "5436:5432"
environment:
- POSTGRES_DB=dispatcharr
- POSTGRES_USER=dispatch

120
docker/entrypoint.sh Executable file
View file

@ -0,0 +1,120 @@
#!/bin/sh
set -e # Exit immediately if a command exits with a non-zero status
# Function to echo with timestamp
echo_with_timestamp() {
echo "$(date '+%Y-%m-%d %H:%M:%S') - $1"
}
# Set PostgreSQL environment variables
export POSTGRES_DB=${POSTGRES_DB:-dispatcharr}
export POSTGRES_USER=${POSTGRES_USER:-dispatch}
export POSTGRES_PASSWORD=${POSTGRES_PASSWORD:-secret}
export POSTGRES_HOST=${POSTGRES_HOST:-localhost}
export POSTGRES_PORT=${POSTGRES_PORT:-5432}
export PGDATA=${PGDATA:-/app/data/db}
export PG_BINDIR="/usr/lib/postgresql/14/bin"
# Echo environment variables for debugging
echo_with_timestamp "POSTGRES_DB: $POSTGRES_DB"
echo_with_timestamp "POSTGRES_USER: $POSTGRES_USER"
echo_with_timestamp "POSTGRES_PASSWORD: $POSTGRES_PASSWORD"
echo_with_timestamp "POSTGRES_HOST: $POSTGRES_HOST"
echo_with_timestamp "POSTGRES_PORT: $POSTGRES_PORT"
# Create group if it doesn't exist
if ! getent group "$PGID" >/dev/null 2>&1; then
groupadd -g "$PGID" mygroup
fi
# Create user if it doesn't exist
if ! getent passwd $PUID > /dev/null 2>&1; then
useradd -u $PUID -g $PGID -m $POSTGRES_USER
else
existing_user=$(getent passwd $PUID | cut -d: -f1)
if [ "$existing_user" != "$POSTGRES_USER" ]; then
usermod -l $POSTGRES_USER -g $PGID "$existing_user"
fi
fi
# Initialize PostgreSQL database
if [ -z "$(ls -A "$PGDATA")" ]; then
echo_with_timestamp "Initializing PostgreSQL database..."
mkdir -p "$PGDATA"
chown -R postgres:postgres "$PGDATA"
chmod 700 "$PGDATA"
# Initialize PostgreSQL
su - postgres -c "$PG_BINDIR/initdb -D $PGDATA"
# Configure PostgreSQL
echo "host all all 0.0.0.0/0 md5" >> "$PGDATA/pg_hba.conf"
echo "listen_addresses='*'" >> "$PGDATA/postgresql.conf"
fi
# Start PostgreSQL
su - postgres -c "$PG_BINDIR/pg_ctl -D $PGDATA start -w -t 300 -o '-c port=${POSTGRES_PORT}'"
# Wait for PostgreSQL to be ready
until su - postgres -c "$PG_BINDIR/pg_isready -h ${POSTGRES_HOST} -p ${POSTGRES_PORT}" >/dev/null 2>&1; do
echo_with_timestamp "Waiting for PostgreSQL to be ready..."
sleep 1
done
# Setup database if needed
if ! su - postgres -c "psql -p ${POSTGRES_PORT} -tAc \"SELECT 1 FROM pg_database WHERE datname = '$POSTGRES_DB';\"" | grep -q 1; then
# Create PostgreSQL database
echo_with_timestamp "Creating PostgreSQL database..."
su - postgres -c "createdb -p ${POSTGRES_PORT} ${POSTGRES_DB}"
# Create user, set ownership, and grant privileges
echo_with_timestamp "Creating PostgreSQL user..."
su - postgres -c "psql -p ${POSTGRES_PORT} -d ${POSTGRES_DB}" <<EOF
DO \$\$
BEGIN
IF NOT EXISTS (SELECT FROM pg_roles WHERE rolname = '$POSTGRES_USER') THEN
CREATE ROLE $POSTGRES_USER WITH LOGIN PASSWORD '$POSTGRES_PASSWORD';
END IF;
END
\$\$;
EOF
echo_with_timestamp "Setting PostgreSQL user privileges..."
su postgres -c "$PG_BINDIR/psql -p ${POSTGRES_PORT} -c \"ALTER DATABASE ${POSTGRES_DB} OWNER TO $POSTGRES_USER;\""
su postgres -c "$PG_BINDIR/psql -p ${POSTGRES_PORT} -c \"GRANT ALL PRIVILEGES ON DATABASE ${POSTGRES_DB} TO $POSTGRES_USER;\""
# Finished setting up PosgresSQL database
echo_with_timestamp "PostgreSQL database setup complete."
fi
# Test PostgreSQL connection and exit if unavailable
echo_with_timestamp "Testing database connection..."
if ! pg_isready -h $POSTGRES_HOST -p $POSTGRES_PORT -U $POSTGRES_USER -d $POSTGRES_DB; then
echo_with_timestamp "ERROR: PostgreSQL is not ready. Exiting..."
exit 1
else
echo_with_timestamp "PostgreSQL is ready to accept connections."
fi
# Verify database accessibility
echo_with_timestamp "Verifying database accessibility..."
if ! su - $POSTGRES_USER -c "psql -p ${POSTGRES_PORT} -d ${POSTGRES_DB} -c 'SELECT 1;'" >/dev/null 2>&1; then
echo_with_timestamp "ERROR: PostgreSQL is running but the database is not accessible. Exiting..."
exit 1
else
echo_with_timestamp "PostgreSQL database is accessible."
fi
# Start Redis
echo_with_timestamp "Starting Redis..."
service redis-server start
# Run Django commands
echo_with_timestamp "Running Django commands..."
python manage.py collectstatic --noinput || true
python manage.py migrate --noinput || true
# Start Celery
echo_with_timestamp "Starting Celery..."
celery -A dispatcharr worker --loglevel=info &
# Start Gunicorn
echo_with_timestamp "Starting Gunicorn..."
gunicorn --workers=4 --worker-class=gevent --timeout=300 --bind 0.0.0.0:9191 dispatcharr.wsgi:application