Nextcloud, docker and environment variables.

I'm trying to deploy a functioning instance of Nextcloud App for my homelab. I'm having a difficult time trying to automate (or at least document provisioning steps for myself).

My hope was that I can use environment variables in docker-compose to provision a new instance without the need to modify config.php manually (or map it to the external source).

My docker compose file is:

version: '3.5'

volumes:
  nextcloud_app:
  nextcloud_db:

services:
  nextcloud_app:
    image: nextcloud:23
    container_name: nextcloud_app
    restart: always
    ports:
      - 8088:80
    links:
      - nextcloud_db
      - nextcloud_redis
    depends_on:
      - nextcloud_db
      - nextcloud_redis
    volumes:
      - ${CONTAINERS}/nextcloud_app:/var/www/html
      - /media/docs:/var/www/html/data
    environment:
      - NEXTCLOUD_ADMIN_USER=${NEXTCLOUD_ADMIN_USER}
      - NEXTCLOUD_ADMIN_PASSWORD=${NEXTCLOUD_ADMIN_PASSWORD}
      - MYSQL_HOST=nextcloud_db
      - MYSQL_DATABASE=nextcloud
      - MYSQL_USER=nextcloud
      - MYSQL_PASSWORD=${NEXTCLOUD_MYSQL_PASSWORD}
      - NEXTCLOUD_DATA_DIR=/var/www/html/data
      - TRUSTED_PROXIES=172.20.50.0/24
      - NEXTCLOUD_TRUSTED_DOMAINS=nextcloud.${DOMAIN}
      # - REDIS_HOST=nextcloud_redis
      # - REDIS_HOST_PASSWORD=${REDIS_NEXTCLOUD_PASSWORD}
      # - REDIS_HOST_PORT=6379
    labels:
      - traefik.enable=true
      - traefik.http.routers.nextcloud_app.rule=Host(`nextcloud.${DOMAIN}`)
      - traefik.http.routers.nextcloud_app.entrypoints=websecured
      - traefik.http.routers.nextcloud_app.tls.certresolver=myresolver
      - traefik.http.routers.nextcloud_app.middlewares=nextcloud,nextcloud_redirect
      - traefik.http.routers.nextcloud_app.tls=true
      - traefik.http.middlewares.nextcloud.headers.customFrameOptionsValue=ALLOW-FROM https://${DOMAIN}
      - traefik.http.middlewares.nextcloud.headers.contentSecurityPolicy=frame-ancestors 'self' ${DOMAIN} *.${DOMAIN}
      - traefik.http.middlewares.nextcloud.headers.stsSeconds=155520011
      - traefik.http.middlewares.nextcloud.headers.stsIncludeSubdomains=true
      - traefik.http.middlewares.nextcloud.headers.stsPreload=true
      - traefik.http.middlewares.nextcloud.headers.customresponseheaders.X-Frame-Options=SAMEORIGIN
      - traefik.http.middlewares.nextcloud_redirect.redirectregex.permanent=true
      - traefik.http.middlewares.nextcloud_redirect.redirectregex.regex=https://(.*)/.well-known/(card|cal)dav
      - traefik.http.middlewares.nextcloud_redirect.redirectregex.replacement=https://$${1}/remote.php/dav/

  nextcloud_db:
    image: linuxserver/mariadb
    container_name: nextcloud_db
    restart: always
    ports: 
      - 3306:3306
    environment:
      MYSQL_DATABASE: nextcloud
      MYSQL_USER: nextcloud
      MYSQL_PASSWORD: ${NEXTCLOUD_MYSQL_PASSWORD}
      MYSQL_RANDOM_ROOT_PASSWORD: ${NEXTCLOUD_MYSQL_ROOT_PASSWORD}
    volumes:
      - "${CONTAINERS}/nextcloud_db:/var/lib/mysql" # DO NOT REMOVE

  nextcloud_redis:
    image: redis:latest
    container_name: nextcloud_redis
    restart: always
    command: redis-server --requirepass ${REDIS_NEXTCLOUD_PASSWORD}
    volumes:
      - ${CONTAINERS}/nextcloud_redis:/var/lib/redis

