diff --git a/server/utils/builders/docker-file.ts b/server/utils/builders/docker-file.ts index d8ead600..46aa44e5 100644 --- a/server/utils/builders/docker-file.ts +++ b/server/utils/builders/docker-file.ts @@ -1,9 +1,8 @@ import type { WriteStream } from "node:fs"; -import { docker } from "@/server/constants"; -import { prepareBuildArgs } from "@/server/utils/docker/utils"; -import * as tar from "tar-fs"; +import { prepareEnvironmentVariables } from "@/server/utils/docker/utils"; import type { ApplicationNested } from "."; import { getBuildAppDirectory } from "../filesystem/directory"; +import { spawnAsync } from "../process/spawnAsync"; import { createEnvFile } from "./utils"; export const buildCustomDocker = async ( @@ -14,29 +13,30 @@ export const buildCustomDocker = async ( const dockerFilePath = getBuildAppDirectory(application); try { const image = `${appName}`; + const contextPath = dockerFilePath.substring(0, dockerFilePath.lastIndexOf("/") + 1) || "."; - const tarStream = tar.pack(contextPath); + const args = prepareEnvironmentVariables(buildArgs); + + const commandArgs = ["build", "-t", image, "-f", dockerFilePath, "."]; + + for (const arg of args) { + commandArgs.push("--build-arg", arg); + } createEnvFile(dockerFilePath, env); - - const stream = await docker.buildImage(tarStream, { - t: image, - buildargs: prepareBuildArgs(buildArgs), - dockerfile: dockerFilePath.substring(dockerFilePath.lastIndexOf("/") + 1), - }); - - await new Promise((resolve, reject) => { - docker.modem.followProgress( - stream, - (err, res) => (err ? reject(err) : resolve(res)), - (event) => { - if (event.stream) { - writeStream.write(event.stream); - } - }, - ); - }); + await spawnAsync( + "docker", + commandArgs, + (data) => { + if (writeStream.writable) { + writeStream.write(data); + } + }, + { + cwd: contextPath, + }, + ); } catch (error) { throw error; }