From fa964cf7c5ebadb23f47133ff64620ceb1ec4e9b Mon Sep 17 00:00:00 2001
From: Dominik Hebeler <dominik@suma-ev.de>
Date: Fri, 31 Jan 2020 14:33:49 +0100
Subject: [PATCH] db connection is now working for docker-compose

---
 .env.example                    |  7 +++++
 Dockerfile                      |  1 -
 app/Console/Commands/WaitDB.php | 55 +++++++++++++++++++++++++++++++++
 docker-compose.yml              | 16 ++++++++--
 init.sh                         | 13 ++++++--
 5 files changed, 87 insertions(+), 5 deletions(-)
 create mode 100644 app/Console/Commands/WaitDB.php

diff --git a/.env.example b/.env.example
index feb2e1d67..a657413fb 100644
--- a/.env.example
+++ b/.env.example
@@ -4,6 +4,13 @@ APP_LOG_LEVEL=debug
 APP_KEY=
 APP_URL=http://localhost
 
+DB_CONNECTION=mysql
+DB_HOST=mgdb
+DB_PORT=3306
+DB_DATABASE=metager
+DB_USERNAME=metager
+DB_PASSWORD="metager"
+
 REDIS_RESULT_CONNECTION=default
 REDIS_RESULT_CACHE_DURATION=60
 
diff --git a/Dockerfile b/Dockerfile
index 73ac6bec7..4604cc1b2 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -34,7 +34,6 @@ EXPOSE 80
 COPY config/nginx.conf /etc/nginx/nginx.conf
 COPY config/nginx-default.conf /etc/nginx/conf.d/default.conf
 COPY --chown=root:nginx . /html
-RUN chmod -R g+w storage bootstrap/cache
 
 CMD chown -R root:nginx storage/logs/metager bootstrap/cache && \
     chmod -R g+w storage/logs/metager bootstrap/cache && \
diff --git a/app/Console/Commands/WaitDB.php b/app/Console/Commands/WaitDB.php
new file mode 100644
index 000000000..7d1d59fdc
--- /dev/null
+++ b/app/Console/Commands/WaitDB.php
@@ -0,0 +1,55 @@
+<?php
+
+namespace App\Console\Commands;
+
+use DB;
+use Illuminate\Console\Command;
+
+class WaitDB extends Command
+{
+    /**
+     * The name and signature of the console command.
+     *
+     * @var string
+     */
+    protected $signature = 'wait:db';
+
+    /**
+     * The console command description.
+     *
+     * @var string
+     */
+    protected $description = 'Waits until default DB connection is available. ';
+
+    /**
+     * Create a new command instance.
+     *
+     * @return void
+     */
+    public function __construct()
+    {
+        parent::__construct();
+    }
+
+    /**
+     * Execute the console command.
+     *
+     * @return mixed
+     */
+    public function handle()
+    {
+        $starttime = microtime(true);
+
+        while (microtime(true) - $starttime < 60) {
+            try {
+                $connection = DB::connection('mysql')->getPdo();
+                $this->line("Connection to database successfull");
+                return 0;
+            } catch (\Exception $e) {
+                $this->error($e->getMessage());
+                sleep(1);
+            }
+        }
+        return 1;
+    }
+}
diff --git a/docker-compose.yml b/docker-compose.yml
index cf67b2773..12a424fca 100644
--- a/docker-compose.yml
+++ b/docker-compose.yml
@@ -12,17 +12,29 @@ services:
     working_dir: /usr/src/app
     command: bash -c "npm install && npm run watch"
   dependencies:
-    image: alpine:latest
+    depends_on:
+      - "mgdb"
+    image: php:7.3-cli
     volumes:
       - .:/data
     working_dir: /data
     command: /data/init.sh
+  mgdb:
+    restart: unless-stopped
+    image: mariadb:latest
+    command: --default-authentication-plugin=mysql_native_password
+    environment:
+      - MYSQL_RANDOM_ROOT_PASSWORD=yes
+      - MYSQL_USER=metager
+      - MYSQL_PASSWORD=metager
+      - MYSQL_DATABASE=metager
   web:
     depends_on:
+      - "mgdb"
       - "dependencies"
       - "phpdeps"
       - "assets"
-    restart: on-failure
+    restart: unless-stopped
     build: .
     working_dir: /html
     volumes:
diff --git a/init.sh b/init.sh
index 905683b98..d1b105df9 100755
--- a/init.sh
+++ b/init.sh
@@ -1,5 +1,9 @@
 #!/bin/sh
 
+# This commands will help initialize data for docker-compose setup
+# Its supposed to run in a php docker image
+docker-php-ext-install pdo pdo_mysql
+
 if [ ! -f "/data/.env" ]; then
     cp /data/.env.example /data/.env
 fi
@@ -7,6 +11,11 @@ fi
 if [ -f "/data/database/useragents.sqlite" ]; then
     rm /data/database/useragents.sqlite
 fi
-cp /data/database/useragents.sqlite.example /data/database/useragents.sqlite
 
-chmod -R go+w storage bootstrap/cache
\ No newline at end of file
+touch /data/database/useragents.sqlite
+
+chmod -R go+w storage bootstrap/cache
+
+php artisan wait:db
+php artisan migrate
+php artisan db:seed
\ No newline at end of file
-- 
GitLab