I've read thru documentation and some other posts on here and it seems like what I'm looking for is possible. My docker-compose file is updated with all of the required environment variables, however when the instance is provisioned, Nextcloud appears to ignore all those variables that I set.

If we start with a functioning, previously configured instance of nextcloud (nextcloud_app) which uses mysql db in nextcloud_db container, here are my steps:

  1. I stop nextcloud_app container
  2. I remove nextcloud_app volume and app itself

​

docker volume rm mybox_nextcloud_app
sudo rm -rf /data/mybox/containers/nextcloud_app

3. Provision new instance

docker-compose -f docker-compose.nextcloud.yml up -d --remove-orphan

Once provisioned, I can inspect variables for that container in Portainer and confirm that they're set correctly.

When looking at logs for the nextcloud_app container, I can see that it's trying to initialize a brand new instance. It picks up the NEXTCLOUD_ADMIN_USER value, however fails to reach/access the db. I'm not 100% sure what DB it's trying to reach, this info isn't in the stack trace.

Initializing nextcloud 23.0.11.1 ...
New nextcloud instance
Installing with MySQL database
Starting nextcloud installation
Username is invalid because files already exist for this user
Retrying install...
MySQL username and/or password not valid
 -> You need to enter details of an existing account.
Trace: #0 /var/www/html/lib/private/Setup.php(354): OC\Setup\MySQL->setupDatabase('%NEXTCLOUD_ADMIN_USER_VALUE%')
#1 /var/www/html/core/Command/Maintenance/Install.php(108): OC\Setup->install(Array)
#2 /var/www/html/3rdparty/symfony/console/Command/Command.php(255): OC\Core\Command\Maintenance\Install->execute(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#3 /var/www/html/3rdparty/symfony/console/Application.php(1009): Symfony\Component\Console\Command\Command->run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#4 /var/www/html/3rdparty/symfony/console/Application.php(273): Symfony\Component\Console\Application->doRunCommand(Object(OC\Core\Command\Maintenance\Install), Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#5 /var/www/html/3rdparty/symfony/console/Application.php(149): Symfony\Component\Console\Application->doRun(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#6 /var/www/html/lib/private/Console/Application.php(211): Symfony\Component\Console\Application->run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#7 /var/www/html/console.php(100): OC\Console\Application->run()
#8 /var/www/html/occ(11): require_once('/var/www/html/c...')
   
}
Previous: Doctrine\DBAL\Exception: Failed to connect to the database: An exception occurred in the driver: SQLSTATE[HY000] [1045] Access denied for user 'nextcloud'@'nextcloud_app.mybox_default' (using password: YES)
Trace: #0 /var/www/html/lib/private/Setup/MySQL.php(64): OC\DB\Connection->connect()

When I look at the config.php in the newly generated container/volume I can see that that a different/new instance ID and password are used.

If I update config.php manually and restart the container, I can proceed with initializing the app using my existing DB. However this isn't practical for my application.

Right now, it looks like there is some issue with environment variables or initialize routine for the docker container. I also see that NEXTCLOUD_TRUSTED_DOMAINS values are not picked up or reflected in there.

I am not an expert at docker, so it's very possible that I'm missing something relatively simple.

Can anyone help me to understand what's going on or what I'm potentially doing wrong here?

Posted Dec 6, 2022, 2:32 PM UTC · edited

14
JeanneD4Rk•

This is not how it works. If config.php does not exist, a new instance is created and Env vars are used by docker entrypoint to populate the file. If the file already exists, it just ignores env vars and proceed to start Nextcloud.

2
SoVeryAwkwardOP•

I'm not sure I understand what you mean. I am trying to create a new nextcloud_app instance and point it to the existing Nextcloud DB that I have.

I don't have a config.php after I remove existing app container and nextcloud_app volume.

