mirror of
https://github.com/Crosstalk-Solutions/project-nomad.git
synced 2026-03-28 03:29:25 +01:00
feat(Docker): avoid repulling existing images
This commit is contained in:
parent
cb85785cb1
commit
c78736c8da
|
|
@ -310,11 +310,7 @@ export class DockerService {
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
logger.warn(`Error during container cleanup: ${error.message}`)
|
logger.warn(`Error during container cleanup: ${error.message}`)
|
||||||
this._broadcast(
|
this._broadcast(serviceName, 'cleanup-warning', `Warning during cleanup: ${error.message}`)
|
||||||
serviceName,
|
|
||||||
'cleanup-warning',
|
|
||||||
`Warning during cleanup: ${error.message}`
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Step 3: Clear volumes/data if needed
|
// Step 3: Clear volumes/data if needed
|
||||||
|
|
@ -356,7 +352,7 @@ export class DockerService {
|
||||||
// Step 5: Recreate the container
|
// Step 5: Recreate the container
|
||||||
this._broadcast(serviceName, 'recreating', `Recreating container...`)
|
this._broadcast(serviceName, 'recreating', `Recreating container...`)
|
||||||
const containerConfig = this._parseContainerConfig(service.container_config)
|
const containerConfig = this._parseContainerConfig(service.container_config)
|
||||||
|
|
||||||
// Execute installation asynchronously and handle cleanup
|
// Execute installation asynchronously and handle cleanup
|
||||||
this._createContainer(service, containerConfig).catch(async (error) => {
|
this._createContainer(service, containerConfig).catch(async (error) => {
|
||||||
logger.error(`Reinstallation failed for ${serviceName}: ${error.message}`)
|
logger.error(`Reinstallation failed for ${serviceName}: ${error.message}`)
|
||||||
|
|
@ -427,14 +423,23 @@ export class DockerService {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Start pulling the Docker image and wait for it to complete
|
const imageExists = await this._checkImageExists(service.container_image)
|
||||||
const pullStream = await this.docker.pull(service.container_image)
|
if (imageExists) {
|
||||||
this._broadcast(
|
this._broadcast(
|
||||||
service.service_name,
|
service.service_name,
|
||||||
'pulling',
|
'image-exists',
|
||||||
`Pulling Docker image ${service.container_image}...`
|
`Docker image ${service.container_image} already exists locally. Skipping pull...`
|
||||||
)
|
)
|
||||||
await new Promise((res) => this.docker.modem.followProgress(pullStream, res))
|
} else {
|
||||||
|
// Start pulling the Docker image and wait for it to complete
|
||||||
|
const pullStream = await this.docker.pull(service.container_image)
|
||||||
|
this._broadcast(
|
||||||
|
service.service_name,
|
||||||
|
'pulling',
|
||||||
|
`Pulling Docker image ${service.container_image}...`
|
||||||
|
)
|
||||||
|
await new Promise((res) => this.docker.modem.followProgress(pullStream, res))
|
||||||
|
}
|
||||||
|
|
||||||
if (service.service_name === DockerService.KIWIX_SERVICE_NAME) {
|
if (service.service_name === DockerService.KIWIX_SERVICE_NAME) {
|
||||||
await this._runPreinstallActions__KiwixServe()
|
await this._runPreinstallActions__KiwixServe()
|
||||||
|
|
@ -466,7 +471,7 @@ export class DockerService {
|
||||||
[DockerService.NOMAD_NETWORK]: {},
|
[DockerService.NOMAD_NETWORK]: {},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
})
|
}),
|
||||||
})
|
})
|
||||||
|
|
||||||
this._broadcast(
|
this._broadcast(
|
||||||
|
|
@ -632,4 +637,22 @@ export class DockerService {
|
||||||
throw new Error(`Invalid container configuration: ${error.message}`)
|
throw new Error(`Invalid container configuration: ${error.message}`)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check if a Docker image exists locally.
|
||||||
|
* @param imageName - The name and tag of the image (e.g., "nginx:latest")
|
||||||
|
* @returns - True if the image exists locally, false otherwise
|
||||||
|
*/
|
||||||
|
private async _checkImageExists(imageName: string): Promise<boolean> {
|
||||||
|
try {
|
||||||
|
const images = await this.docker.listImages()
|
||||||
|
|
||||||
|
// Check if any image has a RepoTag that matches the requested image
|
||||||
|
return images.some((image) => image.RepoTags && image.RepoTags.includes(imageName))
|
||||||
|
} catch (error) {
|
||||||
|
logger.warn(`Error checking if image exists: ${error.message}`)
|
||||||
|
// If run into an error, assume the image does not exist
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user