From e22d5031824e7b666d0d8f8e5d712491794ae1d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D1=84=D1=8B=D1=80=D0=B0=D1=82=20=D1=91=D0=B7=D0=B4=D1=8D?= =?UTF-8?q?=D0=BD?= <31664778+fir4tozden@users.noreply.github.com> Date: Sun, 30 Nov 2025 21:24:23 +0300 Subject: [PATCH] feat: waiting for the command to run during build and pull --- packages/server/src/utils/docker/utils.ts | 48 +++++++++++++++++------ 1 file changed, 35 insertions(+), 13 deletions(-) diff --git a/packages/server/src/utils/docker/utils.ts b/packages/server/src/utils/docker/utils.ts index 4258cfbbe..e68080e1a 100644 --- a/packages/server/src/utils/docker/utils.ts +++ b/packages/server/src/utils/docker/utils.ts @@ -144,13 +144,35 @@ export const getContainerByName = (name: string): Promise => { }); }); }; + +// Commands passed through this method are held during Docker's build or pull process. (https://github.com/dokploy/dokploy/pull/3064) +export const createDockerSafeExec = (command: string) => (`CHECK_INTERVAL=10 + +echo "Starting Docker cleanup..." + +while true; do + PROCESSES=$(ps aux | grep -E "docker build|docker pull" | grep -v grep) + + if [ -z "$PROCESSES" ]; then + echo "Docker is idle. Starting cleanup..." + break + else + echo "Docker is busy. Will check again in $CHECK_INTERVAL seconds..." + sleep $CHECK_INTERVAL + fi +done + +${command} + +echo "Docker cleanup completed."`) + export const cleanUpUnusedImages = async (serverId?: string) => { try { - const command = "docker image prune --force"; + const command = "docker image prune --all --force"; if (serverId) { - await execAsyncRemote(serverId, command); + await execAsyncRemote(serverId, createDockerSafeExec(command)); } else { - await execAsync(command); + await execAsync(createDockerSafeExec(command)); } } catch (error) { console.error(error); @@ -162,9 +184,9 @@ export const cleanStoppedContainers = async (serverId?: string) => { try { const command = "docker container prune --force"; if (serverId) { - await execAsyncRemote(serverId, command); + await execAsyncRemote(serverId, createDockerSafeExec(command)); } else { - await execAsync(command); + await execAsync(createDockerSafeExec(command)); } } catch (error) { console.error(error); @@ -174,11 +196,11 @@ export const cleanStoppedContainers = async (serverId?: string) => { export const cleanUpUnusedVolumes = async (serverId?: string) => { try { - const command = "docker volume prune --force"; + const command = "docker volume prune --all --force"; if (serverId) { - await execAsyncRemote(serverId, command); + await execAsyncRemote(serverId, createDockerSafeExec(command)); } else { - await execAsync(command); + await execAsync(createDockerSafeExec(command)); } } catch (error) { console.error(error); @@ -206,18 +228,18 @@ export const cleanUpInactiveContainers = async () => { export const cleanUpDockerBuilder = async (serverId?: string) => { const command = "docker builder prune --all --force"; if (serverId) { - await execAsyncRemote(serverId, command); + await execAsyncRemote(serverId, createDockerSafeExec(command)); } else { - await execAsync(command); + await execAsync(createDockerSafeExec(command)); } }; export const cleanUpSystemPrune = async (serverId?: string) => { - const command = "docker system prune --force --volumes"; + const command = "docker system prune --all --volumes --force"; if (serverId) { - await execAsyncRemote(serverId, command); + await execAsyncRemote(serverId, createDockerSafeExec(command)); } else { - await execAsync(command); + await execAsync(createDockerSafeExec(command)); } };