mirror of
https://github.com/linux-odyssey/linux-odyssey.git
synced 2026-11-06 03:20:13 +08:00
47 lines
847 B
JavaScript
47 lines
847 B
JavaScript
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,
|
|
}
|
|
|
|
async function createContainer() {
|
|
try {
|
|
const container = await engine.createContainer(containerOptions)
|
|
await container.start()
|
|
return container
|
|
} catch (error) {
|
|
console.error(error)
|
|
throw error
|
|
}
|
|
}
|
|
|
|
async function attachContainer() {
|
|
const container = await createContainer()
|
|
console.log(container.id)
|
|
// Create an exec instance with bash shell
|
|
|
|
container.attach(
|
|
{
|
|
stream: true,
|
|
stdin: true,
|
|
stdout: true,
|
|
stderr: true,
|
|
ws: true,
|
|
},
|
|
(err, stream) => {
|
|
stream.pipe(process.stdout)
|
|
}
|
|
)
|
|
}
|
|
|
|
attachContainer()
|