Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion deploy/bind-cli.sh
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,7 @@ if [[ "$UNINSTALL" == "1" ]]; then
if command -v docker >/dev/null 2>&1; then
while IFS= read -r container; do
[[ -n "$container" ]] || continue
docker rm -f "$container" >/dev/null 2>&1 || true
docker rm -f -v "$container" >/dev/null 2>&1 || true
done < <(docker ps -aq --filter 'name=^/agentflow-rt-' 2>/dev/null || true)
while IFS= read -r image; do
[[ -n "$image" ]] || continue
Expand Down
2 changes: 1 addition & 1 deletion internal/runner/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -688,7 +688,7 @@ func cleanupDockerWorkspace(ctx context.Context, workspace string) error {
}
imageOut, _ := exec.CommandContext(ctx, "docker", "inspect", "-f", "{{.Config.Image}}", id).Output()
image := strings.TrimSpace(string(imageOut))
if removeOut, removeErr := exec.CommandContext(ctx, "docker", "rm", "-f", id).CombinedOutput(); removeErr != nil {
if removeOut, removeErr := exec.CommandContext(ctx, "docker", "rm", "-f", "-v", id).CombinedOutput(); removeErr != nil {
if dockerObjectGone(string(removeOut), removeErr) {
continue
}
Expand Down
6 changes: 3 additions & 3 deletions internal/runner/daemon_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1068,7 +1068,7 @@ inspect)
*) echo "unexpected $last" >&2; exit 1 ;;
esac
;;
rm) printf '%s\n' "$last" >> "$AF_TEST_RM_LOG" ;;
rm) printf '%s\n' "$*" >> "$AF_TEST_RM_LOG" ;;
*) echo "unexpected $1" >&2; exit 1 ;;
esac
`)
Expand All @@ -1081,8 +1081,8 @@ esac
if err != nil {
t.Fatal(err)
}
if strings.TrimSpace(string(removed)) != "keep" {
t.Fatalf("removed = %q, want keep", removed)
if strings.TrimSpace(string(removed)) != "rm -f -v keep" {
t.Fatalf("Docker removal = %q, want only the in-run container and its anonymous volumes", removed)
}
}

Expand Down
23 changes: 22 additions & 1 deletion internal/runtime/docker_cases_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ case "$*" in
printf '[{"Destination":"/workspace","Source":"%s"}]\n' "$CVR_DOCKER_WORKSPACE"; exit 0 ;;
"start box") [ -n "$(status)" ] || exit 1; echo running > "$state/status"; count starts; exit 0 ;;
"create --name box "*) echo exited > "$state/status"; count generation; exit 0 ;;
"rm -f box") rm -f "$state/status"; exit 0 ;;
"rm -f -v box") rm -f "$state/status"; exit 0 ;;
"exec box "*) [ "$(status)" = running ] || { echo 'Error response from daemon: container box is not running' >&2; exit 1; }
case "$*" in *"cd /workspace && pwd"*) echo /workspace ;; esac; exit 0 ;;
"image inspect --format {{.Config.User}} "*) exit 0 ;;
Expand Down Expand Up @@ -404,3 +404,24 @@ type cvRTextError struct {

func (e *cvRTextError) Error() string { return e.text + ": " + e.base.Error() }
func (e *cvRTextError) Unwrap() error { return e.base }

func TestRemoveDockerContainerReclaimsAnonymousVolumes(t *testing.T) {
bin := t.TempDir()
argsPath := filepath.Join(bin, "docker-args")
if err := os.WriteFile(filepath.Join(bin, "docker"), []byte("#!/bin/sh\nprintf '%s\\n' \"$@\" > \"$LOOPFORGE_TEST_DOCKER_ARGS\"\n"), 0o700); err != nil {
t.Fatal(err)
}
t.Setenv("PATH", bin+string(os.PathListSeparator)+os.Getenv("PATH"))
t.Setenv("LOOPFORGE_TEST_DOCKER_ARGS", argsPath)

if err := removeDockerContainer(context.Background(), "agentflow-rt-test"); err != nil {
t.Fatal(err)
}
got, err := os.ReadFile(argsPath)
if err != nil {
t.Fatal(err)
}
if want := "rm\n-f\n-v\nagentflow-rt-test\n"; string(got) != want {
t.Fatalf("docker arguments = %q, want %q", got, want)
}
}
8 changes: 5 additions & 3 deletions internal/runtime/sandbox_docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,9 @@ func removeDockerWorkspaceContainers(ctx context.Context, container string) erro
}

func removeDockerContainer(ctx context.Context, name string) error {
out, err := exec.CommandContext(ctx, "docker", "rm", "-f", name).CombinedOutput()
// Docker allocates anonymous volumes for Dockerfile VOLUME declarations.
// Remove those with their container so old sandboxes do not leave them behind.
out, err := exec.CommandContext(ctx, "docker", "rm", "-f", "-v", name).CombinedOutput()
if err != nil && !dockerObjectGone(string(out), err) {
return fmt.Errorf("remove Docker container %s: %w: %s", name, err, strings.TrimSpace(string(out)))
}
Expand Down Expand Up @@ -821,13 +823,13 @@ func (s *dockerSandbox) start(ctx context.Context, recreate bool) error {
return fmt.Errorf("docker create: %w: %s", err, out)
}
if out, err := exec.CommandContext(ctx, "docker", "start", name).CombinedOutput(); err != nil {
_ = exec.CommandContext(ctx, "docker", "rm", "-f", name).Run()
_ = removeDockerContainer(ctx, name)
dropEphemeralDockerImage(s.builtImage)
s.builtImage = ""
return fmt.Errorf("docker start: %w: %s", err, out)
}
if err := waitDockerWorkspaceReady(ctx, name); err != nil {
_ = exec.Command("docker", "rm", "-f", name).Run()
_ = removeDockerContainer(context.Background(), name)
s.started = false
return err
}
Expand Down