Skip to content

Modern Docker Management, Designed for Everyone

refactor: add image pull popover when pulling projects

Kyle Mendell2edc661020bc
diff --git a/backend/internal/api/stack_handler.go b/backend/internal/api/stack_handler.go
index 01eddf215..d4a96c02a 100644
--- a/backend/internal/api/stack_handler.go
+++ b/backend/internal/api/stack_handler.go
@@ -476,11 +476,30 @@ func (h *StackHandler) DestroyStack(c *gin.Context) {
func (h *StackHandler) PullStack(c *gin.Context) {
stackID := c.Param("stackId")
+ if stackID == "" {
+ stackID = c.Param("id")
+ }
+
+ // Stream headers, same as image pull
+ c.Writer.Header().Set("Content-Type", "application/x-json-stream")
+ c.Writer.Header().Set("Cache-Control", "no-cache")
+ c.Writer.Header().Set("Connection", "keep-alive")
+ c.Writer.Header().Set("X-Accel-Buffering", "no")
+
+ currentUser, exists := middleware.GetCurrentUser(c)
+ if !exists || currentUser == nil {
+ c.JSON(http.StatusUnauthorized, gin.H{"success": false, "error": "User not authenticated"})
+ return
+ }
+
+ if err := h.stackService.PullStackImages(c.Request.Context(), stackID, c.Writer); err != nil {
+ c.JSON(http.StatusInternalServerError, gin.H{
+ "success": false,
+ "error": fmt.Sprintf("Failed to pull stack images: %v", err),
+ })
+ return
+ }
- c.JSON(http.StatusOK, gin.H{
- "success": true,
- "data": gin.H{"message": "Stack images pulled successfully", "stackId": stackID},
- })
}
func (h *StackHandler) DeployStack(c *gin.Context) {
@@ -549,7 +568,18 @@ func (h *StackHandler) PullImages(c *gin.Context) {
stackID = c.Param("id")
}
- if err := h.stackService.PullStackImages(c.Request.Context(), stackID); err != nil {
+ c.Writer.Header().Set("Content-Type", "application/x-json-stream")
+ c.Writer.Header().Set("Cache-Control", "no-cache")
+ c.Writer.Header().Set("Connection", "keep-alive")
+ c.Writer.Header().Set("X-Accel-Buffering", "no")
+
+ currentUser, exists := middleware.GetCurrentUser(c)
+ if !exists || currentUser == nil {
+ c.JSON(http.StatusUnauthorized, gin.H{"success": false, "error": "User not authenticated"})
+ return
+ }
+
+ if err := h.stackService.PullStackImages(c.Request.Context(), stackID, c.Writer); err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"success": false,
"error": err.Error(),
@@ -557,10 +587,7 @@ func (h *StackHandler) PullImages(c *gin.Context) {
return
}
- c.JSON(http.StatusOK, gin.H{
- "success": true,
- "data": gin.H{"message": "Images pulled successfully"},
- })
+ // Do not send a final JSON body; stream completes like the image pull endpoint.
}
func (h *StackHandler) ConvertDockerRun(c *gin.Context) {
diff --git a/backend/internal/bootstrap/services_bootstrap.go b/backend/internal/bootstrap/services_bootstrap.go
index 213a167c7..4c3e3d279 100644
--- a/backend/internal/bootstrap/services_bootstrap.go
+++ b/backend/internal/bootstrap/services_bootstrap.go
@@ -16,12 +16,12 @@ func initializeServices(db *database.DB, cfg *config.Config) (*api.Services, *se
settingsService := services.NewSettingsService(db, cfg)
dockerClientService := services.NewDockerClientService(db)
userService := services.NewUserService(db)
- stackService := services.NewStackService(db, settingsService, eventService)
- environmentService := services.NewEnvironmentService(db)
- containerService := services.NewContainerService(db, eventService, dockerClientService)
containerRegistry := services.NewContainerRegistryService(db)
imageUpdate := services.NewImageUpdateService(db, settingsService, containerRegistry, dockerClientService, eventService)
imageService := services.NewImageService(db, dockerClientService, containerRegistry, imageUpdate, eventService)
+ stackService := services.NewStackService(db, settingsService, eventService, imageService)
+ environmentService := services.NewEnvironmentService(db)
+ containerService := services.NewContainerService(db, eventService, dockerClientService)
volumeService := services.NewVolumeService(db, dockerClientService, eventService)
networkService := services.NewNetworkService(db, dockerClientService, eventService)
templateService := services.NewTemplateService(db)
diff --git a/backend/internal/services/auto_update_service.go b/backend/internal/services/auto_update_service.go
index e64abdd5c..37ba13a2b 100644
--- a/backend/internal/services/auto_update_service.go
+++ b/backend/internal/services/auto_update_service.go
@@ -59,7 +59,7 @@ func NewAutoUpdateService(
imageService: imageService,
imageUpdateService: imageUpdateService,
registryService: registryService,
- eventService: eventService, // Add this line
+ eventService: eventService,
updatingContainers: make(map[string]bool),
updatingStacks: make(map[string]bool),
}
@@ -73,7 +73,6 @@ func (s *AutoUpdateService) CheckForUpdates(ctx context.Context, req dto.AutoUpd
Results: []dto.AutoUpdateResourceResult{},
}
- // Log auto-update check start event
metadata := models.JSON{
"action": "check_start",
"type": req.Type,
@@ -821,7 +820,6 @@ func (s *AutoUpdateService) updateStack(
) error {
log.Printf("Updating stack: %s", stack.Name)
- // Log stack update start event
updateMetadata := models.JSON{
"action": "auto_update_start",
"reason": "image_updates_available",
@@ -831,7 +829,7 @@ func (s *AutoUpdateService) updateStack(
}
log.Printf("Pulling latest images for stack: %s", stack.Name)
- if err := s.stackService.PullStackImages(ctx, stack.ID); err != nil {
+ if err := s.stackService.PullStackImages(ctx, stack.ID, io.Discard); err != nil {
log.Printf("Warning: Failed to pull some images: %v", err)
// Log image pull warning
pullWarningMetadata := models.JSON{
diff --git a/backend/internal/services/stack_service.go b/backend/internal/services/stack_service.go
index 987094e1b..fa1552f21 100644
--- a/backend/internal/services/stack_service.go
+++ b/backend/internal/services/stack_service.go
@@ -8,6 +8,7 @@ import (
"errors"
"fmt"
"io"
+ "net/http"
"os"
"os/exec"
"path/filepath"
@@ -34,13 +35,15 @@ type StackService struct {
db *database.DB
settingsService *SettingsService
eventService *EventService
+ imageService *ImageService
}
-func NewStackService(db *database.DB, settingsService *SettingsService, eventService *EventService) *StackService {
+func NewStackService(db *database.DB, settingsService *SettingsService, eventService *EventService, imageService *ImageService) *StackService {
return &StackService{
db: db,
settingsService: settingsService,
eventService: eventService,
+ imageService: imageService,
}
}
@@ -348,24 +351,168 @@ func (s *StackService) RestartStack(ctx context.Context, stackID string, user mo
return s.updateStackStatusAndCounts(ctx, stackID, models.StackStatusRunning)
}
-func (s *StackService) PullStackImages(ctx context.Context, stackID string) error {
+func (s *StackService) PullStackImages(ctx context.Context, stackID string, progressWriter io.Writer) error {
stack, err := s.GetStackByID(ctx, stackID)
if err != nil {
return err
}
+ composeFile := s.findComposeFile(stack.Path)
+ if composeFile == "" {
+ return fmt.Errorf("no compose file found for stack")
+ }
+
+ servicesFromFile, err := s.parseServicesFromComposeFile(ctx, composeFile, stack.Name)
+ if err != nil {
+ return fmt.Errorf("failed to parse services from compose file: %w", err)
+ }
+
+ // Build a unique image list
+ seen := map[string]struct{}{}
+ var images []string
+ for _, svc := range servicesFromFile {
+ img := strings.TrimSpace(svc.Image)
+ if img == "" {
+ continue
+ }
+ if _, ok := seen[img]; ok {
+ continue
+ }
+ seen[img] = struct{}{}
+ images = append(images, img)
+ }
+
+ // Stream helper
+ flusher, ok := progressWriter.(http.Flusher)
+ writeJSON := func(m map[string]any) {
+ b, _ := json.Marshal(m)
+ _, _ = progressWriter.Write(b)
+ _, _ = progressWriter.Write([]byte("\n"))
+ if ok {
+ flusher.Flush()
+ }
+ }
+
+ if s.imageService != nil && len(images) > 0 {
+ var firstErr error
+
+ writeJSON(map[string]any{
+ "status": "Pulling images for stack",
+ "id": stack.Name,
+ })
+
+ for _, img := range images {
+ // optional delimiter line per image
+ writeJSON(map[string]any{"status": "Pulling", "id": img})
+
+ if err := s.imageService.PullImage(ctx, img, progressWriter, systemUser); err != nil {
+ writeJSON(map[string]any{
+ "error": err.Error(),
+ "status": "error",
+ "id": img,
+ "stackId": stackID,
+ })
+ if firstErr == nil {
+ firstErr = err
+ }
+ // continue pulling the rest
+ continue
+ }
+
+ writeJSON(map[string]any{
+ "status": "Pull complete",
+ "id": img,
+ "progressDetail": map[string]any{"hidecounts": true},
+ })
+ }
+
+ writeJSON(map[string]any{"status": "Done", "id": stack.Name})
+ return firstErr
+ }
+
+ // Fallback to docker-compose pull (legacy), stream as plain lines
cmd := exec.CommandContext(ctx, "docker-compose", "pull")
cmd.Dir = stack.Path
cmd.Env = append(os.Environ(),
fmt.Sprintf("COMPOSE_PROJECT_NAME=%s", stack.Name),
)
- output, err := cmd.CombinedOutput()
+ stdout, err := cmd.StdoutPipe()
+ if err != nil {
+ return fmt.Errorf("failed to create stdout pipe: %w", err)
+ }
+ stderr, err := cmd.StderrPipe()
if err != nil {
- return fmt.Errorf("failed to pull images: %w\nOutput: %s", err, string(output))
+ return fmt.Errorf("failed to create stderr pipe: %w", err)
}
- return nil
+ if err := cmd.Start(); err != nil {
+ return fmt.Errorf("failed to start docker-compose pull: %w", err)
+ }
+
+ writeLine := func(line string) error {
+ if _, werr := progressWriter.Write([]byte(line)); werr != nil {
+ return werr
+ }
+ if _, werr := progressWriter.Write([]byte("\n")); werr != nil {
+ return werr
+ }
+ if ok {
+ flusher.Flush()
+ }
+ return nil
+ }
+
+ errCh := make(chan error, 3)
+
+ go func() {
+ sc := bufio.NewScanner(stdout)
+ sc.Buffer(make([]byte, 0, 64*1024), 1024*1024)
+ for sc.Scan() {
+ if ctx.Err() != nil {
+ errCh <- ctx.Err()
+ return
+ }
+ if werr := writeLine(sc.Text()); werr != nil {
+ errCh <- fmt.Errorf("error writing stdout: %w", werr)
+ return
+ }
+ }
+ errCh <- sc.Err()
+ }()
+
+ go func() {
+ sc := bufio.NewScanner(stderr)
+ sc.Buffer(make([]byte, 0, 64*1024), 1024*1024)
+ for sc.Scan() {
+ if ctx.Err() != nil {
+ errCh <- ctx.Err()
+ return
+ }
+ if werr := writeLine(sc.Text()); werr != nil {
+ errCh <- fmt.Errorf("error writing stderr: %w", werr)
+ return
+ }
+ }
+ errCh <- sc.Err()
+ }()
+
+ go func() {
+ errCh <- cmd.Wait()
+ }()
+
+ var firstErr error
+ for i := 0; i < 3; i++ {
+ if e := <-errCh; e != nil && firstErr == nil && !errors.Is(e, io.EOF) && !errors.Is(e, context.Canceled) {
+ firstErr = e
+ }
+ }
+
+ if ctx.Err() != nil && cmd.Process != nil {
+ _ = cmd.Process.Kill()
+ }
+
+ return firstErr
}
func (s *StackService) ListStacks(ctx context.Context) ([]models.Stack, error) {
@@ -754,7 +901,7 @@ func (s *StackService) RedeployStack(ctx context.Context, stackID string, profil
return err
}
- if err := s.PullStackImages(ctx, stackID); err != nil {
+ if err := s.PullStackImages(ctx, stackID, io.Discard); err != nil {
fmt.Printf("Warning: failed to pull images: %v\n", err)
}
diff --git a/frontend/src/lib/components/action-buttons.svelte b/frontend/src/lib/components/action-buttons.svelte
index dd86abe8f..7c767230e 100644
--- a/frontend/src/lib/components/action-buttons.svelte
+++ b/frontend/src/lib/components/action-buttons.svelte
@@ -6,6 +6,11 @@
import { tryCatch } from '$lib/utils/try-catch';
import { handleApiResultWithCallbacks } from '$lib/utils/api.util';
import { ArcaneButton } from '$lib/components/arcane-button/index.js';
+ import * as Popover from '$lib/components/ui/popover/index.js';
+ import { Progress } from '$lib/components/ui/progress/index.js';
+ import DownloadIcon from '@lucide/svelte/icons/download';
+ import { environmentStore, LOCAL_DOCKER_ENVIRONMENT_ID } from '$lib/stores/environment.store';
+ import { get } from 'svelte/store';
type TargetType = 'container' | 'stack';
@@ -44,6 +49,12 @@
validating: false
});
+ let pullPopoverOpen = $state(false);
+ let pullProgress = $state(0);
+ let pullStatusText = $state('');
+ let pullError = $state('');
+ let layerProgress = $state<Record<string, { current: number; total: number; status: string }>>({});
+
const isRunning = $derived(itemState === 'running' || (type === 'stack' && itemState === 'partially running'));
$effect(() => {
@@ -56,6 +67,52 @@
isLoading.validating = loading.validating ?? false;
});
+ function resetPullState() {
+ pullProgress = 0;
+ pullStatusText = '';
+ pullError = '';
+ layerProgress = {};
+ }
+
+ function calculateOverallProgress() {
+ let totalCurrentBytes = 0;
+ let totalExpectedBytes = 0;
+ let activeLayers = 0;
+
+ for (const id in layerProgress) {
+ const layer = layerProgress[id];
+ if (layer.total > 0) {
+ totalCurrentBytes += layer.current;
+ totalExpectedBytes += layer.total;
+ activeLayers++;
+ }
+ }
+
+ if (totalExpectedBytes > 0) {
+ pullProgress = (totalCurrentBytes / totalExpectedBytes) * 100;
+ } else if (activeLayers > 0 && totalCurrentBytes > 0) {
+ pullProgress = 5;
+ } else if (Object.keys(layerProgress).length > 0 && activeLayers === 0) {
+ const allDone = Object.values(layerProgress).every(
+ (l) => l.status && (l.status.toLowerCase().includes('pull complete') || l.status.toLowerCase().includes('already exists'))
+ );
+ if (allDone) pullProgress = 100;
+ }
+ }
+
+ function buildPullApiUrl(): string {
+ const envId = getCurrentEnvironmentId();
+ if (envId === LOCAL_DOCKER_ENVIRONMENT_ID) {
+ return `/api/stacks/${id}/pull`;
+ }
+ return `/api/environments/${envId}/stacks/${id}/pull`;
+ }
+
+ function getCurrentEnvironmentId(): string {
+ const env = get(environmentStore.selected);
+ return env?.id || LOCAL_DOCKER_ENVIRONMENT_ID;
+ }
+
function confirmAction(action: string) {
if (action === 'remove') {
openConfirmDialog({
@@ -172,16 +229,144 @@
}
async function handlePull() {
+ if (type === 'container') {
+ // Use existing API for containers
+ isLoading.pulling = true;
+ handleApiResultWithCallbacks({
+ result: await tryCatch(environmentAPI.pullContainerImage(id)),
+ message: 'Failed to Pull Image(s)',
+ setLoadingState: (value) => (isLoading.pulling = value),
+ onSuccess: async () => {
+ toast.success('Image(s) Pulled Successfully.');
+ await invalidateAll();
+ }
+ });
+ } else {
+ await handleStackPull();
+ }
+ }
+
+ async function handleStackPull() {
+ resetPullState();
isLoading.pulling = true;
- handleApiResultWithCallbacks({
- result: await tryCatch(type === 'container' ? environmentAPI.pullContainerImage(id) : environmentAPI.pullProjectImages(id)),
- message: 'Failed to Pull Image(s)',
- setLoadingState: (value) => (isLoading.pulling = value),
- onSuccess: async () => {
- toast.success('Image(s) Pulled Successfully.');
- await invalidateAll();
+ pullPopoverOpen = true;
+ pullStatusText = 'Initiating pull...';
+
+ let wasSuccessful = false;
+
+ try {
+ const response = await fetch(buildPullApiUrl(), {
+ method: 'POST',
+ headers: {
+ 'Content-Type': 'application/json'
+ }
+ });
+
+ if (!response.ok || !response.body) {
+ const errorData = await response.json().catch(() => ({
+ error: 'Failed to pull images. Server returned an error.'
+ }));
+ const errorMessage =
+ typeof errorData.error === 'string' ? errorData.error : errorData.message || `HTTP error ${response.status}`;
+ throw new Error(errorMessage);
}
- });
+
+ const reader = response.body.getReader();
+ const decoder = new TextDecoder();
+ let buffer = '';
+
+ while (true) {
+ const { done, value } = await reader.read();
+ if (done) {
+ pullStatusText = 'Processing final layers...';
+ break;
+ }
+
+ buffer += decoder.decode(value, { stream: true });
+ const lines = buffer.split('\n');
+ buffer = lines.pop() || '';
+
+ for (const line of lines) {
+ if (line.trim() === '') continue;
+ try {
+ const data = JSON.parse(line);
+
+ if (data.error) {
+ console.error('Error in stream:', data.error);
+ pullError = typeof data.error === 'string' ? data.error : data.error.message || 'An error occurred during pull.';
+ pullStatusText = `Error: ${pullError}`;
+ continue;
+ }
+
+ pullStatusText = data.status || pullStatusText;
+ if (data.id) {
+ const currentLayer = layerProgress[data.id] || { current: 0, total: 0, status: '' };
+ currentLayer.status = data.status || currentLayer.status;
+
+ if (data.progressDetail) {
+ currentLayer.current = data.progressDetail.current || currentLayer.current;
+ currentLayer.total = data.progressDetail.total || currentLayer.total;
+ }
+ layerProgress[data.id] = currentLayer;
+ }
+ calculateOverallProgress();
+ } catch (e: any) {
+ console.warn('Failed to parse stream line or process data:', line, e);
+ }
+ }
+ }
+
+ calculateOverallProgress();
+ if (!pullError && pullProgress < 100) {
+ const allLayersCompleteOrExisting = Object.values(layerProgress).every(
+ (l) =>
+ l.status &&
+ (l.status.toLowerCase().includes('complete') ||
+ l.status.toLowerCase().includes('already exists') ||
+ l.status.toLowerCase().includes('downloaded newer image'))
+ );
+ if (allLayersCompleteOrExisting && Object.keys(layerProgress).length > 0) {
+ pullProgress = 100;
+ }
+ }
+
+ if (pullError) {
+ throw new Error(pullError);
+ }
+
+ wasSuccessful = true;
+ pullProgress = 100;
+ pullStatusText = 'Images pulled successfully.';
+ toast.success('Images pulled successfully!');
+ await invalidateAll();
+
+ setTimeout(() => {
+ pullPopoverOpen = false;
+ isLoading.pulling = false;
+ resetPullState();
+ }, 2000);
+ } catch (error: any) {
+ console.error('Pull images error:', error);
+ const message = error.message || 'An unexpected error occurred while pulling images.';
+ pullError = message;
+ pullStatusText = `Failed: ${message}`;
+ toast.error(message);
+ } finally {
+ if (!wasSuccessful) {
+ isLoading.pulling = false;
+ }
+ }
+ }
+
+ function handlePopoverOpenChange(newOpenState: boolean) {
+ if (!newOpenState && isLoading.pulling) {
+ pullPopoverOpen = true;
+ return;
+ }
+ pullPopoverOpen = newOpenState;
+ if (newOpenState && !isLoading.pulling) {
+ resetPullState();
+ }
}
</script>
@@ -206,7 +391,53 @@
<ArcaneButton action="remove" onclick={() => confirmAction('remove')} loading={isLoading.remove} />
{:else}
<ArcaneButton action="redeploy" onclick={() => confirmAction('redeploy')} loading={isLoading.redeploy} />
- <ArcaneButton action="pull" onclick={handlePull} loading={isLoading.pulling} />
+
+ {#if type === 'stack'}
+ <Popover.Root bind:open={pullPopoverOpen} onOpenChange={handlePopoverOpenChange}>
+ <Popover.Trigger>
+ <ArcaneButton action="pull" onclick={handlePull} loading={isLoading.pulling} />
+ </Popover.Trigger>
+ <Popover.Content class="w-80 p-4" align="center">
+ <div class="space-y-3">
+ <div class="flex items-center gap-3">
+ <div class="bg-primary/10 flex size-8 shrink-0 items-center justify-center rounded-full">
+ <DownloadIcon class="text-primary size-4" />
+ </div>
+ <div>
+ <h4 class="text-sm font-semibold">Pulling Images</h4>
+ <p class="text-muted-foreground text-xs">Downloading latest versions</p>
+ </div>
+ </div>
+
+ {#if pullError}
+ <div class="rounded-md bg-red-50 p-3 dark:bg-red-950/20">
+ <p class="text-destructive text-sm">{pullError}</p>
+ </div>
+ {:else}
+ <div class="space-y-2">
+ <div class="flex justify-between text-xs">
+ <span class="text-muted-foreground truncate pr-2">{pullStatusText || 'Preparing...'}</span>
+ <span class="text-muted-foreground shrink-0">{Math.round(pullProgress)}%</span>
+ </div>
+ <Progress value={pullProgress} max={100} class="h-2 w-full" />
+ {#if isLoading.pulling}
+ <p class="text-muted-foreground text-xs">This may take a while depending on image sizes and your connection.</p>
+ {/if}
+ </div>
+ {/if}
+
+ {#if !isLoading.pulling && pullProgress === 100 && !pullError}
+ <div class="rounded-md bg-green-50 p-3 dark:bg-green-950/20">
+ <p class="text-sm text-green-600 dark:text-green-400">✓ All images pulled successfully!</p>
+ </div>
+ {/if}
+ </div>
+ </Popover.Content>
+ </Popover.Root>
+ {:else}
+ <ArcaneButton action="pull" onclick={handlePull} loading={isLoading.pulling} />
+ {/if}
+
<ArcaneButton
customLabel={type === 'stack' ? 'Destroy' : 'Remove'}
action="remove"