Repository: paperless-ngx/paperless-ngx
Pull request: Fix: Make management commands aware of the container environment
<!--
Note: All PRs with code changes should be targeted to the `dev` branch, pure documentation changes can target `main`
-->

## Proposed change

<!--
Please include a summary of the change and which issue is fixed (if any) and any relevant motivation / context. List any dependencies that are required for this change. If appropriate, please include an explanation of how your proposed change can be tested. Screenshots and / or videos can also be helpful if appropriate.
-->

<!--
⚠️ Important: Pull requests that implement a new feature or enhancement *should almost always target an existing feature request* with evidence of community interest and discussion. This is in order to balance the work of implementing and maintaining new features / enhancements. If that is not currently the case, please open a feature request instead of this PR to gather feedback from both users and the project maintainers.
-->

This is the smallest change I could get working reliably.  This still allows `/init` to run, so migrations are done, environment from files is done, etc and it doesn't require an interactive session.

Basically, it makes the manage.py file aware of the container environment, but only in the Docker image.  Then users just need to add the `./manage.py` portion we previously hid.

Closes #9497 

## Type of change

<!--
What type of change does your PR introduce to Paperless-ngx?
NOTE: Please check only one box!
-->

- [x] Bug fix: non-breaking change which fixes an issue.
- [ ] New feature / Enhancement: non-breaking change which adds functionality. _Please read the important note above._
- [ ] Breaking change: fix or feature that would cause existing functionality to not work as expected.
- [ ] Documentation only.
- [ ] Other. Please explain:

## Checklist:

<!--
NOTE: PRs that do not address the following will not be merged, please do not skip any relevant items.
-->

