dockerode-compose icon indicating copy to clipboard operation
dockerode-compose copied to clipboard

Add auth to compose pull

Open AndreasHeine opened this issue 2 years ago • 1 comments

hey,

i will tackle the auth topic in the next weeks!

i started working around it by using just dockerode:

    const requiredImages: string[] = []
    for (const [_, value] of Object.entries(getServiceInstance().recipe.services)) {
        requiredImages.push((value as any).image)
    }
    await Promise.all(requiredImages.map(async (image: string) => {
        try {
            console.log("pulling image:", image)
            if (image.startsWith("localhost:15004/")) {
                const options = {
                    // authconfig: {
                    //     username: config.regestryUser,
                    //     password: config.registryPw,
                    // }
                }
                await pullImage(image, options)
            } else {
                await pullImage(image)
            }
        } catch (error: any) {
            console.error(image, "-->", error)
        }
    }))

but this has some flavour ;)

just passing the auth options like in this PR will probably not be enough because not all images of a service are exclusively from one Registry...

so for beeing able to use it in a common way we probably need to pass a array of auth options to the compose pull function so we can try to figure out from which registry we need to pull with the correct auth options.

https://github.com/apocas/dockerode-compose/issues/28

AndreasHeine avatar Sep 28 '23 09:09 AndreasHeine

small update on the subject:

i tested the following with two private registries!


export async function pullImage(image: string, options?: any): Promise<void> {
    if (options === undefined) {
        if (image.includes("myregistry1.com")) {
            options = {
                authconfig: {
                    username: "myregistry1-user",
                    password: "myregistry1-pw",
                    auth: '',
                    email: '[email protected]',
                    serveraddress: 'https://myregistry1.com/v2/'
                }
            }
        }
        if ( image.includes("myregistry2.com")) {
            options = {
                authconfig: {
                    username: "myregistry2-user",
                    password: "myregistry2-pw",
                    auth: '',
                    email: '[email protected]',
                    serveraddress: 'https://myregistry2.com/v2/'
                }
            }
        }
    }
    const pullStream = await docker.pull(image, options)
    await new Promise(res => docker.modem.followProgress(pullStream, res))
}

what that means in order to make a compose pull to a mix of registrys:

const registry = {
    "myregistry1.com": {
        username: "",
        password: "",
        email: "",
        serveraddress: ""        
    },
    "myregistry2.com": {
        username: "",
        password: "",
        email: "",
        serveraddress: ""
    },
}

compose.pull(serviceN, options, registry)

/* 
inside compose.pull() we look if a prefix fits (myregistry1.com or myregistry2.com) and select to correct auth options

maybe like:
Object.keys(registry).map((key) => {
    if (imageName.includes(key)) {
        return registry[key]
    }
})
*/

PR update follows soon!

AndreasHeine avatar Oct 30 '23 14:10 AndreasHeine