ohmyzsh/plugins/containerenv/README.md
James Nesbitt b7b60b4641 ContainerEnv interpretation
- new containerenv plugin, based off of the toolbox plugin
  - interpret the /run/.containerenv file for parametrization
wq  - add a prompt info function that can be used to put container info into the prompt

Signed-off-by: James Nesbitt <james.r.nesbitt@gmail.com>
2026-02-22 20:27:18 +02:00

82 lines
2.7 KiB
Markdown

# containerenv plugin
A lightweight plugin that provides convenient functions to access container environment variables from `/run/.containerenv`. Useful in containerized environments (Podman, Docker, Toolbox, etc.).
To use it, add `containerenv` to the plugins array in your `.zshrc` file:
```zsh
plugins=(... containerenv)
```
## Usage
### Accessor functions
Each function reads one key from `/run/.containerenv` and prints its value to stdout. On error (not in a container or missing key), a message is printed to stderr and the function returns 1.
| Function | Key | Description |
|----------|-----|-------------|
| `containerenv_engine` | `engine` | Container runtime (e.g. `podman`, `docker`) |
| `containerenv_name` | `name` | Container name |
| `containerenv_id` | `id` | Container ID |
| `containerenv_image` | `image` | Image name/reference |
| `containerenv_imageid` | `imageid` | Image ID |
| `containerenv_rootless` | `rootless` | Whether the container is rootless |
| `containerenv_graphrootmounted` | `graphRootMounted` | Whether the graph root is mounted |
**Examples:**
```bash
# Show container name
containerenv_name
# Use in a conditional or variable
if name=$(containerenv_name 2>/dev/null); then
echo "Container: $name"
fi
```
### Show all container env vars
```bash
containerenv_all
```
Prints the full contents of `/run/.containerenv`. Exits with 1 if not in a container.
### Check if running in a container
```bash
is_containerized
```
Returns 0 if `/run/.containerenv` exists, 1 otherwise. Useful in scripts:
```bash
if is_containerized; then
echo "Inside container: $(containerenv_name 2>/dev/null)"
fi
```
### Prompt integration
Use `containerenv_prompt_info` in your theme to show container info in the prompt (e.g. `📦 container-name`). If not in a container, it prints nothing. When icons are enabled, the prompt shows an engine-specific icon (🐳 docker, ⬢ podman, 📦 other) and optionally 🔓 for rootless containers.
In a custom theme or in `PROMPT`/`RPROMPT`:
```zsh
# Example: add to RPROMPT
RPROMPT='$(containerenv_prompt_info)'
```
**Environment variable**
| Variable | Description |
|----------|-------------|
| `OMZ_CONTAINERENV_PROMPT_NO_ICONS` | Set to any value (e.g. `1`) to disable prompt icons. Only the container name is shown (no engine or rootless icons). When unset, icons are shown. |
---
## About `/run/.containerenv`
Podman (and compatible runtimes) write a file at `/run/.containerenv` inside the container with key=value pairs such as `engine`, `name`, `id`, `image`, `imageid`, `rootless`, and `graphRootMounted`. This plugin reads that file; outside a container or if the file is missing, the accessors report an error.