Merge pull request #310 from Dokploy/fix/docker-context

fix(docker-context): add docker context path #284
This commit is contained in:
Mauricio Siu 2024-08-02 09:52:27 -06:00 committed by GitHub
commit 7245e7dfd7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
10 changed files with 3101 additions and 26 deletions

View File

@ -55,6 +55,7 @@ const baseApp: ApplicationNested = {
title: null,
updateConfigSwarm: null,
username: null,
dockerContextPath: null,
};
const baseDomain: Domain = {

View File

@ -36,6 +36,7 @@ const mySchema = z.discriminatedUnion("buildType", [
invalid_type_error: "Dockerfile path is required",
})
.min(1, "Dockerfile required"),
dockerContextPath: z.string().nullable(),
}),
z.object({
buildType: z.literal("heroku_buildpacks"),
@ -79,12 +80,12 @@ export const ShowBuildChooseForm = ({ applicationId }: Props) => {
const buildType = form.watch("buildType");
useEffect(() => {
if (data) {
// TODO: refactor this
if (data.buildType === "dockerfile") {
form.reset({
buildType: data.buildType,
...(data.buildType && {
dockerfile: data.dockerfile || "",
dockerContextPath: data.dockerContextPath || "",
}),
});
} else {
@ -103,6 +104,8 @@ export const ShowBuildChooseForm = ({ applicationId }: Props) => {
publishDirectory:
data.buildType === "nixpacks" ? data.publishDirectory : null,
dockerfile: data.buildType === "dockerfile" ? data.dockerfile : null,
dockerContextPath:
data.buildType === "dockerfile" ? data.dockerContextPath : null,
})
.then(async () => {
toast.success("Build type saved");
@ -194,26 +197,51 @@ export const ShowBuildChooseForm = ({ applicationId }: Props) => {
}}
/>
{buildType === "dockerfile" && (
<FormField
control={form.control}
name="dockerfile"
render={({ field }) => {
return (
<FormItem>
<FormLabel>Docker File</FormLabel>
<FormControl>
<Input
placeholder={"Path of your docker file"}
{...field}
value={field.value ?? ""}
/>
</FormControl>
<>
<FormField
control={form.control}
name="dockerfile"
render={({ field }) => {
return (
<FormItem>
<FormLabel>Docker File</FormLabel>
<FormControl>
<Input
placeholder={"Path of your docker file"}
{...field}
value={field.value ?? ""}
/>
</FormControl>
<FormMessage />
</FormItem>
);
}}
/>
<FormMessage />
</FormItem>
);
}}
/>
<FormField
control={form.control}
name="dockerContextPath"
render={({ field }) => {
return (
<FormItem>
<FormLabel>Docker Context Path</FormLabel>
<FormControl>
<Input
placeholder={
"Path of your docker context default: ."
}
{...field}
value={field.value ?? ""}
/>
</FormControl>
<FormMessage />
</FormItem>
);
}}
/>
</>
)}
{buildType === "nixpacks" && (

View File

@ -114,7 +114,7 @@ export const SaveDockerProvider = ({ applicationId }: Props) => {
<FormItem>
<FormLabel>Password</FormLabel>
<FormControl>
<Input placeholder="Password" {...field} />
<Input placeholder="Password" {...field} type="password" />
</FormControl>
<FormMessage />
</FormItem>

View File

@ -0,0 +1 @@
ALTER TABLE "application" ADD COLUMN "dockerContextPath" text;

File diff suppressed because it is too large Load Diff

View File

@ -204,6 +204,13 @@
"when": 1722503439951,
"tag": "0028_jittery_eternity",
"breakpoints": true
},
{
"idx": 29,
"version": "6",
"when": 1722578386823,
"tag": "0029_colossal_zodiak",
"breakpoints": true
}
]
}

View File

@ -192,6 +192,7 @@ export const applicationRouter = createTRPCRouter({
buildType: input.buildType,
dockerfile: input.dockerfile,
publishDirectory: input.publishDirectory,
dockerContextPath: input.dockerContextPath,
});
return true;

View File

@ -141,6 +141,7 @@ export const applications = pgTable("application", {
},
),
dockerfile: text("dockerfile"),
dockerContextPath: text("dockerContextPath"),
// Drop
dropBuildPath: text("dropBuildPath"),
// Docker swarm json
@ -356,6 +357,7 @@ export const apiSaveBuildType = createSchema
applicationId: true,
buildType: true,
dockerfile: true,
dockerContextPath: true,
})
.required()
.merge(createSchema.pick({ publishDirectory: true }));

View File

@ -1,7 +1,10 @@
import type { WriteStream } from "node:fs";
import { prepareEnvironmentVariables } from "@/server/utils/docker/utils";
import type { ApplicationNested } from ".";
import { getBuildAppDirectory } from "../filesystem/directory";
import {
getBuildAppDirectory,
getDockerContextPath,
} from "../filesystem/directory";
import { spawnAsync } from "../process/spawnAsync";
import { createEnvFile } from "./utils";
@ -14,21 +17,21 @@ export const buildCustomDocker = async (
try {
const image = `${appName}`;
const contextPath =
const defaultContextPath =
dockerFilePath.substring(0, dockerFilePath.lastIndexOf("/") + 1) || ".";
const args = prepareEnvironmentVariables(buildArgs);
const dockerContextPath = getDockerContextPath(application);
const commandArgs = ["build", "-t", image, "-f", dockerFilePath, "."];
for (const arg of args) {
commandArgs.push("--build-arg", arg);
}
/*
Do not generate an environment file when publishDirectory is specified,
as it could be publicly exposed.
*/
if (!publishDirectory) {
createEnvFile(dockerFilePath, env);
}
@ -42,7 +45,7 @@ export const buildCustomDocker = async (
}
},
{
cwd: contextPath,
cwd: dockerContextPath || defaultContextPath,
},
);
} catch (error) {

View File

@ -89,3 +89,12 @@ export const getBuildAppDirectory = (application: Application) => {
return path.join(APPLICATIONS_PATH, appName, "code", buildPath ?? "");
};
export const getDockerContextPath = (application: Application) => {
const { appName, dockerContextPath } = application;
if (!dockerContextPath) {
return null;
}
return path.join(APPLICATIONS_PATH, appName, "code", dockerContextPath);
};