From 9bbb7e8a2dc9062c3d84456ec9361bfe681c76e8 Mon Sep 17 00:00:00 2001 From: myrmidex Date: Mon, 30 Jun 2025 20:29:55 +0200 Subject: [PATCH] Dockerize app --- Dockerfile | 53 +++++++++++++++++++++ README.md | 110 +++++++++++++++++++++++++++++++++++++++++++ docker/entrypoint.sh | 31 ++++++++++++ 3 files changed, 194 insertions(+) create mode 100644 Dockerfile create mode 100644 README.md create mode 100644 docker/entrypoint.sh diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..ce5bee4 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,53 @@ +# Multi-stage build for Laravel with React frontend +FROM node:22-alpine AS frontend-builder + +WORKDIR /app +COPY package*.json ./ +RUN npm install --only=production +COPY . . +RUN npm run build + +FROM php:8.4-fpm-alpine + +# Install system dependencies and PHP extensions +RUN apk add --no-cache \ + git \ + curl \ + libpng-dev \ + oniguruma-dev \ + libxml2-dev \ + zip \ + unzip \ + && docker-php-ext-install pdo_mysql mbstring exif pcntl bcmath gd + +# Install Composer +COPY --from=composer:latest /usr/bin/composer /usr/bin/composer + +# Set working directory +WORKDIR /var/www/html + +# Copy composer files +COPY composer*.json ./ + +# Copy application files (needed for artisan in composer scripts) +COPY . . + +# Install dependencies +RUN composer install --no-dev --optimize-autoloader --no-interaction + +# Copy built frontend assets +COPY --from=frontend-builder /app/public/build /var/www/html/public/build + +# Set permissions +RUN chown -R www-data:www-data /var/www/html \ + && chmod -R 755 /var/www/html/storage \ + && chmod -R 755 /var/www/html/bootstrap/cache + +# Create entrypoint script +COPY docker/entrypoint.sh /entrypoint.sh +RUN chmod +x /entrypoint.sh + +EXPOSE 8000 + +ENTRYPOINT ["/entrypoint.sh"] +CMD ["web"] \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..a7ac6f3 --- /dev/null +++ b/README.md @@ -0,0 +1,110 @@ +# Lemmy Poster + +A Laravel application for posting articles to Lemmy platforms. + +## Docker Deployment + +### Building the Image + +```bash +docker build -t your-registry/lemmy-poster:latest . +docker push your-registry/lemmy-poster:latest +``` + +### Docker Compose + +Create a `docker-compose.yml` file: + +```yaml +services: + app-web: + image: your-registry/lemmy-poster:latest + command: ["web"] + ports: + - "8000:8000" + environment: + - APP_ENV=production + - APP_KEY=${APP_KEY} + - DB_CONNECTION=mysql + - DB_HOST=mysql + - DB_PORT=3306 + - DB_DATABASE=lemmy_poster + - DB_USERNAME=lemmy_user + - DB_PASSWORD=${DB_PASSWORD} + - QUEUE_CONNECTION=database + - LEMMY_INSTANCE=${LEMMY_INSTANCE} + - LEMMY_USERNAME=${LEMMY_USERNAME} + - LEMMY_PASSWORD=${LEMMY_PASSWORD} + - LEMMY_COMMUNITY=${LEMMY_COMMUNITY} + depends_on: + - mysql + volumes: + - storage_data:/var/www/html/storage/app + restart: unless-stopped + + app-queue: + image: your-registry/lemmy-poster:latest + command: ["queue"] + environment: + - APP_ENV=production + - APP_KEY=${APP_KEY} + - DB_CONNECTION=mysql + - DB_HOST=mysql + - DB_PORT=3306 + - DB_DATABASE=lemmy_poster + - DB_USERNAME=lemmy_user + - DB_PASSWORD=${DB_PASSWORD} + - QUEUE_CONNECTION=database + - LEMMY_INSTANCE=${LEMMY_INSTANCE} + - LEMMY_USERNAME=${LEMMY_USERNAME} + - LEMMY_PASSWORD=${LEMMY_PASSWORD} + - LEMMY_COMMUNITY=${LEMMY_COMMUNITY} + depends_on: + - mysql + volumes: + - storage_data:/var/www/html/storage/app + restart: unless-stopped + + mysql: + image: mysql:8.0 + environment: + - MYSQL_DATABASE=lemmy_poster + - MYSQL_USER=lemmy_user + - MYSQL_PASSWORD=${DB_PASSWORD} + - MYSQL_ROOT_PASSWORD=${DB_ROOT_PASSWORD} + volumes: + - mysql_data:/var/lib/mysql + restart: unless-stopped + +volumes: + mysql_data: + storage_data: +``` + +### Environment Variables + +Create a `.env` file with: + +```env +APP_KEY=your-app-key-here +DB_PASSWORD=your-db-password +DB_ROOT_PASSWORD=your-root-password +LEMMY_INSTANCE=your-lemmy-instance.com +LEMMY_USERNAME=your-lemmy-username +LEMMY_PASSWORD=your-lemmy-password +LEMMY_COMMUNITY=your-target-community +``` + +Generate the APP_KEY: +```bash +openssl rand -base64 32 | tr -d '=' +``` + +### Deployment + +1. Build and push the image +2. Copy the docker-compose.yml to your server +3. Create the .env file with your environment variables +4. Run: `docker-compose up -d` + +The web interface will be available on port 8000, ready for CloudFlare tunnel configuration. diff --git a/docker/entrypoint.sh b/docker/entrypoint.sh new file mode 100644 index 0000000..f432a4f --- /dev/null +++ b/docker/entrypoint.sh @@ -0,0 +1,31 @@ +#!/bin/sh + +# Exit on any error +set -e + +# Wait for database to be ready +until php artisan tinker --execute="DB::connection()->getPdo();" > /dev/null 2>&1; do + echo "Waiting for database connection..." + sleep 2 +done + +# Run migrations on first start (web container only) +if [ "$1" = "web" ]; then + php artisan migrate --force +fi + +# Execute the command based on the argument +case "$1" in + "web") + echo "Starting web server..." + exec php artisan serve --host=0.0.0.0 --port=8000 + ;; + "queue") + echo "Starting queue worker..." + exec php artisan queue:work --tries=3 + ;; + *) + echo "Usage: $0 {web|queue}" + exit 1 + ;; +esac \ No newline at end of file