mondediefr / docker-flarum

:speech_balloon: :whale: Docker image of Flarum
https://hub.docker.com/r/mondedie/flarum
MIT License
499 stars 129 forks source link

Extension get disabled if container is recreated #81

Open VincentDugard opened 3 years ago

VincentDugard commented 3 years ago

Hello, If the container is recreated (for example to toggle debug mode), all extension I added are disabled after restart. Any idea ?

Magicalex commented 3 years ago

Ok, same problem

xxxxxliil commented 3 years ago

Ok, same problem

I've come up with a solution that may completely refactor https://github.com/mondediefr/docker-flarum/blob/master/rootfs/usr/local/bin/startup#L69-L125 rows

Magicalex commented 3 years ago

The reason of disabled extensions comes from here https://github.com/flarum/core/pull/2629

smecsia commented 3 years ago

For anyone else looking for a workaround, I've crafted a script that re-enables extensions that must be enabled:

        <?php

        use Illuminate\Database\Capsule\Manager;

        require_once 'vendor/autoload.php';

        $config = include 'config.php';

        $flarumDbPassword = $config['database']['password'] ? " -p{$config['database']['password']}" : '';

        $db = new Manager();
        $db->addConnection($config['database'], 'flarum');

        // get supposed-to-be-enabled extensions
        $extList = collect(explode("\n", file_get_contents('extensions/list')))
            ->map(function($ext) {
                  return str_replace('/', '-', $ext);
            })
            ->filter(function ($ext) {
                  return $ext != '';
            });

        // get all currently enabled extensions and merge with supposed-to-be-enabled
        $res = collect(json_decode($db->getConnection('flarum')
            ->table('settings')
            ->where('key', 'extensions_enabled')
            ->first()->value))
            ->merge($extList);

        var_dump($res);

        // save to database
        $db->getConnection('flarum')
            ->table('settings')
            ->where('key', 'extensions_enabled')
            ->update(['value' => $res->values()->toJson(JSON_OBJECT_AS_ARRAY)]);

We run flarum in Kubernetes, and the above script has to be executed after preparation steps in /usr/local/bin/startup, so we mounted it to /flarum/app/enable-extensions.php and hackily modified startup script to run the enable using the following command for the container:

#...
      command:
        - /bin/sh
        - -c
        - |-
          apk add --update mysql-client bash;
          sed -i '/^exec su-exec.*/i php enable-extensions.php; php flarum migrate; php flarum assets:publish' /usr/local/bin/startup

          /usr/local/bin/startup
#...

I guess this could be contributed, but my PHP knowledge is not the best, so if anyone can improve it, feel free to raise a PR.

PipecraftNet commented 2 years ago

I solved this problem by adding volumes for vendor, composer.json and composer.lock. After adding these volumes, the time to recreate the container has been reduced a lot.

 volumes:
       - /mnt/docker/flarum/assets:/flarum/app/public/assets
       - /mnt/docker/flarum/extensions:/flarum/app/extensions
       - /mnt/docker/flarum/storage/logs:/flarum/app/storage/logs
       - /mnt/docker/flarum/nginx:/etc/nginx/flarum
+      - /mnt/docker/flarum/vendor:/flarum/app/vendor
+      - /mnt/docker/flarum/composer.json:/flarum/app/composer.json
+      - /mnt/docker/flarum/composer.lock:/flarum/app/composer.lock
Marty commented 2 years ago

Hi @PipecraftNet how exactly did you do this? Did you first start the container, copied the new three directories/files out of the container and mount them afterwards? I get errors when I mount these before the installation of flarum.

Marty commented 2 years ago

I played around with the possibility to move the files composer.* to a different directory to make this easier. After the installation of flarum, set the COMPOSER env variable and move those files. I moved them to /flarum/app/composer and mounted a volume there.

However, when I mount the vendor volume from the start, flarum can not get a connection to the database. SQLSTATE[HY000] [2002] Connection refused

oceanlvr commented 2 years ago

However, when I mount the vendor volume from the start, flarum can not get a connection to the database.

Same problem, have you solved this?

zzzhangqi commented 2 years ago

I modified dockerfile and startup,Mount the /flarum/app directory.I don't know much about php,It doesn't look very elegant,But it will solve problem.

