mirror of
https://github.com/linux-odyssey/linux-odyssey.git
synced 2026-11-08 11:30:12 +08:00
Reuse container by name
This commit is contained in:
1 parent
e3d66709e4
commit
df12fee39c
2 files changed
+11
-7
No files matched your search
+9
-5
@@ -13,15 +13,19 @@ const containerOptions = {
|
|||||||
StdinOnce: false,
|
StdinOnce: false,
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function createContainer() {
|
export async function getOrCreateContainer(name) {
|
||||||
try {
|
try {
|
||||||
const container = await engine.createContainer(containerOptions)
|
const container = engine.getContainer(name)
|
||||||
|
await container.inspect()
|
||||||
|
return container
|
||||||
|
} catch (error) {
|
||||||
|
const container = await engine.createContainer({
|
||||||
|
...containerOptions,
|
||||||
|
name,
|
||||||
|
})
|
||||||
await container.start()
|
await container.start()
|
||||||
console.log(container.id)
|
console.log(container.id)
|
||||||
return container
|
return container
|
||||||
} catch (error) {
|
|
||||||
console.error(error)
|
|
||||||
throw error
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+2
-2
@@ -1,7 +1,7 @@
|
|||||||
import express from 'express'
|
import express from 'express'
|
||||||
import http from 'http'
|
import http from 'http'
|
||||||
import { Server } from 'socket.io'
|
import { Server } from 'socket.io'
|
||||||
import { createContainer, attachContainer } from './docker.js'
|
import { getOrCreateContainer, attachContainer } from './docker.js'
|
||||||
|
|
||||||
const app = express()
|
const app = express()
|
||||||
const server = http.createServer(app)
|
const server = http.createServer(app)
|
||||||
@@ -9,7 +9,7 @@ const io = new Server(server)
|
|||||||
|
|
||||||
io.on('connection', async (socket) => {
|
io.on('connection', async (socket) => {
|
||||||
console.log('Connected to the client.')
|
console.log('Connected to the client.')
|
||||||
const container = await createContainer()
|
const container = await getOrCreateContainer('odyssey-test')
|
||||||
const stream = await attachContainer(container)
|
const stream = await attachContainer(container)
|
||||||
|
|
||||||
stream.on('data', (chunk) => {
|
stream.on('data', (chunk) => {
|
||||||
|
|||||||
Reference in new issue
Block a user