feat: send command typed in container to API

- Modify the Dockerfile in the `server/quests/helloworld` directory to add a user and change the working directory
- Rename the file `server/quests/helloworld/entrypoint.sh` to `server/quests/helloworld/home/.bashrc`
- Modify the `server/src/config.js` file to add the `isProduction` flag
- Modify the `server/src/utils/docker.js` file to add a `HostConfig` option and bind a directory if not in production mode
- Modify the Dockerfile to add an argument `DOCKER_GID` and create a group and user with the specified `DOCKER_GID`
- Add a new Dockerfile.dev file with the same modifications as the Dockerfile
- Add a new file `inspect.sh`
- Modify the `server/src/client.js` file to use the `API_ENDPOINT` variable instead of a hardcoded URL

Signed-off-by: wancat <wancat@wancat.cc>
This commit is contained in:
wancat committed 2023-07-26 20:54:01 +08:00
1 parent 9376e16f72
commit d997a061a9
8 files changed
+41 -18

No files matched your search

+18 -5
View File
@@ -3,6 +3,8 @@ import config from '../config.js'
const engine = new Docker()
const questPath = '/home/zeko/src/linux-odyssey/server/quests'
const containerOptions = {
Image: 'lancatlin/quest-helloworld',
AttachStdin: true,
@@ -12,23 +14,34 @@ const containerOptions = {
Cmd: ['/bin/bash'],
OpenStdin: true,
StdinOnce: false,
HostConfig: {
Binds: config.isProduction
? []
: [`${questPath}/helloworld/home:/home/rudeus`],
},
}
const network = engine.getNetwork(config.dockerNetwork)
export async function getOrCreateContainer(id) {
console.log(`Getting container: ${id}`)
let container
try {
const container = engine.getContainer(id)
container = engine.getContainer(id)
if (!(await container.inspect()).State.Running) {
await container.start()
}
return container
} catch (error) {
const container = await engine.createContainer({
...containerOptions,
name: id,
})
try {
container = await engine.createContainer({
...containerOptions,
name: id,
})
} catch (error) {
console.log(error)
throw error
}
await network.connect({ Container: container.id })
await container.start()