mirror of
https://github.com/linux-odyssey/linux-odyssey.git
synced 2026-11-06 03:20:13 +08:00
fix: remove hard coded project path
+ pass host_pwd using docker compose + mount it only when provided
This commit is contained in:
1 parent
cf5b761f3f
commit
4b3a80608c
4 files changed
+10
-9
No files matched your search
@@ -0,0 +1,71 @@
|
||||
import Docker from 'dockerode'
|
||||
import config from '../config.js'
|
||||
|
||||
const engine = new Docker()
|
||||
|
||||
const containerOptions = {
|
||||
Image: 'lancatlin/linux-odyssey:helloworld',
|
||||
AttachStdin: true,
|
||||
AttachStdout: true,
|
||||
AttachStderr: true,
|
||||
Tty: true,
|
||||
OpenStdin: true,
|
||||
StdinOnce: false,
|
||||
HostConfig: {
|
||||
Binds:
|
||||
config.isProduction && config.hostPwd
|
||||
? []
|
||||
: [
|
||||
`${config.hostPwd}/quests/helloworld/home:/home/rudeus`,
|
||||
`${config.hostPwd}/packages/container:/usr/local/lib/container`,
|
||||
],
|
||||
},
|
||||
}
|
||||
|
||||
const network = engine.getNetwork(config.dockerNetwork)
|
||||
|
||||
export async function getOrCreateContainer(id) {
|
||||
console.log(`Getting container: ${id}`)
|
||||
let container
|
||||
try {
|
||||
container = engine.getContainer(id)
|
||||
if (!(await container.inspect()).State.Running) {
|
||||
await container.start()
|
||||
}
|
||||
return container
|
||||
} catch (error) {
|
||||
try {
|
||||
container = await engine.createContainer({
|
||||
...containerOptions,
|
||||
name: id,
|
||||
})
|
||||
} catch (err) {
|
||||
console.log(err)
|
||||
throw err
|
||||
}
|
||||
await network.connect({ Container: container.id })
|
||||
|
||||
await container.start()
|
||||
console.log(container.id)
|
||||
return container
|
||||
}
|
||||
}
|
||||
|
||||
export async function attachContainer(container, { token }) {
|
||||
// Create an exec instance with bash shell
|
||||
const exec = await container.exec({
|
||||
AttachStdin: true,
|
||||
AttachStdout: true,
|
||||
AttachStderr: true,
|
||||
Cmd: ['/bin/zsh'],
|
||||
Tty: true,
|
||||
Env: [`TOKEN=${token}`, `API_ENDPOINT=http://backend:3000`],
|
||||
})
|
||||
|
||||
const execOutput = await exec.start({
|
||||
Detach: false,
|
||||
Tty: true,
|
||||
})
|
||||
|
||||
return execOutput.socket
|
||||
}
|
||||
Reference in new issue
Block a user