Execute commands in a running DNA container.
dna exec [OPTIONS] [SERVICE] [--] COMMAND [ARGS...]The dna exec command executes commands and arguments in a running DNA container. This is useful for running specific commands, scripts, or tools within the containerized environment without needing to attach to the container interactively.
| Service | Description |
|---|---|
develop |
Execute in development service (default) |
deploy |
Execute in deployment service |
| Option | Description |
|---|---|
-e, --env stringArray |
Set container environment variables |
-w, --workdir string |
Override path to working directory |
-T, --no-TTY |
Disable pseudo-TTY allocation |
--detach |
Execute COMMAND in the background |
--dry-run |
Dry run mode (requires --detach flag) |
--help, -h |
Show help message and exit |
| Argument | Description |
|---|---|
COMMAND [ARGS...] |
Command and arguments to execute inside the container (default: bash) |
# Execute a simple command
dna exec ls -la
# Execute with specific service
dna exec develop python3 --version
dna exec deploy ./deploy_script.sh# Execute command in specific directory
dna exec --workdir "/" -- bash -c 'tree -L 1 -a $(pwd)'
# Run tests from test directory
dna exec --workdir "/ros2_ws" -- colcon test# Set environment variables
dna exec -e "DEBUG=true" -e "LOG_LEVEL=info" -- python3 debug_script.py
# Multiple environment variables
dna exec --env "ROS_DOMAIN_ID=42" --env "VERBOSE=1" -- ros2 node list# Run command in background
dna exec --detach -- ros2 launch my_package my_launch.py
# Background with dry run
dna exec --dry-run --detach -- long_running_process.sh# Complex command with shell interpretation
dna exec -- bash -c 'echo "Hello from container: $(hostname)"'
# Execute without TTY (useful for scripts)
dna exec -T -- python3 automated_script.py
# Chain multiple commands
dna exec -- bash -c 'cd /ros2_ws && source install/setup.bash && ros2 run my_package my_node'# Build ROS workspace
dna exec --workdir "/ros2_ws" -- colcon build
# Run tests
dna exec -- pytest tests/
# Install Python packages
dna exec -- pip3 install numpy matplotlib# View logs
dna exec -- journalctl -u my_service --since "1 hour ago"
# Update packages
dna exec -- apt-get update && apt-get upgrade -y# Check environment variables
dna exec -- env | grep ROS
# Inspect file system
dna exec -- find /ros2_ws -name "*.py" | head -10
# Check running processes
dna exec -- ps aux- Single commands: For executing specific commands
- Automation: For scripted operations
- Background tasks: For non-interactive operations
- CI/CD pipelines: For automated testing and deployment
- Interactive development: When you need a full shell session
- Long-running sessions: For extended development work
- Container exploration: When you want to explore the container environment
- Working directory: Uses container's default working directory
- Environment: Inherits container's environment variables
- User context: Runs as the configured container user
- TTY allocation: Automatically allocated for interactive commands
The exec command automatically discovers the appropriate service:
- Explicit service: If you specify
developordeploy - Offline deployment: Automatically detects offline deployment services
- Default fallback: Uses
developservice if no other option
# Start long-running process in background
dna exec --detach -- python3 long_running_script.py
# Check if process is still running
docker exec DN_CONTAINER_NAME ps aux | grep python3# Start background service
dna exec --detach -- ros2 launch my_package service.launch.py
# Monitor logs
docker logs -f DN_CONTAINER_NAME
# Stop background process (if needed)
dna exec -- pkill -f "ros2 launch"Problem: "command not found" error.
Solutions:
-
Check PATH: Verify command is in container's PATH
dna exec -- which python3 dna exec -- echo $PATH
-
Use full path: Specify complete path to executable
dna exec -- /usr/local/bin/python3 script.py -
Install missing packages: Install required software
dna exec -- apt-get update && apt-get install -y package-name
Problem: Permission errors when executing commands.
Solutions:
-
Check file permissions:
dna exec -- ls -la /path/to/file -
Use appropriate user: Run as root if necessary
dna exec --user root -- command
-
Fix ownership: Correct file ownership
dna exec -- sudo chown user:group /path/to/file
Problem: Environment variables not set correctly.
Solutions:
-
Set explicitly: Use
-eflagdna exec -e "VAR=value" -- command
-
Source environment: Load environment files
dna exec -- bash -c 'source /opt/ros/humble/setup.bash && command'
Problem: Cannot execute command because container is not running.
Solutions:
-
Start container: Ensure container is running
dna up --no-attach
-
Check container status:
docker ps
# Run unit tests
dna exec --no-TTY -- pytest tests/ --junit-xml=results.xml
# Run integration tests
dna exec --detach deploy -- ./integration_tests.sh# Build project
dna exec --workdir "/ros2_ws" -- colcon build --cmake-args -DCMAKE_BUILD_TYPE=Release
# Package application
dna exec -- ./package_script.sh- Command overhead: Each
execcall has minimal overhead - Resource sharing: Commands share container resources
- Network access: Commands have same network access as container
- File system: Commands operate on container's file system
- dna attach - Attach to running containers
- dna up - Start containers
- dna run - Run commands in new containers
- Project Initialization & Configuration - Container configuration