|
1 | | -# Rsync - Docker mod for openssh-server |
| 1 | +# On-demand - Docker mod for SWAG |
2 | 2 |
|
3 | | -This mod adds rsync to openssh-server, to be installed/updated during container start. |
| 3 | +This mod gives SWAG the ability to start containers on-demand when accessed through SWAG and stop them after a period of inactivity. It takes a few seconds for containers to start on-demand, you'll need to refresh the tab or add a loading page as detailed below. |
4 | 4 |
|
5 | | -In openssh-server docker arguments, set an environment variable `DOCKER_MODS=linuxserver/mods:openssh-server-rsync` |
| 5 | +## Setup: |
| 6 | +- In SWAG's docker arguments, set an environment variable `DOCKER_MODS=linuxserver/mods:swag-ondemand` and either add a volume mapping for `/var/run/docker.sock:/var/run/docker.sock:ro`, or set an environment var `DOCKER_HOST=remoteaddress` (read the security considerations below). |
| 7 | +- Add the label `swag_ondemand=enable` to on-demand containers. |
| 8 | + ```yaml |
| 9 | + somecontainer: |
| 10 | + container_name: somecontainer |
| 11 | + ... |
| 12 | + labels: |
| 13 | + - swag_ondemand=enable |
| 14 | + ``` |
| 15 | +- Replace the following line in `/config/nginx/nginx.conf`: |
| 16 | + ```nginx |
| 17 | + access_log /config/log/nginx/access.log; |
| 18 | + ``` |
| 19 | + With: |
| 20 | + ```nginx |
| 21 | + log_format main '$remote_addr - $remote_user [$time_local] ' |
| 22 | + '"$request_method $scheme://$host$request_uri $server_protocol" ' |
| 23 | + '$status $body_bytes_sent ' |
| 24 | + '"$http_referer" "$http_user_agent"'; |
| 25 | + access_log /config/log/nginx/access.log main; |
| 26 | + ``` |
| 27 | +- *Optional* - Additional environment variables |
| 28 | + - `SWAG_ONDEMAND_STOP_THRESHOLD` - duration of inactivity in seconds before stopping on-demand containers, defaults to `600` (10 minutes). |
| 29 | + - `SWAG_ONDEMAND_CONTAINER_QUERY_SLEEP` - sleep time in seconds between querying containers, defaults to `5.0`. |
| 30 | + - `SWAG_ONDEMAND_LOG_READER_SLEEP` - sleep time in seconds between log reads, defaults to `1.0`. |
6 | 31 |
|
7 | | -If adding multiple mods, enter them in an array separated by `|`, such as `DOCKER_MODS=linuxserver/mods:openssh-server-rsync|linuxserver/mods:openssh-server-mod2` |
| 32 | +### Loading Page: |
8 | 33 |
|
9 | | -# Mod creation instructions |
| 34 | + |
10 | 35 |
|
11 | | -* Fork the repo, create a new branch based on the branch `template`. |
12 | | -* Edit the `Dockerfile` for the mod. `Dockerfile.complex` is only an example and included for reference; it should be deleted when done. |
13 | | -* Inspect the `root` folder contents. Edit, add and remove as necessary. |
14 | | -* After all init scripts and services are created, run `find ./ -path "./.git" -prune -o \( -name "run" -o -name "finish" -o -name "check" \) -not -perm -u=x,g=x,o=x -print -exec chmod +x {} +` to fix permissions. |
15 | | -* Edit this readme with pertinent info, delete these instructions. |
16 | | -* Finally edit the `.github/workflows/BuildImage.yml`. Customize the vars for `BASEIMAGE` and `MODNAME`. Set the versioning logic and `MULTI_ARCH` if needed. |
17 | | -* Ask the team to create a new branch named `<baseimagename>-<modname>`. Baseimage should be the name of the image the mod will be applied to. The new branch will be based on the `template` branch. |
18 | | -* Submit PR against the branch created by the team. |
| 36 | +Instead of showing a 502 error page, it can display a loading page and auto-refresh once the container is up. |
| 37 | + |
| 38 | +Add the following `include` to each proxy-conf where you wish to show the loading page inside the `server` section: |
| 39 | +```nginx |
| 40 | +server { |
| 41 | + ... |
| 42 | + include /config/nginx/ondemand.conf; |
| 43 | + ... |
| 44 | +``` |
| 45 | +Or set the following label if using `swag-auto-proxy`: |
| 46 | +```yaml |
| 47 | + somecontainer: |
| 48 | + container_name: somecontainer |
| 49 | + ... |
| 50 | + labels: |
| 51 | + - swag_server_custom_directive=include /config/nginx/ondemand.conf; |
| 52 | +``` |
| 53 | +### Labels: |
| 54 | +- `swag_ondemand=enable` - required for on-demand. |
| 55 | +- `swag_ondemand_urls=https://wake.domain.com,https://app.domain.com/up` - *optional* - overrides the monitored URLs for starting the container on-demand. Defaults to `https://somecontainer.,http://somecontainer.`. |
19 | 56 |
|
| 57 | +### URLs: |
| 58 | +- Accessed URLs need to start with one of `swag_ondemand_urls` to be matched, for example, setting `swag_ondemand_urls=https://plex.` will apply to `https://plex.domain.com` and `https://plex.domain.com/something`. |
| 59 | +- `swag_ondemand_urls` default to `https://somecontainer.,http://somecontainer.`, for example `https://plex.,http://plex.`. |
| 60 | +- `swag_ondemand_urls` don't need to be valid, it will work as long as it reaches swag and gets logged by nginx under `/config/log/nginx/access.log`. |
| 61 | +- The same URL can be set on multiple containers and all of them will be started when accessing that URL. |
20 | 62 |
|
21 | | -## Tips and tricks |
| 63 | +### Logging: |
| 64 | +The log file can be found under `/config/log/ondemand/ondemand.log`. |
22 | 65 |
|
23 | | -* Some images have helpers built in, these images are currently: |
24 | | - * [Openvscode-server](https://github.com/linuxserver/docker-openvscode-server/pull/10/files) |
25 | | - * [Code-server](https://github.com/linuxserver/docker-code-server/pull/95) |
| 66 | +## Security Consideration: |
| 67 | +Mapping the `docker.sock`, especially in a publicly accessible container is a security liability. Since this mod only needs read-only access to the docker api, the recommended method is to proxy the `docker.sock` via a solution like [our docker socket proxy](https://github.com/linuxserver/docker-socket-proxy), limit the access, and set `DOCKER_HOST=` to point to the proxy address. |
| 68 | + |
| 69 | +Here's a sample compose yaml snippet for `linuxserver/docker-socket-proxy`: |
| 70 | +```yaml |
| 71 | + socket-proxy: |
| 72 | + image: lscr.io/linuxserver/socket-proxy:latest |
| 73 | + container_name: socket-proxy |
| 74 | + environment: |
| 75 | + - ALLOW_START=1 |
| 76 | + - ALLOW_STOP=1 |
| 77 | + - CONTAINERS=1 |
| 78 | + - POST=0 |
| 79 | + volumes: |
| 80 | + - /var/run/docker.sock:/var/run/docker.sock:ro |
| 81 | + restart: unless-stopped |
| 82 | + read_only: true |
| 83 | + tmpfs: |
| 84 | + - /run |
| 85 | +``` |
| 86 | +Then the env var in SWAG can be set as `DOCKER_HOST=tcp://socket-proxy:2375`. This will allow docker in SWAG to be able to start/stop existing containers, but it won't be allowed to spin up new containers. |
0 commit comments