docker-flarum icon indicating copy to clipboard operation
docker-flarum copied to clipboard

Extension get disabled if container is recreated

Open VincentDugard opened this issue 4 years ago • 12 comments

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

VincentDugard avatar May 05 '21 09:05 VincentDugard

Ok, same problem

al3xLvs avatar May 26 '21 22:05 al3xLvs

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

xxxxxliil avatar Jun 03 '21 03:06 xxxxxliil

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

al3xLvs avatar Jun 07 '21 19:06 al3xLvs

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.

smecsia avatar Jun 15 '21 01:06 smecsia

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

PipecraftNet avatar Oct 26 '21 21:10 PipecraftNet

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 avatar Mar 02 '22 09:03 Marty

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

Marty avatar Mar 03 '22 09:03 Marty

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?

oceanlvr avatar Mar 19 '22 12:03 oceanlvr

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

zzzhangqi avatar Apr 12 '22 14:04 zzzhangqi

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.

maikell avatar Apr 17 '22 05:04 maikell

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

LoneDev6 avatar Dec 03 '22 09:12 LoneDev6

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.

cloudrack-ca avatar Sep 23 '23 05:09 cloudrack-ca

I can't get any of the previous solutions to work and thanks to the help that you have put I have been looking at the database and I have seen that flarum saves there the enabled extensions, then what I do is run once the docker compose up a sql that writes in that field all the extensions that I use. The bad thing about this is that it is a bit manual since the .sql always has to be updated, but in my case that I do not usually install new extensions is not a problem for me. As a trick and to facilitate the work when I put a new extension what I do is to go to the database to look at that field and copy and paste in the sql what it says in that field once I have activated the ones I want.

The part of volumes of docker compose is like this:

volumes, the .sql file I have it in a folder called flarum in the same directory as my docker compose.

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
  - ./flarum/update_extensions.sql:/docker-entrypoint-initdb.d/update_extensions.sql

And the sql file is this:

UPDATE settings SET value = '["flarum-flags","flarum-approval","flarum-tags","serakoi-flarumstaffbadge","flarum-suspend","flarum-subscriptions","flarum-sticky","flarum-statistics","flarum-nicknames","flarum-mentions","flarum-markdown","flarum-lock","flarum-likes","flarum-lang-english","flarum-emoji","flarum-bbcode"]' WHERE key = 'extensions_enabled';

j3rgy avatar Apr 28 '24 19:04 j3rgy