When I re-create a container with docker-compose, my expectation is to find a newly provisioned config.php with values that I provided via docker env vars.

What I find instead is that Nextcloud attempts to do a fresh install, which is not what I want.

1
pippikagga•

I am not sure about some things. But you could try the following:You have not defined the MySQL target host. Just set it with MYSQL_HOST=nextcloud_db in the environment variables.With this you have to pack the database, nextcloud and redis container in a own network, otherwise the name resolution from the nextcloud to the database container "nextcloud_db" does not work in the default Network.Add the following at the bottom of the config file:

networks:
nextcloud_internal:
name: nextcloud_internal

And this to each container in your .yml:

networks:
- nextcloud_internal

-----------------------------------------

Extra hint: You seem to work with an .env file. You could use "convert" to check if all environment variables are translated correctly.

docker compose -f "COMPOSEFILE.yml" convert

2
SoVeryAwkwardOP•

Hey, thanks for the response and an idea u/pippikagga

I think generally, it's probably a good idea to keep Nextcloud instance on a separate (defined) network.

It didn't resolve my issue. unfortunately. The container still gets provisioned with new/random values and requires direct/manual changes.

​

Initializing nextcloud 23.0.11.1 ...

New nextcloud instance

Installing with MySQL database

Starting nextcloud installation

Username is invalid because files already exist for this user

Retrying install...

MySQL username and/or password not valid

-> You need to enter details of an existing account.

Trace: #0 /var/www/html/lib/private/Setup.php(354): OC\Setup\MySQL->setupDatabase('NEXTCLOUD_ADMIN_USER')

#1 /var/www/html/core/Command/Maintenance/Install.php(108): OC\Setup->install(Array)

#2 /var/www/html/3rdparty/symfony/console/Command/Command.php(255): OC\Core\Command\Maintenance\Install->execute(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))

#3 /var/www/html/3rdparty/symfony/console/Application.php(1009): Symfony\Component\Console\Command\Command->run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))

#4 /var/www/html/3rdparty/symfony/console/Application.php(273): Symfony\Component\Console\Application->doRunCommand(Object(OC\Core\Command\Maintenance\Install), Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))

#5 /var/www/html/3rdparty/symfony/console/Application.php(149): Symfony\Component\Console\Application->doRun(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))

#6 /var/www/html/lib/private/Console/Application.php(211): Symfony\Component\Console\Application->run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))

#7 /var/www/html/console.php(100): OC\Console\Application->run()

#8 /var/www/html/occ(11): require_once('/var/www/html/c...')



}

Previous: Doctrine\DBAL\Exception: Failed to connect to the database: An exception occurred in the driver: SQLSTATE[HY000] [1045] Access denied for user 'nextcloud'@'nextcloud_app.nextcloud_internal' (using password: YES)

Trace: #0 /var/www/html/lib/private/Setup/MySQL.php(64): OC\DB\Connection->connect()

#1 /var/www/html/lib/private/Setup.php(354): OC\Setup\MySQL->setupDatabase('NEXTCLOUD_ADMIN_USER')

#2 /var/www/html/core/Command/Maintenance/Install.php(108): OC\Setup->install(Array)

#3 /var/www/html/3rdparty/symfony/console/Command/Command.php(255): OC\Core\Command\Maintenance\Install->execute(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))

#4 /var/www/html/3rdparty/symfony/console/Application.php(1009): Symfony\Component\Console\Command\Command->run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))

#5 /var/www/html/3rdparty/symfony/console/Application.php(273): Symfony\Component\Console\Application->doRunCommand(Object(OC\Core\Command\Maintenance\Install), Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))

#6 /var/www/html/3rdparty/symfony/console/Application.php(149): Symfony\Component\Console\Application->doRun(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))

#7 /var/www/html/lib/private/Console/Application.php(211): Symfony\Component\Console\Application->run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))

#8 /var/www/html/console.php(100): OC\Console\Application->run()

#9 /var/www/html/occ(11): require_once('/var/www/html/c...')

#10 {main}

2