- [ ] I have read & agree with the [contributing guidelines](https://github.com/paperless-ngx/paperless-ngx/blob/main/CONTRIBUTING.md).
- [ ] If applicable, I have included testing coverage for new code in this PR, for [backend](https://docs.paperless-ngx.com/development/#testing) and / or [front-end](https://docs.paperless-ngx.com/development/#testing-and-code-style) changes.
- [ ] If applicable, I have tested my code for new features & regressions on both mobile & desktop devices, using the latest version of major browsers.
- [ ] If applicable, I have checked that all tests pass, see [documentation](https://docs.paperless-ngx.com/development/#back-end-development).
- [ ] I have run all `pre-commit` hooks, see [documentation](https://docs.paperless-ngx.com/development/#code-formatting-with-pre-commit-hooks).
- [ ] I have made corresponding changes to the documentation as needed.
- [ ] I have checked my modifications for any breaking changes.


Problem statement:
[BUG] Management command "createsuperuser" not working anymore with 2.15.0 #beta
### Description

The management command `createsuperuser` is broken since the beta v2.15.0. I tested it with the "latest" Tag, it worked, then changed to the "beta" Tag and it was broken.


### Steps to reproduce

Using docker-compose (Synology):

1. Run compose project with `ghcr.io/paperless-ngx/paperless-ngx:latest`
2. Run `docker-compose run --rm webserver createsuperuser` (this works)
3. Run compose project with `ghcr.io/paperless-ngx/paperless-ngx:beta`
4. Run `docker-compose run --rm webserver createsuperuser` (this does not work)

### Webserver logs

```bash
docker-compose run --rm webserver createsuperuser
[+] Running 4/0
 ⠿ Container paperless-2-broker-1     Running                                                                                                                                                                                                                                                                                                                                               0.0s
 ⠿ Container paperless-2-tika-1       Running                                                                                                                                                                                                                                                                                                                                               0.0s
 ⠿ Container paperless-2-gotenberg-1  Running                                                                                                                                                                                                                                                                                                                                               0.0s
 ⠿ Container paperless-2-db-1         Running                                                                                                                                                                                                                                                                                                                                               0.0s
[init-start] paperless-ngx docker container starting...
[init-start]  paperless-ngx docker container starting init as root
[env-init] Checking for environment from files
[env-init] No *_FILE environment found
[init-redis-wait] Waiting for Redis to report ready
[init-db-wait] Waiting for postgresql to report ready
[init-db-wait] Waiting for PostgreSQL to start...
[init-tesseract-langs] Checking if additional teseract languages needed
[init-tesseract-langs] No additional installs requested
[init-user] Mapping UID for paperless to 1026
[init-user] Mapping GID for paperless to 100
[init-folders] Checking for folder existence
mkdir: created directory '/tmp/paperless'
[init-folders] Adjusting file and folder permissions
changed ownership of '/tmp/paperless' from root:root to paperless:paperless
Waiting for Redis...
Connected to Redis broker.
[init-redis-wait] Redis ready
Connected to PostgreSQL
[init-db-wait] Database is ready
[init-migrations] Apply database migrations...
Operations to perform:
  Apply all migrations: account, admin, auditlog, auth, authtoken, contenttypes, django_celery_results, documents, guardian, mfa, paperless, paperless_mail, sessions, socialaccount
Running migrations:
  No migrations to apply.
[init-superuser] Not creating superuser
[init-checks] Running Django checks
/usr/local/lib/python3.12/site-packages/whoosh/analysis/filters.py:50: SyntaxWarning: invalid escape sequence '\w'
  url_pattern = rcompile("""
/usr/local/lib/python3.12/site-packages/whoosh/analysis/filters.py:148: SyntaxWarning: invalid escape sequence '\S'
  """Interleaves the results of two or more filters (or filter chains).
/usr/local/lib/python3.12/site-packages/whoosh/analysis/intraword.py:37: SyntaxWarning: invalid escape sequence '\S'
  """Given a set of words (or any object with a ``__contains__`` method),
/usr/local/lib/python3.12/site-packages/whoosh/analysis/intraword.py:224: SyntaxWarning: invalid escape sequence '\S'
  """Splits words into subwords and performs optional transformations on
/usr/local/lib/python3.12/site-packages/whoosh/analysis/intraword.py:285: SyntaxWarning: invalid escape sequence '\|'
  def __init__(self, delims=u("-_'\"()!@#$%^&*[]{}<>\|;:,./?`~=+"),

System check identified 1 issue (0 silenced).
[custom-init] /custom-cont-init.d doesn't exist, nothing to do
[init-complete] paperless-ngx docker container init completed in 27 seconds
[init-complete] Starting services
[svc-flower] Checking if we should start flower...
[svc-flower] Not starting flower
/run/s6/basedir/scripts/rc.init: 76: createsuperuser: not found
```

### Browser logs

```bash

```

### Paperless-ngx version

2.15.0 beta

### Host OS

Synology DSM 7.2

### Installation method

Docker - official image

### System status

```json

```

### Browser

_No response_

### Configuration changes

_No response_

### Please confirm the following

- [x] I believe this issue is a bug that affects all users of Paperless-ngx, not something specific to my installation.
- [x] This issue is not about the OCR or archive creation of a specific file(s). Otherwise, please see above regarding OCR tools.
- [x] I have already searched for relevant existing issues and discussions before opening this report.
- [x] I have updated the title field above with a concise description.

Hints from issue discussion:

Commit message: Updates manage.py so it is aware of the container environment provided by s6-overlay

Patch to review:
diff --git a/Dockerfile b/Dockerfile
index f56422a8ee7..a7ea8b70f8d 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -239,6 +239,7 @@ COPY --from=compile-frontend --chown=1000:1000 /src/src/documents/static/fronten
 # add users, setup scripts
 # Mount the compiled frontend to expected location
 RUN set -eux \
+  && sed -i '1s|^#!/usr/bin/env python3|#!/command/with-contenv python3|' manage.py \
   && echo "Setting up user/group" \
     && addgroup --gid 1000 paperless \
     && useradd --uid 1000 --gid paperless --home-dir /usr/src/paperless paperless \
diff --git a/docker/compose/docker-compose.mariadb-tika.yml b/docker/compose/docker-compose.mariadb-tika.yml
index c158797a576..73f30da788d 100644
--- a/docker/compose/docker-compose.mariadb-tika.yml
+++ b/docker/compose/docker-compose.mariadb-tika.yml
@@ -24,7 +24,7 @@
 # - Copy this file as 'docker-compose.yml' and the files 'docker-compose.env'
 #   and '.env' into a folder.
 # - Run 'docker compose pull'.
-# - Run 'docker compose run --rm webserver createsuperuser' to create a user.
+# - Run 'docker compose run --rm webserver ./manage.py createsuperuser' to create a user.
 # - Run 'docker compose up -d'.
 #
 # For more extensive installation and update instructions, refer to the
diff --git a/docker/compose/docker-compose.mariadb.yml b/docker/compose/docker-compose.mariadb.yml
index 0b7a0fd3e57..2a00a0be6ae 100644
--- a/docker/compose/docker-compose.mariadb.yml
+++ b/docker/compose/docker-compose.mariadb.yml
@@ -20,7 +20,7 @@
 # - Copy this file as 'docker-compose.yml' and the files 'docker-compose.env'
 #   and '.env' into a folder.
 # - Run 'docker compose pull'.
-# - Run 'docker compose run --rm webserver createsuperuser' to create a user.
+# - Run 'docker compose run --rm webserver ./manage.py createsuperuser' to create a user.
 # - Run 'docker compose up -d'.
 #
 # For more extensive installation and update instructions, refer to the
diff --git a/docker/compose/docker-compose.portainer.yml b/docker/compose/docker-compose.portainer.yml
index 2132c67a69f..f655131eba0 100644
--- a/docker/compose/docker-compose.portainer.yml
+++ b/docker/compose/docker-compose.portainer.yml
@@ -24,7 +24,7 @@
 # - Click 'Deploy the stack' and wait for it to be deployed
 # - Open the list of containers, select paperless_webserver_1
 # - Click 'Console' and then 'Connect' to open the command line inside the container
-# - Run 'python3 manage.py createsuperuser' to create a user
+# - Run 'python3 manage.py ./manage.py createsuperuser' to create a user
 # - Exit the console
 #
 # For more extensive installation and update instructions, refer to the
diff --git a/docker/compose/docker-compose.postgres-tika.yml b/docker/compose/docker-compose.postgres-tika.yml
index 60bfada5f65..fdd79427552 100644
--- a/docker/compose/docker-compose.postgres-tika.yml
+++ b/docker/compose/docker-compose.postgres-tika.yml
@@ -24,7 +24,7 @@
 # - Copy this file as 'docker-compose.yml' and the files 'docker-compose.env'
 #   and '.env' into a folder.
 # - Run 'docker compose pull'.
-# - Run 'docker compose run --rm webserver createsuperuser' to create a user.
+# - Run 'docker compose run --rm webserver ./manage.py createsuperuser' to create a user.
 # - Run 'docker compose up -d'.
 #
 # For more extensive installation and update instructions, refer to the
diff --git a/docker/compose/docker-compose.postgres.yml b/docker/compose/docker-compose.postgres.yml
index da2953b7b47..eeb9ddfa2ff 100644
--- a/docker/compose/docker-compose.postgres.yml
+++ b/docker/compose/docker-compose.postgres.yml
@@ -20,7 +20,7 @@
 # - Copy this file as 'docker-compose.yml' and the files 'docker-compose.env'
 #   and '.env' into a folder.
 # - Run 'docker compose pull'.
-# - Run 'docker compose run --rm webserver createsuperuser' to create a user.
+# - Run 'docker compose run --rm webserver ./manage.py createsuperuser' to create a user.
 # - Run 'docker compose up -d'.
 #
 # For more extensive installation and update instructions, refer to the
diff --git a/docker/compose/docker-compose.sqlite-tika.yml b/docker/compose/docker-compose.sqlite-tika.yml
index 54292a845d1..a6340b1663e 100644
--- a/docker/compose/docker-compose.sqlite-tika.yml
+++ b/docker/compose/docker-compose.sqlite-tika.yml
@@ -24,7 +24,7 @@
 # - Copy this file as 'docker-compose.yml' and the files 'docker-compose.env'
 #   and '.env' into a folder.
 # - Run 'docker compose pull'.
-# - Run 'docker compose run --rm webserver createsuperuser' to create a user.
+# - Run 'docker compose run --rm webserver ./manage.py createsuperuser' to create a user.
 # - Run 'docker compose up -d'.
 #
 # For more extensive installation and update instructions, refer to the
diff --git a/docker/compose/docker-compose.sqlite.yml b/docker/compose/docker-compose.sqlite.yml
index 1ca5fe86b29..92c126bca39 100644
--- a/docker/compose/docker-compose.sqlite.yml
+++ b/docker/compose/docker-compose.sqlite.yml
@@ -17,7 +17,7 @@
 # - Copy this file as 'docker-compose.yml' and the files 'docker-compose.env'
 #   and '.env' into a folder.
 # - Run 'docker compose pull'.
-# - Run 'docker compose run --rm webserver createsuperuser' to create a user.
+# - Run 'docker compose run --rm webserver ./manage.py createsuperuser' to create a user.
 # - Run 'docker compose up -d'.
 #
 # For more extensive installation and update instructions, refer to the
diff --git a/docs/setup.md b/docs/setup.md
index 7bb71a761ff..d3f43aca7f3 100644
--- a/docs/setup.md
+++ b/docs/setup.md
@@ -135,7 +135,7 @@ account. The script essentially automatically performs the steps described in [D
     execute the following command:
 
     ```shell-session
-    docker compose run --rm webserver createsuperuser
+    docker compose run --rm webserver ./manage.py createsuperuser
     ```
 
     or using docker exec from within the container:
diff --git a/install-paperless-ngx.sh b/install-paperless-ngx.sh
index 3977cece93a..d901353a388 100755
--- a/install-paperless-ngx.sh
+++ b/install-paperless-ngx.sh
@@ -402,6 +402,6 @@ if [ "$DATABASE_BACKEND" == "postgres" ] || [ "$DATABASE_BACKEND" == "mariadb" ]
 	docker compose stop
 fi
 
-docker compose run --rm -e DJANGO_SUPERUSER_PASSWORD="$PASSWORD" webserver createsuperuser --noinput --username "$USERNAME" --email "$EMAIL"
+docker compose run --rm -e DJANGO_SUPERUSER_PASSWORD="$PASSWORD" webserver ./manage.py createsuperuser --noinput --username "$USERNAME" --email "$EMAIL"
 
 docker compose up --detach


Code review comments:
diff_hunk: @@ -24,7 +24,7 @@
 # - Click 'Deploy the stack' and wait for it to be deployed
 # - Open the list of containers, select paperless_webserver_1
 # - Click 'Console' and then 'Connect' to open the command line inside the container
-# - Run 'python3 manage.py createsuperuser' to create a user
+# - Run 'python3 manage.py ./manage.py createsuperuser' to create a user
path: docker/compose/docker-compose.portainer.yml
text: @user1:
```suggestion
# - Run 'python3 ./manage.py createsuperuser' to create a user
```