feat: seperate container creation and get

This commit is contained in:
wancat committed 2023-08-23 11:51:16 +08:00
1 parent 2c866d2e83
commit 8e63284211
3 files changed
+43 -19

No files matched your search

+11 -15
View File
@@ -24,7 +24,16 @@ const containerOptions = {
const network = engine.getNetwork(config.dockerNetwork)
export async function getOrCreateContainer(id) {
export async function createContainer(name) {
const container = await engine.createContainer({
...containerOptions,
name,
})
await network.connect({ Container: container.id })
return container
}
export async function getAndStartContainer(id) {
console.log(`Getting container: ${id}`)
let container
try {
@@ -34,20 +43,7 @@ export async function getOrCreateContainer(id) {
}
return container
} catch (error) {
try {
container = await engine.createContainer({
...containerOptions,
name: id,
})
} catch (err) {
console.log(err)
throw err
}
await network.connect({ Container: container.id })
await container.start()
console.log(container.id)
return container
throw new Error(`Container not found: ${id}`)
}
}