https://github.com/zzzhangqi/docker-flarum/blob/master/Dockerfile#L60-L61 https://github.com/zzzhangqi/docker-flarum/blob/master/rootfs/usr/local/bin/startup#L3-L11

use my image

version: "3"

services:
  flarum:
    image: registry.cn-hangzhou.aliyuncs.com/zqqq/flarum:1.2.0
    container_name: flarum
    env_file:
      - /mnt/docker/flarum/flarum.env
    volumes:
      - /mnt/flarum:/flarum/app
      - /mnt/flarum/nginx:/etc/nginx/flarum
    ports:
      - 80:8888
    depends_on:
      - mariadb

  mariadb:
    image: mariadb:10.5
    container_name: mariadb
    environment:
      - MYSQL_ROOT_PASSWORD=xxxxxxxxxx
      - MYSQL_DATABASE=flarum
      - MYSQL_USER=flarum
      - MYSQL_PASSWORD=xxxxxxxxxx
    volumes:
      - /mnt/docker/mysql/db:/var/lib/mysql
maikell commented 2 years ago

I solved this problem by adding volumes for vendor, composer.json and composer.lock. After adding these volumes, the time to recreate the container has been reduced a lot.

 volumes:
       - /mnt/docker/flarum/assets:/flarum/app/public/assets
       - /mnt/docker/flarum/extensions:/flarum/app/extensions
       - /mnt/docker/flarum/storage/logs:/flarum/app/storage/logs
       - /mnt/docker/flarum/nginx:/etc/nginx/flarum
+      - /mnt/docker/flarum/vendor:/flarum/app/vendor
+      - /mnt/docker/flarum/composer.json:/flarum/app/composer.json
+      - /mnt/docker/flarum/composer.lock:/flarum/app/composer.lock

First time you'll need to go through the installation procedure without mounting

  - /mnt/docker/flarum/vendor:/flarum/app/vendor
  - /mnt/docker/flarum/composer.json:/flarum/app/composer.json
  - /mnt/docker/flarum/composer.lock:/flarum/app/composer.lock

Then copy the content to your host, define the paths in your composer file and restart the container.

LoneDev6 commented 1 year ago

This is my working solution: https://github.com/LoneDev6/docker-flarum/commit/9d753d6e5a347e3f353af24d67cc9f639b92b374

cloudrack-ca commented 1 year ago

For anyone else looking for a workaround, I've crafted a script that re-enables extensions that must be enabled:

        <?php

        use Illuminate\Database\Capsule\Manager;

        require_once 'vendor/autoload.php';

        $config = include 'config.php';

        $flarumDbPassword = $config['database']['password'] ? " -p{$config['database']['password']}" : '';

        $db = new Manager();
        $db->addConnection($config['database'], 'flarum');

        // get supposed-to-be-enabled extensions
        $extList = collect(explode("\n", file_get_contents('extensions/list')))
          ->map(function($ext) {
                  return str_replace('/', '-', $ext);
          })
          ->filter(function ($ext) {
                  return $ext != '';
          });

        // get all currently enabled extensions and merge with supposed-to-be-enabled
        $res = collect(json_decode($db->getConnection('flarum')
            ->table('settings')
            ->where('key', 'extensions_enabled')
            ->first()->value))
            ->merge($extList);

        var_dump($res);

        // save to database
        $db->getConnection('flarum')
            ->table('settings')
            ->where('key', 'extensions_enabled')
            ->update(['value' => $res->values()->toJson(JSON_OBJECT_AS_ARRAY)]);

We run flarum in Kubernetes, and the above script has to be executed after preparation steps in /usr/local/bin/startup, so we mounted it to /flarum/app/enable-extensions.php and hackily modified startup script to run the enable using the following command for the container:

#...
      command:
        - /bin/sh
        - -c
        - |-
          apk add --update mysql-client bash;
          sed -i '/^exec su-exec.*/i php enable-extensions.php; php flarum migrate; php flarum assets:publish' /usr/local/bin/startup

          /usr/local/bin/startup
#...

I guess this could be contributed, but my PHP knowledge is not the best, so if anyone can improve it, feel free to raise a PR.

i do have copilot access - might be able to help I'm hoping most of these issues will just get fixed on setup - tbh tho could just make a free flarum on https://flarum.cloud i.e https://freeflarum.com/ as well.