From 9b7dfb2bafa008713a18fdde9c79267b2678f429 Mon Sep 17 00:00:00 2001 From: hackerESQ Date: Wed, 4 Sep 2024 17:48:40 -0500 Subject: [PATCH] dockerize wip --- .env.example | 12 ++++----- docker-compose.yml | 48 ++++++++++++++++++++++++++++++++++++ docker/.gitignore | 1 + docker/Dockerfile | 54 +++++++++++++++++++++++++++++++++++++++++ docker/entrypoint.sh | 18 ++++++++++++++ docker/nginx.conf | 25 +++++++++++++++++++ docker/redis.conf | 2 ++ docker/supervisord.conf | 41 +++++++++++++++++++++++++++++++ 8 files changed, 195 insertions(+), 6 deletions(-) create mode 100644 docker-compose.yml create mode 100644 docker/.gitignore create mode 100644 docker/Dockerfile create mode 100755 docker/entrypoint.sh create mode 100644 docker/nginx.conf create mode 100644 docker/redis.conf create mode 100644 docker/supervisord.conf diff --git a/.env.example b/.env.example index 47868df..9bf8311 100644 --- a/.env.example +++ b/.env.example @@ -19,12 +19,12 @@ LOG_STACK=single LOG_DEPRECATIONS_CHANNEL=null LOG_LEVEL=debug -DB_CONNECTION=sqlite -# DB_HOST=127.0.0.1 -# DB_PORT=3306 -# DB_DATABASE=investbrain -# DB_USERNAME=root -# DB_PASSWORD= +DB_CONNECTION=mysql +DB_HOST=investbrain-mysql +DB_PORT=3306 +DB_DATABASE=investbrain +DB_USERNAME=investbrain +DB_PASSWORD=investbrain SESSION_DRIVER=redis SESSION_LIFETIME=120 diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..c6cad66 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,48 @@ +networks: + investbrain_network: + driver: bridge +services: + app: + build: + context: . + dockerfile: docker/Dockerfile + container_name: investbrain-app + restart: unless-stopped + tty: true + expose: + - "9000" + volumes: + - .:/var/www/app:delegated + depends_on: + - mysql + networks: + - investbrain_network + nginx: + image: nginx:alpine + container_name: investbrain-nginx + restart: unless-stopped + tty: true + ports: + - "8000:80" + volumes: + - ./docker/nginx.conf:/etc/nginx/conf.d/default.conf + - .:/var/www/app:delegated + depends_on: + - app + networks: + - investbrain_network + mysql: + image: mysql:8.4 + container_name: investbrain-mysql + restart: unless-stopped + environment: + MYSQL_ROOT_PASSWORD: investbrain + MYSQL_DATABASE: investbrain + MYSQL_USER: investbrain + MYSQL_PASSWORD: investbrain + volumes: + - investbrain-mysql:/var/lib/mysql + networks: + - investbrain_network +volumes: + investbrain-mysql: diff --git a/docker/.gitignore b/docker/.gitignore new file mode 100644 index 0000000..f142970 --- /dev/null +++ b/docker/.gitignore @@ -0,0 +1 @@ +dump.rdb \ No newline at end of file diff --git a/docker/Dockerfile b/docker/Dockerfile new file mode 100644 index 0000000..0a77ec0 --- /dev/null +++ b/docker/Dockerfile @@ -0,0 +1,54 @@ +# use PHP 8.3 +FROM php:8.3-fpm + +ENV DEBIAN_FRONTEND noninteractive + +# Set the working directory +COPY . /var/www/app +WORKDIR /var/www/app + +# Install common php extension dependencies +RUN apt-get update && apt-get install -y \ + libfreetype-dev \ + libjpeg62-turbo-dev \ + libpng-dev \ + zlib1g-dev \ + libzip-dev \ + unzip \ + libicu-dev \ + git \ + curl \ + redis \ + supervisor \ + && docker-php-ext-configure gd --with-freetype --with-jpeg \ + && docker-php-ext-install -j$(nproc) \ + gd \ + zip \ + pdo_mysql \ + mysqli \ + intl + +# Install Node.js and npm +RUN curl -fsSL https://deb.nodesource.com/setup_20.x | bash - \ + && apt-get install -y nodejs \ + && npm install -g npm@latest + +# Copy over supervisor configuration +COPY ./docker/supervisord.conf /etc/supervisor/conf.d/supervisord.conf + +# Update permissions +RUN chown -R www-data:www-data . \ + && chmod -R 775 ./storage \ + && chmod +x ./docker/entrypoint.sh + +# install composer +COPY --from=composer:2.6.5 /usr/bin/composer /usr/local/bin/composer + +# copy composer.json to workdir & install dependencies +RUN composer install + +# Run NPM build +RUN npm install && npm run build + +# Run everything else +CMD ["./docker/entrypoint.sh"] diff --git a/docker/entrypoint.sh b/docker/entrypoint.sh new file mode 100755 index 0000000..76afce5 --- /dev/null +++ b/docker/entrypoint.sh @@ -0,0 +1,18 @@ +#!/bin/bash + +echo "Running entrypoint script..." + +if [ ! -f /var/www/app/.env ]; then + echo "Ope, gotta create an .env file!" + + cp /var/www/app/.env.example /var/www/app/.env +fi + +echo "Waiting a second for the database to become available..." +sleep 2 + +echo "Running migrations..." +/usr/local/bin/php /var/www/app/artisan migrate + +echo "Spinning up Supervisor daemon..." +exec /usr/bin/supervisord -c /etc/supervisor/conf.d/supervisord.conf diff --git a/docker/nginx.conf b/docker/nginx.conf new file mode 100644 index 0000000..cf0f1c5 --- /dev/null +++ b/docker/nginx.conf @@ -0,0 +1,25 @@ +server { + listen 80; + server_name localhost; + + root /var/www/app/public; + index index.php index.html index.htm; + + location / { + try_files $uri $uri/ /index.php?$query_string; + } + + location ~ \.php$ { + include fastcgi_params; + fastcgi_pass investbrain-app:9000; + fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; + fastcgi_param PATH_INFO $fastcgi_path_info; + } + + location ~ /\.ht { + deny all; + } + + error_log /var/log/nginx/error.log; + access_log /var/log/nginx/access.log; +} \ No newline at end of file diff --git a/docker/redis.conf b/docker/redis.conf new file mode 100644 index 0000000..5991105 --- /dev/null +++ b/docker/redis.conf @@ -0,0 +1,2 @@ +# Redis RDB and AOF file location +dir /var/www/app/docker \ No newline at end of file diff --git a/docker/supervisord.conf b/docker/supervisord.conf new file mode 100644 index 0000000..dff5205 --- /dev/null +++ b/docker/supervisord.conf @@ -0,0 +1,41 @@ +[supervisord] +nodaemon=true +user=root +logfile=/var/log/supervisor/supervisord.log +pidfile=/var/run/supervisord.pid + +[program:php] +command=php-fpm -F +autostart=true +autorestart=true +stdout_logfile=/var/log/supervisor/php.log +stderr_logfile=/var/log/supervisor/php_error.log + +[program:redis] +command=redis-server /var/www/app/docker/redis.conf +autostart=true +autorestart=true +stdout_logfile=/var/log/supervisor/redis.log +stderr_logfile=/var/log/supervisor/redis_error.log + +[program:scheduler] +command=php artisan schedule:work +autorestart=true +redirect_stderr=true +stdout_logfile=/dev/stdout +stdout_logfile_maxbytes=0 +stderr_logfile=/dev/stderr +stderr_logfile_maxbytes=0 + +[program:queue-worker] +command=php artisan queue:work --sleep=3 --tries=1 --memory=256 --timeout=3600 +process_name=%(program_name)s_%(process_num)02d +autorestart=true +redirect_stderr=true +stdout_logfile=/dev/stdout +stdout_logfile_maxbytes=0 +stderr_logfile=/dev/stderr +stderr_logfile_maxbytes=0 +numprocs=2 + +[supervisorctl] \ No newline at end of file