Skip to content

Commit 9a5f0cb

Browse files
committed
docs: add container terminal access documentation
Documents the openTerminal() JavaScript function for opening container console and log viewers. Includes parameter reference, usage examples, and dashboard tile considerations. Also adds web terminal URL patterns to file-path-reference.
1 parent 576a0bb commit 9a5f0cb

File tree

2 files changed

+137
-0
lines changed

2 files changed

+137
-0
lines changed

docs/advanced/docker-integration.md

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,129 @@ $stats = json_decode($output[0], true);
179179
?>
180180
```
181181

182+
## Container Terminal Access
183+
184+
Unraid® provides a built-in `openTerminal()` JavaScript function for opening container console sessions and viewing container logs in a popup window. This function handles spawning the ttyd terminal server and opening the WebSocket connection.
185+
186+
### The openTerminal Function
187+
188+
The global `openTerminal(tag, name, more)` function is defined in Unraid's core JavaScript (HeadInlineJS.php) and is available on all pages.
189+
190+
| Parameter | Description | Values |
191+
|-----------|-------------|--------|
192+
| `tag` | Terminal type | `'docker'` for containers, `'ttyd'` or `'syslog'` for system |
193+
| `name` | Container name | Must match Docker container name exactly |
194+
| `more` | Shell or log flag | Shell command (e.g., `'bash'`, `'sh'`) or `'.log'` for logs |
195+
196+
### Opening Container Console
197+
198+
To open an interactive shell session in a container:
199+
200+
```javascript
201+
// Open bash console in a container
202+
openTerminal('docker', 'mycontainer', 'bash');
203+
204+
// Open sh console (for containers without bash)
205+
openTerminal('docker', 'mycontainer', 'sh');
206+
```
207+
208+
### Viewing Container Logs
209+
210+
To open a live-streaming log viewer for a container, pass `'.log'` as the third parameter:
211+
212+
```javascript
213+
// Open container logs in a terminal window
214+
openTerminal('docker', 'mycontainer', '.log');
215+
```
216+
217+
{: .important }
218+
> The `'.log'` value must include the leading dot. This tells Unraid to open a log viewer instead of a shell session.
219+
220+
### How It Works Internally
221+
222+
When `openTerminal()` is called, it:
223+
224+
1. **Sanitizes the name**: Replaces spaces and `#` characters with underscores
225+
2. **Opens a popup window**: Creates a sized popup for the terminal
226+
3. **Calls OpenTerminal.php**: Makes an AJAX request to `/webGui/include/OpenTerminal.php` which spawns the ttyd process
227+
4. **Navigates to socket URL**: After a short delay, redirects the popup to the WebSocket URL:
228+
- Logs: `/logterminal/{name}.log/`
229+
- Console: The function constructs the appropriate webterminal URL
230+
231+
```javascript
232+
// Simplified view of what openTerminal does internally
233+
function openTerminal(tag, name, more) {
234+
name = name.replace(/[ #]/g, "_");
235+
var popup = window.open('', name + (more == '.log' ? more : ''), 'width=1200,height=800');
236+
237+
// Determine socket URL
238+
var socket = '/logterminal/' + name + (more == '.log' ? more : '') + '/';
239+
240+
// Spawn ttyd process then navigate
241+
$.get('/webGui/include/OpenTerminal.php', {tag: tag, name: name, more: more}, function() {
242+
setTimeout(function() {
243+
popup.location = socket;
244+
}, 200);
245+
});
246+
}
247+
```
248+
249+
### Using in Plugin Context Menus
250+
251+
When building container context menus (like the Docker tab does), you can add Console and Logs options:
252+
253+
```javascript
254+
// Adding terminal options to a context menu
255+
var opts = [];
256+
257+
// Console option (only when container is running)
258+
if (isRunning) {
259+
opts.push({
260+
text: 'Console',
261+
icon: 'fa-terminal',
262+
action: function(e) {
263+
e.preventDefault();
264+
openTerminal('docker', containerName, shell || 'bash');
265+
}
266+
});
267+
}
268+
269+
// Logs option
270+
opts.push({
271+
text: 'Logs',
272+
icon: 'fa-navicon',
273+
action: function(e) {
274+
e.preventDefault();
275+
openTerminal('docker', containerName, '.log');
276+
}
277+
});
278+
279+
context.attach('#' + elementId, opts);
280+
```
281+
282+
### Dashboard Tiles and openTerminal
283+
284+
On dashboard tiles, the global `openTerminal` function may not be available if `docker.js` isn't loaded. Check for its existence before calling, or use a fallback:
285+
286+
```javascript
287+
function openContainerTerminal(name, isLogs, shell) {
288+
if (typeof window.openTerminal === 'function') {
289+
// Use Unraid's built-in function
290+
window.openTerminal('docker', name, isLogs ? '.log' : (shell || 'sh'));
291+
} else {
292+
// Fallback: open terminal URL directly (less reliable)
293+
var safeName = name.replace(/[ #]/g, '_');
294+
var url = isLogs
295+
? '/logterminal/' + encodeURIComponent(safeName) + '.log/'
296+
: '/webterminal/docker/';
297+
window.open(url, '_blank');
298+
}
299+
}
300+
```
301+
302+
{: .warning }
303+
> The fallback method of directly opening the URL may not work reliably because it doesn't call `OpenTerminal.php` to spawn the ttyd process first. Always prefer using the global `openTerminal()` function when available.
304+
182305
## Working with Images
183306

184307
Common image operations including listing, pulling, and removing. When pulling images, redirect stderr to capture progress output. Always check return values for error handling.

docs/reference/file-path-reference.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -393,3 +393,17 @@ VAR_FILE="/var/local/emhttp/var.ini"
393393
- [Plugin Settings Storage](../core/plugin-settings-storage.md)
394394
- [PLG File Reference](../plg-file.md)
395395
- [Var Array Reference](var-array-reference.md)
396+
397+
## Web Terminal URLs
398+
399+
Unraid provides terminal access via ttyd WebSocket URLs. These are used by the `openTerminal()` JavaScript function (see [Docker Integration - Container Terminal Access](../advanced/docker-integration.md#container-terminal-access)).
400+
401+
| URL Pattern | Description |
402+
|-------------|-------------|
403+
| `/logterminal/{container}.log/` | Container log viewer (live streaming) |
404+
| `/webterminal/docker/{container}/{shell}/` | Container console shell |
405+
| `/webterminal/ttyd/` | System terminal |
406+
| `/webterminal/syslog/` | System log viewer |
407+
408+
{: .important }
409+
> Don't navigate to these URLs directly. Use the `openTerminal()` JavaScript function which calls `/webGui/include/OpenTerminal.php` to spawn the ttyd process first.

0 commit comments

Comments
 (0)