devcontainer-env
Bridge devcontainers and the host environment — run host commands with devcontainer service environments and automatically rewrite container service URLs to host ports.
Requirements
- Rust 1.70+ (
rustc,cargo)
macOS (Homebrew):
Nix:
Installation
Cargo (Crates.io)
Nix (recommended)
Run directly without installing:
Or install into your profile:
Usage
Export Environment Variables
To make persistent, add to your .envrc file. Then run direnv allow to enable it.
Run Commands
Execute commands with devcontainer environment variables:
Inspect Configuration
View parsed devcontainer configuration and service port mappings:
CLI Reference
Usage: devcontainer-env <COMMAND>
Commands:
export Export environment variables as shell statements
exec Execute command with devcontainer environment
inspect Display parsed devcontainer configuration
help Print help message
Options:
-h, --help Print help
-V, --version Print version
devcontainer-env export — Output environment variables from containerEnv as shell statements.
Only variables defined in the containerEnv section are exported to the host (not service configuration or service environment variables). See Configuration for an example.
devcontainer-env exec — Run a command with devcontainer environment available:
The -- separator is required. Everything after -- is passed to the command.
devcontainer-env inspect — Parse and display the devcontainer configuration:
Configuration
Configure your .devcontainer/devcontainer.json to define environment variables and services:
devcontainer.json
docker-compose.yml
services:
workspace:
image: "mcr.microsoft.com/devcontainers/base:noble"
command: sleep infinity
postgres:
image: postgres:18-bookworm
restart: unless-stopped
volumes:
- postgres:/var/lib/postgres
environment:
POSTGRES_DB: my-project
POSTGRES_USER: vscode
POSTGRES_HOST_AUTH_METHOD: trust
healthcheck:
test:
interval: 1s
timeout: 5s
retries: 10
ports:
- 5432
volumes:
postgres:
Port Mapping
Use ports: [<PORT>] syntax (not "HOST:PORT") to let Docker assign random available host ports. This prevents conflicts when running multiple devcontainers or projects simultaneously. devcontainer-env export automatically detects the Docker-assigned host port and makes it available in exported environment variables. Do not use forwardPorts in devcontainer.json — rely on docker-compose.yml port mapping instead.
License
MIT — Copyright (c) 2025 devcontainer-env