From 6278417423572ce51b2186d0442e604766cf226b Mon Sep 17 00:00:00 2001 From: Mohit Panjwani Date: Wed, 27 May 2020 14:18:29 +0530 Subject: [PATCH] refactor docker setup --- .gitignore | 2 - Dockerfile | 73 ++++++++++++++------------------- docker-compose.yaml.example | 40 ------------------ docker-compose.yml | 51 +++++++++++++++++++++++ docker-compose/nginx/nginx.conf | 20 +++++++++ docker-compose/setup.sh | 7 ++++ nginx.conf | 53 ------------------------ 7 files changed, 108 insertions(+), 138 deletions(-) delete mode 100644 docker-compose.yaml.example create mode 100644 docker-compose.yml create mode 100644 docker-compose/nginx/nginx.conf create mode 100755 docker-compose/setup.sh delete mode 100644 nginx.conf diff --git a/.gitignore b/.gitignore index 5f355185..0afb940b 100644 --- a/.gitignore +++ b/.gitignore @@ -11,5 +11,3 @@ Homestead.yaml .rnd /.expo /.vscode -docker-compose.yml -docker-compose.yaml diff --git a/Dockerfile b/Dockerfile index 28c46c56..ae887edc 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,53 +1,40 @@ -##### STAGE 1 ##### +FROM php:7.4-fpm -FROM composer as composer +# Arguments defined in docker-compose.yml +ARG user +ARG uid -# Copy composer files from project root into composer container's working dir -COPY composer.* /app/ +# Install system dependencies +RUN apt-get update && apt-get install -y \ + git \ + curl \ + libpng-dev \ + libonig-dev \ + libxml2-dev \ + zip \ + unzip \ + libzip-dev -# Copy database directory for autoloader optimization -COPY database /app/database +# Clear cache +RUN apt-get clean && rm -rf /var/lib/apt/lists/* -# Run composer to build dependencies in vendor folder -RUN composer install --no-scripts --no-suggest --no-interaction --prefer-dist --optimize-autoloader +# Install PHP extensions +RUN docker-php-ext-install pdo_mysql mbstring zip exif pcntl bcmath gd -# Copy everything from project root into composer container's working dir -COPY . /app - -RUN composer dump-autoload --optimize --classmap-authoritative +# Get latest Composer +COPY --from=composer:latest /usr/bin/composer /usr/bin/composer -##### STAGE 2 ##### +# Create system user to run Composer and Artisan Commands +RUN useradd -G www-data,root -u $uid -d /home/$user $user +RUN mkdir -p /home/$user/.composer && \ + chown -R $user:$user /home/$user -FROM php:7.3.12-fpm-alpine +# Set working directory +WORKDIR /var/www -# Use the default production configuration -RUN mv "$PHP_INI_DIR/php.ini-production" "$PHP_INI_DIR/php.ini" +USER $user -RUN apk add --no-cache libpng-dev libxml2-dev oniguruma-dev libzip-dev gnu-libiconv && \ - docker-php-ext-install bcmath ctype json gd mbstring pdo pdo_mysql tokenizer xml zip +# COPY ./docker-compose/setup.sh /tmp +# RUN ln -s /usr/local/bin/setup.sh -ENV LD_PRELOAD /usr/lib/preloadable_libiconv.so php - -# Set container's working dir -WORKDIR /app - -# Copy everything from project root into php container's working dir -COPY . /app - -# Copy vendor folder from composer container into php container -COPY --from=composer /app/vendor /app/vendor - -RUN touch database/database.sqlite && \ - cp .env.example .env && \ - php artisan config:cache && \ - php artisan passport:keys && \ - php artisan key:generate && \ - chown -R www-data:www-data . && \ - chmod -R 755 . && \ - chmod -R 775 storage/framework/ && \ - chmod -R 775 storage/logs/ && \ - chmod -R 775 bootstrap/cache/ - -EXPOSE 9000 - -CMD ["php-fpm", "--nodaemonize"] +# ENTRYPOINT ["/tmp/setup.sh"] diff --git a/docker-compose.yaml.example b/docker-compose.yaml.example deleted file mode 100644 index 590e4c2a..00000000 --- a/docker-compose.yaml.example +++ /dev/null @@ -1,40 +0,0 @@ -version: '3.1' - -services: - - web: - image: nginx - depends_on: - - php - ports: - - 8080:80 - volumes: - - ./nginx.conf:/etc/nginx/nginx.conf:ro - - app:/app - restart: always - - php: - build: . - depends_on: - - db - expose: - - 9000 - volumes: - - app:/app - restart: always - - db: - image: mariadb - restart: always - volumes: - - db:/var/lib/mysql - environment: - MYSQL_USER: crater - MYSQL_PASSWORD: crater - MYSQL_DATABASE: crater - MYSQL_ROOT_PASSWORD: crater - -volumes: - app: - db: - diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 00000000..52bc0eb8 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,51 @@ +version: '3.7' + +services: + app: + build: + args: + user: crater-user + uid: 1000 + context: ./ + dockerfile: Dockerfile + image: crater-php + container_name: crater-app + restart: unless-stopped + working_dir: /var/www/ + volumes: + - ./:/var/www + networks: + - crater + + db: + image: mariadb + restart: always + volumes: + - db:/var/lib/mysql + environment: + MYSQL_USER: crater + MYSQL_PASSWORD: crater + MYSQL_DATABASE: crater + MYSQL_ROOT_PASSWORD: crater + ports: + - '33006:3306' + networks: + - crater + + nginx: + image: nginx:1.17-alpine + restart: unless-stopped + ports: + - 8080:80 + volumes: + - ./:/var/www + - ./docker-compose/nginx:/etc/nginx/conf.d/ + networks: + - crater + +volumes: + db: + +networks: + crater: + driver: bridge diff --git a/docker-compose/nginx/nginx.conf b/docker-compose/nginx/nginx.conf new file mode 100644 index 00000000..4c6cbf44 --- /dev/null +++ b/docker-compose/nginx/nginx.conf @@ -0,0 +1,20 @@ +server { + listen 80; + index index.php index.html; + error_log /var/log/nginx/error.log; + access_log /var/log/nginx/access.log; + root /var/www/public; + location ~ \.php$ { + try_files $uri =404; + fastcgi_split_path_info ^(.+\.php)(/.+)$; + fastcgi_pass app:9000; + fastcgi_index index.php; + include fastcgi_params; + fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; + fastcgi_param PATH_INFO $fastcgi_path_info; + } + location / { + try_files $uri $uri/ /index.php?$query_string; + gzip_static on; + } +} \ No newline at end of file diff --git a/docker-compose/setup.sh b/docker-compose/setup.sh new file mode 100755 index 00000000..120b3e5c --- /dev/null +++ b/docker-compose/setup.sh @@ -0,0 +1,7 @@ +#!/bin/sh + +cd /var/www + +php artisan storage:link || true +php artisan key:generate +php artisan passport:keys || true \ No newline at end of file diff --git a/nginx.conf b/nginx.conf deleted file mode 100644 index 36645fa7..00000000 --- a/nginx.conf +++ /dev/null @@ -1,53 +0,0 @@ -worker_processes 8; - -error_log /var/log/nginx/error.log warn; -pid /var/run/nginx.pid; - -events { - worker_connections 4096; -} - -http { - include /etc/nginx/mime.types; - default_type application/octet-stream; - - log_format main '$remote_addr - $remote_user [$time_local] "$request" ' - '$status $body_bytes_sent "$http_referer" ' - '"$http_user_agent" "$http_x_forwarded_for"'; - - access_log /var/log/nginx/access.log main; - - sendfile on; - - keepalive_timeout 65; - - server { - listen 80 default_server; - - root /app/public; - index index.php; - charset utf-8; - - access_log off; - - location / { - try_files $uri $uri/ /index.php?$query_string; - } - - location = /favicon.ico { access_log off; log_not_found off; } - location = /robots.txt { access_log off; log_not_found off; } - - add_header X-Content-Type-Options nosniff; - add_header X-XSS-Protection "1; mode=block"; - add_header X-Robots-Tag none; - add_header Content-Security-Policy "frame-ancestors 'self'"; - - location ~ \.php$ { - fastcgi_pass php:9000; - fastcgi_index index.php; - include fastcgi_params; - fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; - include /etc/nginx/fastcgi_params; - } - } -}