From 88329c12065356cdb91b6526527d9f13d1f8f4a1 Mon Sep 17 00:00:00 2001 From: "m.marjorie" Date: Mon, 24 Mar 2025 14:30:06 +0800 Subject: [PATCH 1/2] Initial commit with Docker setup for Laravel, MySQL, and Nginx --- Dockerfile | 41 ++++++++++++++++------------- docker-compose.yml | 64 +++++++++++++++++++++------------------------- 2 files changed, 52 insertions(+), 53 deletions(-) diff --git a/Dockerfile b/Dockerfile index 9b1a9e6..7aebc4a 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,26 +1,31 @@ FROM php:8.2-fpm +# Install dependencies +RUN apt-get update && apt-get install -y \ + libpng-dev \ + libjpeg-dev \ + libfreetype6-dev \ + zip \ + unzip \ + && docker-php-ext-configure gd --with-freetype --with-jpeg \ + && docker-php-ext-install gd pdo pdo_mysql + +# Install Composer +RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer + # Set working directory WORKDIR /var/www/html -# Install dependencies -RUN apt-get update && apt-get install -y \ - git \ - curl \ - libpng-dev \ - libonig-dev \ - libxml2-dev \ - zip \ - unzip +# Copy the application code +COPY . . -# Clear cache -RUN apt-get clean && rm -rf /var/lib/apt/lists/* - -# Install PHP extensions -RUN docker-php-ext-install pdo_mysql mbstring exif pcntl bcmath gd - -# Install Composer -COPY --from=composer:latest /usr/bin/composer /usr/bin/composer +# Install Laravel dependencies +RUN composer install --no-dev --optimize-autoloader # Set permissions -RUN chown -R www-data:www-data /var/www/html +RUN chown -R www-data:www-data /var/www/html/storage /var/www/html/bootstrap/cache + +# Expose port +EXPOSE 9000 + +CMD ["php-fpm"] \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml index c04a6e9..619225f 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,70 +1,64 @@ version: '3.8' services: - # MySQL Database - db: + mysql: image: mysql:8.0 - container_name: laravel-mysql - restart: unless-stopped + container_name: mysql environment: + MYSQL_ROOT_PASSWORD: root MYSQL_DATABASE: laravel - MYSQL_ROOT_PASSWORD: root_password - MYSQL_USER: laravel_user - MYSQL_PASSWORD: laravel_password + MYSQL_USER: laravel + MYSQL_PASSWORD: secret volumes: - - mysql_data:/var/lib/mysql - ports: - - "3306:3306" + - mysql-data:/var/lib/mysql networks: - - laravel-network + - app-network healthcheck: test: ["CMD", "mysqladmin", "ping", "-h", "localhost"] interval: 10s timeout: 5s retries: 5 - start_period: 30s + restart: unless-stopped - # Laravel Application - app: + laravel: build: context: . dockerfile: Dockerfile - container_name: laravel-app - restart: unless-stopped - volumes: - - ./src:/var/www/html + container_name: laravel depends_on: - db: + mysql: condition: service_healthy + volumes: + - .:/var/www/html networks: - - laravel-network + - app-network healthcheck: - test: ["CMD", "php", "-v"] + test: ["CMD", "php", "artisan", "config:cache"] interval: 10s timeout: 5s retries: 5 command: > - sh -c "php-fpm" - - # Nginx Web Server - web: - image: nginx:alpine - container_name: laravel-nginx + sh -c "php artisan migrate --force && php-fpm" restart: unless-stopped + + nginx: + image: nginx:alpine + container_name: nginx + depends_on: + laravel: + condition: service_healthy ports: - "80:80" volumes: - - ./src:/var/www/html - - ./nginx/default.conf:/etc/nginx/conf.d/default.conf - depends_on: - app: - condition: service_healthy + - ./nginx.conf:/etc/nginx/conf.d/default.conf + - .:/var/www/html networks: - - laravel-network + - app-network + restart: unless-stopped networks: - laravel-network: + app-network: driver: bridge volumes: - mysql_data: + mysql-data: \ No newline at end of file From 5ed6e1d34b49133668835e3dfdd290929af11ae2 Mon Sep 17 00:00:00 2001 From: "m.marjorie" Date: Mon, 24 Mar 2025 15:30:47 +0800 Subject: [PATCH 2/2] Fix nginx.conf to be a file and update project configuration --- .env.example | 12 ++++++------ docker-compose.yml | 4 ++-- nginx.conf | 22 ++++++++++++++++++++++ 3 files changed, 30 insertions(+), 8 deletions(-) create mode 100644 nginx.conf diff --git a/.env.example b/.env.example index 35db1dd..e4808a7 100755 --- a/.env.example +++ b/.env.example @@ -20,12 +20,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=laravel -# DB_USERNAME=root -# DB_PASSWORD= +DB_CONNECTION=mysql +DB_HOST=mysql +DB_PORT=3306 +DB_DATABASE=laravel +DB_USERNAME=laravel +DB_PASSWORD=secret SESSION_DRIVER=database SESSION_LIFETIME=120 diff --git a/docker-compose.yml b/docker-compose.yml index 619225f..3bd8d49 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -33,12 +33,12 @@ services: networks: - app-network healthcheck: - test: ["CMD", "php", "artisan", "config:cache"] + test: ["CMD", "php", "--version"] interval: 10s timeout: 5s retries: 5 command: > - sh -c "php artisan migrate --force && php-fpm" + sh -c "sleep 10 && php artisan migrate --force && php-fpm" restart: unless-stopped nginx: diff --git a/nginx.conf b/nginx.conf new file mode 100644 index 0000000..e1fec47 --- /dev/null +++ b/nginx.conf @@ -0,0 +1,22 @@ +server { + listen 80; + server_name localhost; + + root /var/www/html/public; + index index.php index.html; + + location / { + try_files $uri $uri/ /index.php?$query_string; + } + + location ~ \.php$ { + fastcgi_pass laravel:9000; + fastcgi_index index.php; + fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; + include fastcgi_params; + } + + location ~ /\.ht { + deny all; + } +}