mirror of
https://github.com/linux-odyssey/linux-odyssey.git
synced 2026-11-06 11:30:13 +08:00
Re-organize code
This commit is contained in:
1 parent
8901b385ad
commit
d51a75e5bc
7 files changed
+7
-5
No files matched your search
@@ -0,0 +1,42 @@
|
||||
import Docker from 'dockerode'
|
||||
|
||||
const engine = new Docker()
|
||||
|
||||
const containerOptions = {
|
||||
Image: 'ubuntu',
|
||||
AttachStdin: true,
|
||||
AttachStdout: true,
|
||||
AttachStderr: true,
|
||||
Tty: true,
|
||||
Cmd: ['/bin/bash'],
|
||||
OpenStdin: true,
|
||||
StdinOnce: false,
|
||||
}
|
||||
|
||||
export async function getOrCreateContainer(name) {
|
||||
try {
|
||||
const container = engine.getContainer(name)
|
||||
if (!(await container.inspect()).State.Running) {
|
||||
await container.start()
|
||||
}
|
||||
return container
|
||||
} catch (error) {
|
||||
const container = await engine.createContainer({
|
||||
...containerOptions,
|
||||
name,
|
||||
})
|
||||
await container.start()
|
||||
console.log(container.id)
|
||||
return container
|
||||
}
|
||||
}
|
||||
|
||||
export function attachContainer(container) {
|
||||
// Create an exec instance with bash shell
|
||||
return container.attach({
|
||||
stream: true,
|
||||
stdin: true,
|
||||
stdout: true,
|
||||
stderr: true,
|
||||
})
|
||||
}
|
||||
Reference in new issue
Block a user