Disposable

2 snippets across 2 stacks - Docker, JavaScript

DKDocker

Auto-Remove on Exit (--rm)

DK · Run Options
Syntax
docker run --rm <image>
Example
docker run --rm -it python:3.12 python -c 'print(2**100)'
docker run --rm alpine:3.19 cat /etc/os-release
Output
1267650600228229401496703205376

Note Automatically removes the container and its anonymous volumes when it exits. Perfect for one-off commands and throwaway shells. Cannot be combined with --restart.

JSJavaScript

Explicit Resource Management (using)

JS · Modern Features
Syntax
using resource = acquireResource();
await using resource = acquireAsyncResource();
Example
class TempFile {
  constructor(path) {
    this.path = path;
    console.log(`Created: ${path}`);
  }

  [Symbol.dispose]() {
    console.log(`Cleaned up: ${this.path}`);
  }
}

{
  using file = new TempFile("/tmp/data.txt");
  // Use file here...
} // Automatically calls file[Symbol.dispose]() when block exits
Output
"Created: /tmp/data.txt"
"Cleaned up: /tmp/data.txt"

Note TC39 Stage 3 (expected in ES2025/2026). Similar to Python's 'with' or C#'s 'using'. Guarantees cleanup even if an error is thrown. Use Symbol.asyncDispose for async cleanup.

Frequently asked questions

How does Docker handle disposable?
This task is covered in 2 stacks on this page: Docker, JavaScript. The "Auto-Remove on Exit (--rm)" snippet in Docker uses `docker run --rm <image>`.
Which command does the Docker example use?
The "Auto-Remove on Exit (--rm)" snippet uses `docker run --rm <image>`, from the Run Options section of the Docker cheat sheet.
Which stacks cover "disposable" on this page?
Docker, JavaScript. Together they hold 2 copy-ready snippets for this task.
Is there anything to watch out for?
Yes. For "Auto-Remove on Exit (--rm)": Automatically removes the container and its anonymous volumes when it exits. Perfect for one-off commands and throwaway shells. Cannot be combined with --restart.