mirror of
https://github.com/linux-odyssey/linux-odyssey.git
synced 2026-11-05 19:20:12 +08:00
Create docker container and attach
This commit is contained in:
1 parent
14663476ae
commit
0ca4bf0bc8
3 files changed
+230
-6
No files matched your search
@@ -0,0 +1,46 @@
|
||||
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()
|
||||
+3
-1
@@ -8,7 +8,9 @@
|
||||
"dev": "nodemon index.js"
|
||||
},
|
||||
"dependencies": {
|
||||
"express": "^4.18.2"
|
||||
"dockerode": "^3.3.5",
|
||||
"express": "^4.18.2",
|
||||
"ws": "^8.13.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"nodemon": "^3.0.1"
|
||||
|
||||
Reference in new issue
Block a user