Docker: Install custom app from occ when nextcloud finished to be installed - Dockerfile

Created on 26 Jul 2019  路  4Comments  路  Source: nextcloud/docker

Hello,

I created a Dockerfile from Nextcloud 16 and I would like to add some custom apps when the installation is finished. So, I decided to create a custom entrypoint where I launched the basic entrypoint "/entrypoint.sh apache2-foreground".

In order to install custom apps, I need to execute occ file with this command :
/var/www/html/occ app:install user_saml

But I need that Nextcloud has finished to be install. And I don't know how to deal with.
If anyone has any suggestions, I am grateful for that.

Best regards.

Most helpful comment

@rere-rere even though your small hack works I thing it would be cleaner to install any custom apps right inside the entrypoint.sh script. This way there is no need to start the entrypoint script in the background and use pgrep to wait until apache is started in the background ...

Here is my (not perfect) solution on installing custom nextcloud apps, using a docker environment variable. I tested it with the alpine image variant of nextcloud - but it should also work with the normal version I guess.

And any feedback is very welcome! This is more or less just a proof of concept and not very good shell coding style ;)


In your docker-compose.yml in the app section specify custom apps separated via spaces in the CUSTOM_APPS environment variable and use a custom nextcloud image:

  app:
    build: ./app
    # image: nextcloud:fpm-alpine
    ...
    environment:
      - CUSTOM_APPS=maps spreed onlyoffice
    ...

Create folder ./app with two files:

  • ./app/Dockerfile

    FROM nextcloud:fpm-alpine
    COPY ./entrypoint.sh /entrypoint.sh
    
    ENTRYPOINT ["/entrypoint.sh"]
    CMD ["php-fpm"]
    
  • ./app/entrypoint.sh

    # above: insert normal entrypoint.sh except it's very last line
    
    function install_custom_apps(){
      # https://stackoverflow.com/a/39568613/6334421
      # https://docs.nextcloud.com/server/19/admin_manual/configuration_server/>
    
      set +u
      set -- $CUSTOM_APPS  # don't quote variable here
      while [ -n "$1" ]; do
        run_as "php /var/www/html/occ app:install $1" || echo "<< WARNING >>"
        run_as "php /var/www/html/occ app:enable $1" || echo "<< WARNING >>"
        shift
      done
      set -u
    }
    
    
    file_env CUSTOM_APPS
    install_custom_apps
    
    # below insert very last line of etrypoint.sh
    exec "$@"
    

This way the normal entrypoint.sh script will be executed just before it starts nextcloud. Then all custom apps will be installed and enabled (or just enabled if they're already installed). Finally nextcloud is started.

All 4 comments

The solution I used in my custom entrypoint is to execute the nextcloud entrypoint. Then, I do a while until apach2 is run. Finally, I can run some configuration commands with occ.

# Start the entrypoint from docker and get his PID
/entrypoint.sh apache2-foreground & pid=$!

# Wait until the installation of nextcloud is done.
while ! pgrep -x "apache2" > /dev/null ; do
  :
done

It's maybe not clean, but it does the work.

Pretty cool trick :)

The custom entrypoint script must keep the container running though, one may end the script with sleep infinity.

@rere-rere even though your small hack works I thing it would be cleaner to install any custom apps right inside the entrypoint.sh script. This way there is no need to start the entrypoint script in the background and use pgrep to wait until apache is started in the background ...

Here is my (not perfect) solution on installing custom nextcloud apps, using a docker environment variable. I tested it with the alpine image variant of nextcloud - but it should also work with the normal version I guess.

And any feedback is very welcome! This is more or less just a proof of concept and not very good shell coding style ;)


In your docker-compose.yml in the app section specify custom apps separated via spaces in the CUSTOM_APPS environment variable and use a custom nextcloud image:

  app:
    build: ./app
    # image: nextcloud:fpm-alpine
    ...
    environment:
      - CUSTOM_APPS=maps spreed onlyoffice
    ...

Create folder ./app with two files:

  • ./app/Dockerfile

    FROM nextcloud:fpm-alpine
    COPY ./entrypoint.sh /entrypoint.sh
    
    ENTRYPOINT ["/entrypoint.sh"]
    CMD ["php-fpm"]
    
  • ./app/entrypoint.sh

    # above: insert normal entrypoint.sh except it's very last line
    
    function install_custom_apps(){
      # https://stackoverflow.com/a/39568613/6334421
      # https://docs.nextcloud.com/server/19/admin_manual/configuration_server/>
    
      set +u
      set -- $CUSTOM_APPS  # don't quote variable here
      while [ -n "$1" ]; do
        run_as "php /var/www/html/occ app:install $1" || echo "<< WARNING >>"
        run_as "php /var/www/html/occ app:enable $1" || echo "<< WARNING >>"
        shift
      done
      set -u
    }
    
    
    file_env CUSTOM_APPS
    install_custom_apps
    
    # below insert very last line of etrypoint.sh
    exec "$@"
    

This way the normal entrypoint.sh script will be executed just before it starts nextcloud. Then all custom apps will be installed and enabled (or just enabled if they're already installed). Finally nextcloud is started.

Thanks for this.

To install multiple apps, I had to comment out the call to file_env CUSTOM_APPS. Otherwise, for me, only the first app in the list is installed. In my case I'm using plain environment variables, not docker secrets.

Thanks.

Was this page helpful?
0 / 5 - 0 ratings