Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Install custom app from occ when nextcloud finished to be installed - Dockerfile #820

Closed
RegisGraptin opened this issue Jul 26, 2019 · 9 comments

Comments

@RegisGraptin
Copy link

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.

@RegisGraptin
Copy link
Author

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.

@chikamichi
Copy link

Pretty cool trick :)

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

@langfingaz
Copy link

langfingaz commented Aug 25, 2020

@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.

@bdube
Copy link

bdube commented Sep 15, 2020

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.

@Schroedingers-Cat
Copy link
Contributor

I think it would be great to have a solution directly built into the original docker image.

@sebschlicht
Copy link

sebschlicht commented Apr 10, 2021

I'll just drop this for the next who stumbles across this thread:

You don't need to copy the Nextcloud entrypoint script, which would require quite some maintenance on your part.
You just need a run_as function, to act as the correct user.

The required steps (Nextcloud 20) are:

#!/bin/sh
#
# Install the custom apps listed in NEXTCLOUD_CUSTOM_APPS.
#

run_as() {
    if [ "$(id -u)" = 0 ]; then
        su -p www-data -s /bin/sh -c "$1"
    else
        sh -c "$1"
    fi
}

echo "installing custom apps: $NEXTCLOUD_CUSTOM_APPS"
for app in $NEXTCLOUD_CUSTOM_APPS; do
  php /var/www/html/occ app:install "$app" || echo "Warning: Failed to install custom app '$app'!"
done

You can just mount this script into your container and run it.

Docker-compose:

  1. mount the script into your container, e.g.

     volumes:
       - ./custom-apps.sh:/hooks/install/custom-apps.sh
    
  2. have the entrypoint call this script on startup

     command: ["/hooks/install/custom-apps.sh"]
    
  3. and define which custom apps to install

     env:
       NEXTCLOUD_CUSTOM_APPS: user_saml music_player
       ...
    

However, you may want to extend the script or write a wrapper, to ensure such installation steps only run once (or maybe to run multiple commands easily).

@ThumbGen
Copy link

This is a smart idea:

#!/bin/sh

# Let /entrypoint.sh not run apache
tail -1 /entrypoint.sh | grep exec && sed -i '$ d' /entrypoint.sh

# Launch canonical entrypoint
/entrypoint.sh $@

# Function from the Nextcloud's original entrypoint
run_as() {
    if [ "$(id -u)" = 0 ]; then
        su -p www-data -s /bin/sh -c "$1"
    else
        sh -c "$1"
    fi
}

run_as 'php occ app:disable accessibility'
run_as 'php occ app:disable bruteforcesettings'
run_as 'php occ app:disable comments'

# Run the server
exec "$@"

Source: nextcloud-fuss

@donquixote
Copy link

Here is the way I did it:
In my Dockerfile, I added RUN and COPY commands that:

  • modify the existing entrypoint.sh file, removing the exec "$@" line, using sed -i 's/exec "$@"//g' /entrypoint.sh.
  • move and rename that modified /entrypoint.sh to /entrypoint.d/10-nextcloud-install.sh
  • add an additional /entrypoint.d/20-post-install.sh (using COPY).
  • add a new /entrypoint.sh with a loop to execute all *.sh files within /entrypoint.d/. At the end of this script I added the exec "$@" line. This is now the main entrypoint script.

Benefits:

  • I don't have to maintain a copy of /entrypoint.sh.
  • I can add any number of additional startup scripts in /entrypoint.d/.

@J0WI
Copy link
Contributor

J0WI commented Jun 22, 2023

Closing in favour of #1964.

@J0WI J0WI closed this as completed Jun 22, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

9 participants