Connect to different shell

This commit is contained in:
wancat committed 2023-07-22 16:35:44 +08:00
1 parent 4a74127785
commit c83ca750e1
2 files changed
+29 -7

No files matched your search

+1 -1
View File
@@ -22,6 +22,6 @@ export default (server) => {
console.log('Disconnected from the client.') console.log('Disconnected from the client.')
}) })
socket.send('Hello from server!') socket.send(`${container.id}\n`)
}) })
} }
+28 -6
View File
@@ -13,6 +13,20 @@ const containerOptions = {
StdinOnce: false, StdinOnce: false,
} }
export async function createContainer() {
const container = await engine.createContainer({
...containerOptions,
})
await container.start()
return container
}
export async function getContainer(id) {
const container = await engine.getContainer(id)
await container.start()
return container
}
export async function getOrCreateContainer(name) { export async function getOrCreateContainer(name) {
try { try {
const container = engine.getContainer(name) const container = engine.getContainer(name)
@@ -31,12 +45,20 @@ export async function getOrCreateContainer(name) {
} }
} }
export function attachContainer(container) { export async function attachContainer(container) {
// Create an exec instance with bash shell // Create an exec instance with bash shell
return container.attach({ const exec = await container.exec({
stream: true, AttachStdin: true,
stdin: true, AttachStdout: true,
stdout: true, AttachStderr: true,
stderr: true, Cmd: ['/bin/bash'],
Tty: true,
}) })
const execOutput = await exec.start({
Detach: false,
Tty: true,
})
return execOutput.socket
} }