From dc76405ba9a83e1ae6f3ca6c2ceb6c31d2e1f1ec Mon Sep 17 00:00:00 2001 From: Lewis Dale Date: Fri, 15 Nov 2024 11:24:21 +0000 Subject: [PATCH] Add docker compose config with Postgres --- .dockerignore | 34 ++++++++++++ .gitignore | 2 + .idea/.gitignore | 8 --- .idea/baleen.iml | 94 ------------------------------- .idea/modules.xml | 8 --- .idea/php.xml | 113 -------------------------------------- .idea/symfony2.xml | 6 -- .idea/vcs.xml | 6 -- bin/doctrine | 2 +- compose.yml | 33 +++++++++++ docker/nginx/default.conf | 36 ++++++++++++ docker/php/Dockerfile | 24 ++++++++ src/app.php | 2 +- src/dependencies.php | 6 +- 14 files changed, 135 insertions(+), 239 deletions(-) create mode 100644 .dockerignore delete mode 100644 .idea/.gitignore delete mode 100644 .idea/baleen.iml delete mode 100644 .idea/modules.xml delete mode 100644 .idea/php.xml delete mode 100644 .idea/symfony2.xml delete mode 100644 .idea/vcs.xml create mode 100644 compose.yml create mode 100644 docker/nginx/default.conf create mode 100644 docker/php/Dockerfile diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..2ca7d33 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,34 @@ +# Include any files or directories that you don't want to be copied to your +# container here (e.g., local build artifacts, temporary files, etc.). +# +# For more help, visit the .dockerignore file reference guide at +# https://docs.docker.com/go/build-context-dockerignore/ + +**/.classpath +**/.dockerignore +**/.env +**/.git +**/.gitignore +**/.project +**/.settings +**/.toolstarget +**/.vs +**/.vscode +**/.next +**/.cache +**/*.*proj.user +**/*.dbmdl +**/*.jfm +**/charts +**/docker-compose* +**/compose.y*ml +!**/composer.json +!**/composer.lock +**/Dockerfile* +**/node_modules +**/npm-debug.log +**/obj +**/secrets.dev.yaml +**/values.dev.yaml +LICENSE +README.md diff --git a/.gitignore b/.gitignore index 887435d..dfd3bd2 100644 --- a/.gitignore +++ b/.gitignore @@ -56,3 +56,5 @@ .env /*.db + +.idea \ No newline at end of file diff --git a/.idea/.gitignore b/.idea/.gitignore deleted file mode 100644 index 13566b8..0000000 --- a/.idea/.gitignore +++ /dev/null @@ -1,8 +0,0 @@ -# Default ignored files -/shelf/ -/workspace.xml -# Editor-based HTTP Client requests -/httpRequests/ -# Datasource local storage ignored files -/dataSources/ -/dataSources.local.xml diff --git a/.idea/baleen.iml b/.idea/baleen.iml deleted file mode 100644 index c7797a8..0000000 --- a/.idea/baleen.iml +++ /dev/null @@ -1,94 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml deleted file mode 100644 index 7f796dd..0000000 --- a/.idea/modules.xml +++ /dev/null @@ -1,8 +0,0 @@ - - - - - - - - diff --git a/.idea/php.xml b/.idea/php.xml deleted file mode 100644 index f675471..0000000 --- a/.idea/php.xml +++ /dev/null @@ -1,113 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/.idea/symfony2.xml b/.idea/symfony2.xml deleted file mode 100644 index bd98e40..0000000 --- a/.idea/symfony2.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - - \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml deleted file mode 100644 index 35eb1dd..0000000 --- a/.idea/vcs.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - - - \ No newline at end of file diff --git a/bin/doctrine b/bin/doctrine index 600436b..b9ea0d1 100644 --- a/bin/doctrine +++ b/bin/doctrine @@ -8,7 +8,7 @@ use Lewisdale\App\Models\Repositories\FeedRepository; use Lewisdale\App\Tools\Console\TestFeed; $dotenv = Dotenv\Dotenv::createImmutable([__DIR__, __DIR__ . "/.."]); -$dotenv->load(); +$dotenv->safeLoad(); require_once __DIR__ . '/../src/dependencies.php'; diff --git a/compose.yml b/compose.yml new file mode 100644 index 0000000..89f145d --- /dev/null +++ b/compose.yml @@ -0,0 +1,33 @@ +name: baleen + +services: + nginx: + image: nginx:1.27-alpine + ports: + - "8080:80" + volumes: + - ./:/var/www/html + - ./docker/nginx/default.conf:/etc/nginx/conf.d/default.conf + depends_on: + - php + postgres: + image: postgres:17-alpine + expose: + - 5432 + environment: + POSTGRES_USER: postgres + POSTGRES_PASSWORD: password + POSTGRES_DB: baleen + php: + build: + context: . + dockerfile: docker/php/Dockerfile + expose: + - 9000 + volumes: + - ./src:/var/www/html/src + - ./public:/var/www/html/public + - ./views:/var/www/html/views + - ./bin:/var/www/html/bin + environment: + DB_CONNECTION: postgres://postgres:password@postgres:5432/baleen \ No newline at end of file diff --git a/docker/nginx/default.conf b/docker/nginx/default.conf new file mode 100644 index 0000000..6dfb078 --- /dev/null +++ b/docker/nginx/default.conf @@ -0,0 +1,36 @@ +server { + + listen 80 default_server; + root /var/www/html/public; + index index.html index.php; + + 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; } + + access_log off; + error_log /var/log/nginx/error.log error; + + sendfile off; + + client_max_body_size 100m; + + location ~ .php$ { + fastcgi_split_path_info ^(.+.php)(/.+)$; + fastcgi_pass php:9000; + fastcgi_index index.php; + include fastcgi_params; + fastcgi_read_timeout 300; + fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; + fastcgi_intercept_errors off; + fastcgi_buffer_size 16k; + fastcgi_buffers 4 16k; + } + + location ~ /.ht { + deny all; + } +} \ No newline at end of file diff --git a/docker/php/Dockerfile b/docker/php/Dockerfile new file mode 100644 index 0000000..a2bf70a --- /dev/null +++ b/docker/php/Dockerfile @@ -0,0 +1,24 @@ +FROM composer:lts AS build + +WORKDIR /var/www/html + +COPY composer.json . +COPY composer.lock . + +RUN --mount=type=bind,source=composer.json,target=composer.json \ + --mount=type=bind,source=composer.lock,target=composer.lock \ + --mount=type=cache,target=/tmp/cache \ + composer install --no-dev --no-interaction + +FROM php:8.3-fpm AS run + +ARG DB_CONNECTION + +WORKDIR /var/www/html +RUN apt-get update && apt-get install -y \ + libpq-dev + + +RUN docker-php-ext-install pdo_pgsql + +COPY --from=build /var/www/html/vendor /var/www/html/vendor \ No newline at end of file diff --git a/src/app.php b/src/app.php index 15d8874..4fc8ea6 100644 --- a/src/app.php +++ b/src/app.php @@ -12,7 +12,7 @@ global $container; global $app; $dotenv = Dotenv\Dotenv::createImmutable([__DIR__, __DIR__ . "/.."]); -$dotenv->load(); +$dotenv->safeLoad(); require_once __DIR__ . "/session.php"; diff --git a/src/dependencies.php b/src/dependencies.php index 6386c09..af4d806 100644 --- a/src/dependencies.php +++ b/src/dependencies.php @@ -49,12 +49,14 @@ $container->set(EntityManager::class, static function() { isDevMode: true, ); - $dsnParser = new DsnParser(); + $dsnParser = new DsnParser(['postgres' => 'pdo_pgsql']); $params = $dsnParser->parse($_ENV["DB_CONNECTION"]); $connection = DriverManager::getConnection($params, $config); - $connection->executeQuery("PRAGMA foreign_keys = ON"); + if ($params['driver'] === 'sqlite3' || $params['driver'] === 'pdo_sqlite') { + $connection->executeQuery("PRAGMA foreign_keys = ON"); + } return new EntityManager($connection, $config); });