mirror of
https://github.com/linux-odyssey/linux-odyssey.git
synced 2026-11-05 19:20:12 +08:00
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:
1 parent
9376e16f72
commit
d997a061a9
8 files changed
+37
-14
No files matched your search
@@ -5,8 +5,14 @@ COPY package.json yarn.lock ./
|
|||||||
COPY server/package.json ./server/
|
COPY server/package.json ./server/
|
||||||
RUN yarn install --frozen-lockfile --production
|
RUN yarn install --frozen-lockfile --production
|
||||||
|
|
||||||
|
ARG DOCKER_GID=140
|
||||||
|
|
||||||
|
RUN groupadd -g $DOCKER_GID docker && usermod -aG docker node
|
||||||
|
|
||||||
WORKDIR /app/server
|
WORKDIR /app/server
|
||||||
|
|
||||||
|
USER node
|
||||||
|
|
||||||
COPY server/src ./src
|
COPY server/src ./src
|
||||||
COPY server/quests ./quests
|
COPY server/quests ./quests
|
||||||
COPY server/swagger.yaml .
|
COPY server/swagger.yaml .
|
||||||
|
|||||||
@@ -7,6 +7,12 @@ RUN yarn install --frozen-lockfile
|
|||||||
|
|
||||||
WORKDIR /app/server
|
WORKDIR /app/server
|
||||||
|
|
||||||
|
ARG DOCKER_GID=140
|
||||||
|
|
||||||
|
RUN groupadd -g $DOCKER_GID docker && usermod -aG docker node
|
||||||
|
|
||||||
|
USER node
|
||||||
|
|
||||||
ENV NODE_ENV=development
|
ENV NODE_ENV=development
|
||||||
ENV HOST=0.0.0.0
|
ENV HOST=0.0.0.0
|
||||||
ENV PORT=3000
|
ENV PORT=3000
|
||||||
|
|||||||
Executable
+2
@@ -0,0 +1,2 @@
|
|||||||
|
#!/bin/sh
|
||||||
|
docker compose logs app -f --no-log-prefix
|
||||||
@@ -4,8 +4,12 @@ RUN apt-get update && apt-get install -y \
|
|||||||
curl \
|
curl \
|
||||||
vim
|
vim
|
||||||
|
|
||||||
WORKDIR /app
|
RUN adduser --disabled-password --gecos '' rudeus
|
||||||
|
|
||||||
COPY . .
|
USER rudeus
|
||||||
|
|
||||||
|
WORKDIR /home/rudeus
|
||||||
|
|
||||||
|
COPY ./home /home/rudeus/
|
||||||
|
|
||||||
CMD [ "/bin/bash" ]
|
CMD [ "/bin/bash" ]
|
||||||
@@ -1,12 +1,3 @@
|
|||||||
#!/bin/bash
|
|
||||||
|
|
||||||
# enable programmable completion features (you don't need to enable
|
|
||||||
# this, if it's already enabled in /etc/bash.bashrc and /etc/profile
|
|
||||||
# sources /etc/bash.bashrc).
|
|
||||||
#if [ -f /etc/bash_completion ] && ! shopt -oq posix; then
|
|
||||||
# . /etc/bash_completion
|
|
||||||
#fi
|
|
||||||
|
|
||||||
# This function will be called before each command is executed
|
# This function will be called before each command is executed
|
||||||
preexec() {
|
preexec() {
|
||||||
[ -n "$COMP_LINE" ] && return # do nothing if completing
|
[ -n "$COMP_LINE" ] && return # do nothing if completing
|
||||||
@@ -28,7 +28,7 @@ async function main() {
|
|||||||
console.log(`Session ID: ${sessionId}`)
|
console.log(`Session ID: ${sessionId}`)
|
||||||
if (!sessionId) exit()
|
if (!sessionId) exit()
|
||||||
|
|
||||||
const socket = io('ws://localhost:3000', {
|
const socket = io(API_ENDPOINT, {
|
||||||
query: {
|
query: {
|
||||||
session_id: sessionId,
|
session_id: sessionId,
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -6,4 +6,5 @@ export default {
|
|||||||
db: get('MONGO_URL', 'mongodb://localhost:27017/odyssey-test'),
|
db: get('MONGO_URL', 'mongodb://localhost:27017/odyssey-test'),
|
||||||
jwtSecret: get('JWT_SECRET', 'secret'),
|
jwtSecret: get('JWT_SECRET', 'secret'),
|
||||||
dockerNetwork: get('DOCKER_NETWORK', 'linux-odyssey-players'),
|
dockerNetwork: get('DOCKER_NETWORK', 'linux-odyssey-players'),
|
||||||
|
isProduction: process.env.NODE_ENV === 'production',
|
||||||
}
|
}
|
||||||
@@ -3,6 +3,8 @@ import config from '../config.js'
|
|||||||
|
|
||||||
const engine = new Docker()
|
const engine = new Docker()
|
||||||
|
|
||||||
|
const questPath = '/home/zeko/src/linux-odyssey/server/quests'
|
||||||
|
|
||||||
const containerOptions = {
|
const containerOptions = {
|
||||||
Image: 'lancatlin/quest-helloworld',
|
Image: 'lancatlin/quest-helloworld',
|
||||||
AttachStdin: true,
|
AttachStdin: true,
|
||||||
@@ -12,23 +14,34 @@ const containerOptions = {
|
|||||||
Cmd: ['/bin/bash'],
|
Cmd: ['/bin/bash'],
|
||||||
OpenStdin: true,
|
OpenStdin: true,
|
||||||
StdinOnce: false,
|
StdinOnce: false,
|
||||||
|
HostConfig: {
|
||||||
|
Binds: config.isProduction
|
||||||
|
? []
|
||||||
|
: [`${questPath}/helloworld/home:/home/rudeus`],
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
const network = engine.getNetwork(config.dockerNetwork)
|
const network = engine.getNetwork(config.dockerNetwork)
|
||||||
|
|
||||||
export async function getOrCreateContainer(id) {
|
export async function getOrCreateContainer(id) {
|
||||||
console.log(`Getting container: ${id}`)
|
console.log(`Getting container: ${id}`)
|
||||||
|
let container
|
||||||
try {
|
try {
|
||||||
const container = engine.getContainer(id)
|
container = engine.getContainer(id)
|
||||||
if (!(await container.inspect()).State.Running) {
|
if (!(await container.inspect()).State.Running) {
|
||||||
await container.start()
|
await container.start()
|
||||||
}
|
}
|
||||||
return container
|
return container
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
const container = await engine.createContainer({
|
try {
|
||||||
|
container = await engine.createContainer({
|
||||||
...containerOptions,
|
...containerOptions,
|
||||||
name: id,
|
name: id,
|
||||||
})
|
})
|
||||||
|
} catch (error) {
|
||||||
|
console.log(error)
|
||||||
|
throw error
|
||||||
|
}
|
||||||
await network.connect({ Container: container.id })
|
await network.connect({ Container: container.id })
|
||||||
|
|
||||||
await container.start()
|
await container.start()
|
||||||
|
|||||||
Reference in new issue
Block a user