agent-zero icon indicating copy to clipboard operation
agent-zero copied to clipboard

Docker fails to load .env vars

Open GratefulDave opened this issue 7 months ago • 1 comments

ISSUE 1: When running docker with: docker run -p $PORT:80 -v /Users/username/PycharmProjects/a0 frdel/agent-zero-run

Instance fails to load .env vars.

STEPS TO REPRODUCE:

  • Create new folder with proper directory structure and .env
otal 4.0K
drwxr-xr-x   9 user staff  288 Jun 14 01:07 .
drwxr-xr-x 118 user staff 3.7K Jun 14 01:36 ..
-rw-r--r--   1 user staff  965 Jun 14 01:43 .env
drwxr-xr-x   2 user staff   64 Jun 14 01:01 instruments
drwxr-xr-x   2 user staff   64 Jun 14 01:06 knowledge
drwxr-xr-x   2 user staff   64 Jun 14 01:00 memory
drwxr-xr-x   2 user staff   64 Jun 14 01:01 prompts
-rw-r--r--   1 user staff    0 Jun 14 01:07 settings.json
drwxr-xr-x   2 user staff   64 Jun 14 01:07 workdir

Instance loads. I go to localhost and api keys are empty.

If I update keys, the changes are not retained locally.

ISSUE 2 Unable to set JSON MCP Configuration file to work with Github MCP-Toolkip running in another container. I tried too many things to list.

Can you provide a sample config?

ENIVORNMENT: Mac M4/128GB Sequoia 15.5 Ghostty Terminal

GratefulDave avatar Jun 14 '25 06:06 GratefulDave

My short-term solution is to use:

#!/bin/bash

# Exit immediately if a command exits with a non-zero status.
set -e

# --- Pre-flight Checks ---
# Check if .env file exists in the current directory
if [ ! -f .env ]; then
  echo "Error: .env file not found. Please create one with a PORT variable (e.g., PORT=8080)."
  exit 1
fi

# Check if the data directory exists, create if not
if [ ! -d "data" ]; then
  echo "Data directory not found. Creating './data' for you."
  mkdir -p data
fi

# --- Load Environment and Run ---
# Export the variables from the .env file for use on the command line
export $(grep -v '^#' .env | xargs)

# Check if PORT was loaded correctly
if [ -z "$PORT" ]; then
    echo "Error: PORT is not set in your .env file."
    exit 1
fi

echo "Starting container, mapping host port $PORT to container port 80..."
echo "Data volume will be mounted from: $(pwd)/data"

# Execute the docker command
# - We use "$(pwd)/data" to get the absolute path to our local data folder.
# - We use --env-file to pass all variables from .env into the container.
docker run \
  -p $PORT:80 \
  --env-file .env \
  -v "$(pwd)/data:/a0" \
  frdel/agent-zero-run "$@"

GratefulDave avatar Jun 14 '25 23:06 GratefulDave

Okay, well I solved my own issue. Maybe you can let me know if I got right or is there an easier/better way to do this:

STEP 1: I created my custom Dockerfile:

# --- The Correct and Final Dockerfile for a0-custom-run ---
# This version is simplified and correct for the Kali Linux base image.

# Start from the official agent-zero image, which is based on Kali Linux.
FROM frdel/agent-zero-run

# Switch to the root user to gain permission to install software.
USER root

#
# Install the Docker CLI using Kali's built-in package repository.
# The package name in the default Kali repos is 'docker.io'.
# This avoids all issues with external Docker repositories.
#
RUN apt-get update && apt-get install -y docker.io

STEP 2: Build docker image docker build -t a0-custom-run .

STEP 3: I run with this script run_a0.sh:

#!/bin/bash

# ==============================================================================
# Final Production Runner for Agent-Zero
# ==============================================================================
# This script starts the Agent-Zero container using the custom-built image
# that includes the necessary Docker CLI tools.
# ==============================================================================

# --- Script Configuration ---
set -e
set -u
set -o pipefail

# --- User-Friendly Colors ---
COLOR_GREEN='\033[0;32m'
COLOR_YELLOW='\033[1;33m'
COLOR_RED='\033[0;31m'
NC='\033[0m'

# --- Pre-flight Checks ---
if ! command -v docker &> /dev/null; then
    echo -e "${COLOR_RED}Error: 'docker' command not found. Please start Docker Desktop.${NC}"
    exit 1
fi
if [ ! -f .env ]; then
  echo -e "${COLOR_RED}Error: '.env' file not found. Create one with a PORT variable.${NC}"
  exit 1
fi
if [ ! -S /var/run/docker.sock ]; then
    echo -e "${COLOR_RED}Error: Docker socket not found. Is Docker Desktop running?${NC}"
    exit 1
fi

# --- Load Environment from .env file ---
echo "Loading environment variables from .env file..."
while IFS= read -r line || [[ -n "$line" ]]; do
    if [[ "$line" =~ ^\s*# ]] || [[ -z "$line" ]]; then continue; fi
    export "$line"
done < .env

if [ -z "${PORT:-}" ]; then
    echo -e "${COLOR_RED}Error: The PORT variable is not set in your .env file.${NC}"
    exit 1
fi

# --- Execute Docker Command ---
echo -e "\n${COLOR_GREEN}--- Starting Agent-Zero ---${NC}"
echo -e "Using Custom Image: ${COLOR_YELLOW}a0-custom-run${NC}"
echo -e "Project Directory:  ${COLOR_YELLOW}$(pwd)${NC} -> ${COLOR_YELLOW}/a0${NC}"
echo -e "${COLOR_RED}Docker Engine Access: Granted${NC}"
echo -e "${COLOR_GREEN}Container starting... (Press Ctrl+C to stop)${NC}\n"

#
# The Docker client is permanently installed in our 'a0-custom-run' image.
# The command block below is now syntactically correct with no
# comments or blank lines between the backslashes.
#
docker run \
  --rm \
  -p "${PORT}:80" \
  --env-file .env \
  -v "$(pwd):/a0" \
  -v /var/run/docker.sock:/var/run/docker.sock \
  a0-custom-run "$@"
  1. Data persists.
  2. Github MCP-Toolkit works.

GratefulDave avatar Jun 15 '25 02:06 GratefulDave