separate run commands into multiple layers (#9)

* separate scripts into multiple RUN commands

* update filenames range

* loop filenames in array way

* Add step start and complete messages
This commit is contained in:
Bowen Liang
2026-01-19 20:10:46 +08:00
committed by GitHub
parent acdc0e8a3b
commit c22387cb39
8 changed files with 26 additions and 45 deletions
+26 -17
View File
@@ -25,29 +25,38 @@ def render_snippet(snippet: str, context: dict) -> str:
def build_install_script(scripts_dir: Path, context: dict) -> str:
filenames = {
1: "01-install-system-packages.sh",
2: "02-install-languages.sh",
3: "03-install-python-packages.sh",
4: "04-install-nodejs-packages.sh",
5: "05-create-user.sh",
6: "06-configure-root.sh",
}
filenames = [
"01-install-system-packages.sh",
"02-install-languages.sh",
"03-install-python-packages.sh",
"04-install-nodejs-packages.sh",
"05-create-user.sh",
"06-configure-root.sh",
]
parts: list[str] = []
for idx in range(1, 7):
script_path = scripts_dir / filenames[idx]
for filename in filenames:
script_path = scripts_dir / filename
step_number = filename.split("-")[0].lstrip("0")
step_name = filename.split("-", 1)[1].removesuffix(".sh").replace("-", " ")
rendered = render_snippet(load_script(script_path), context)
parts.append(rendered)
combined = "\n".join(parts)
if not combined.endswith("\n"):
combined += "\n"
return "RUN <<'EOF'\n" + combined + "EOF\n"
if not rendered.endswith("\n"):
rendered += "\n"
# Add step start and complete messages
wrapped_script = f"echo '[agentbox] Step {step_number}: {step_name}'\n"
wrapped_script += rendered
wrapped_script += f"echo '[agentbox] Step {step_number} complete'\n"
parts.append("RUN <<'EOF'\n" + wrapped_script + "EOF\n")
return "\n".join(parts)
def build_user_script(scripts_dir: Path, context: dict) -> str:
user_script = render_snippet(load_script(scripts_dir / "07-configure-user.sh"), context)
if not user_script.endswith("\n"):
user_script += "\n"
return "RUN <<'EOF'\n" + user_script + "EOF\n"
# Add step start and complete messages
wrapped_script = "echo '[agentbox] Step 7: configure user'\n"
wrapped_script += user_script
wrapped_script += "echo '[agentbox] Step 7 complete'\n"
return "RUN <<'EOF'\n" + wrapped_script + "EOF\n"
@@ -59,4 +68,4 @@ def render_dockerfile(template_path: Path, output_path: Path, context: dict) ->
)
template = env.get_template(template_path.name)
content = template.render(**context)
output_path.write_text(content, encoding="utf-8")
output_path.write_text(content, encoding="utf-8")
-4
View File
@@ -1,8 +1,6 @@
#!/bin/bash
set -ex
echo "[agentbox] Step 1: install system packages"
# Install all system packages
apt-get update -qq && apt-get install -y -qq --no-install-recommends \
{% for pkg in system_packages.essential -%}
@@ -25,5 +23,3 @@ apt-get update -qq && apt-get install -y -qq --no-install-recommends \
apt-get clean
rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
echo "[agentbox] Step 1 complete"
-4
View File
@@ -1,8 +1,6 @@
#!/bin/bash
set -ex
echo "[agentbox] Step 2: install runtimes (miniconda/node/go/rust)"
# Detect architecture (x86_64 or aarch64)
ARCH=$(uname -m)
case "$ARCH" in
@@ -41,5 +39,3 @@ curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y --defaul
# Cleanup
apt-get clean
rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
echo "[agentbox] Step 2 complete"
-4
View File
@@ -1,8 +1,6 @@
#!/bin/bash
set -ex
echo "[agentbox] Step 3: install Python env and packages"
# Create base environment with Python
/opt/conda/bin/conda install -y -q -n base -c conda-forge \
python={{ languages.python.version }} \
@@ -21,5 +19,3 @@ echo "[agentbox] Step 3: install Python env and packages"
# Cleanup
/opt/conda/bin/conda clean --all -y
/opt/conda/bin/pip cache purge || true
echo "[agentbox] Step 3 complete"
-4
View File
@@ -1,8 +1,6 @@
#!/bin/bash
set -ex
echo "[agentbox] Step 4: install Node.js globals"
# Install Node.js global packages
npm install -g \
{% for pkg in nodejs_packages -%}
@@ -10,5 +8,3 @@ npm install -g \
{% endfor %}
npm cache clean --force
echo "[agentbox] Step 4 complete"
-4
View File
@@ -1,8 +1,6 @@
#!/bin/bash
set -ex
echo "[agentbox] Step 5: create non-root user"
# Remove conflicting UID/GID if exists
if getent passwd {{ user.uid }} >/dev/null 2>&1; then
userdel -r $(getent passwd {{ user.uid }} | cut -d: -f1)
@@ -30,5 +28,3 @@ chown -R {{ user.name }}:{{ user.name }} /home/{{ user.name }}/.rustup
# Create workspace
mkdir -p {{ workdir }}
chown -R {{ user.name }}:{{ user.name }} {{ workdir }}
echo "[agentbox] Step 5 complete"
-4
View File
@@ -1,8 +1,6 @@
#!/bin/bash
set -ex
echo "[agentbox] Step 6: configure root + Playwright"
# Install Playwright system dependencies
apt-get update
apt-get install -y --no-install-recommends \
@@ -56,5 +54,3 @@ echo 'conda activate base 2>/dev/null || true' >> /root/.bashrc
# Clean up system packages
apt-get clean
rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
echo "[agentbox] Step 6 complete"
-4
View File
@@ -1,8 +1,6 @@
#!/bin/bash
set -ex
echo "[agentbox] Step 7: configure user shell"
# Initialize micromamba for user
# Initialize conda for user
/opt/conda/bin/conda init bash
@@ -14,5 +12,3 @@ echo 'export GOPATH=/home/{{ user.name }}/go' >> /home/{{ user.name }}/.bashrc
echo 'export GOBIN=/home/{{ user.name }}/go/bin' >> /home/{{ user.name }}/.bashrc
echo '. /opt/conda/etc/profile.d/conda.sh' >> /home/{{ user.name }}/.bashrc
echo 'conda activate base 2>/dev/null || true' >> /home/{{ user.name }}/.bashrc
echo "[agentbox] Step 7 complete"