fix: compose containers

This commit is contained in:
Lorenzo Migliorero 2024-07-22 17:33:42 +02:00
parent a15eb3b229
commit 7c981b2aac
No known key found for this signature in database
GPG Key ID: 9A9F1AD60C05DFE2
3 changed files with 17 additions and 4 deletions

View File

@ -30,12 +30,14 @@ export const DockerLogs = dynamic(
interface Props {
appName: string;
appType: "stack" | "docker-compose";
}
export const ShowDockerLogsCompose = ({ appName }: Props) => {
export const ShowDockerLogsCompose = ({ appName, appType }: Props) => {
const { data } = api.docker.getContainersByAppNameMatch.useQuery(
{
appName,
appType,
},
{
enabled: !!appName,

View File

@ -25,11 +25,14 @@ export const dockerRouter = createTRPCRouter({
getContainersByAppNameMatch: protectedProcedure
.input(
z.object({
appType: z
.union([z.literal("stack"), z.literal("docker-compose")])
.optional(),
appName: z.string().min(1),
}),
)
.query(async ({ input }) => {
return await getContainersByAppNameMatch(input.appName);
return await getContainersByAppNameMatch(input.appName, input.appType);
}),
getContainersByAppLabel: protectedProcedure

View File

@ -66,10 +66,18 @@ export const getConfig = async (containerId: string) => {
} catch (error) {}
};
export const getContainersByAppNameMatch = async (appName: string) => {
export const getContainersByAppNameMatch = async (
appName: string,
appType?: "stack" | "docker-compose",
) => {
try {
const cmd =
"docker ps -a --format 'CONTAINER ID : {{.ID}} | Name: {{.Names}} | State: {{.State}}'";
const { stdout, stderr } = await execAsync(
`docker ps -a --format 'CONTAINER ID : {{.ID}} | Name: {{.Names}} | State: {{.State}}' | grep ${appName}`,
appType === "docker-compose"
? `${cmd} --filter='label=com.docker.compose.project=${appName}'`
: `${cmd} | grep ${appName}`,
);
if (stderr) {