wip: allow connection from docker container

This commit is contained in:
Justin Lin committed 2024-10-13 15:05:20 +08:00
1 parent 5ba0021c37
commit 4c292fd6ba
6 files changed
+36 -22

No files matched your search

+5 -1
View File
@@ -12,12 +12,16 @@ services:
base: base:
image: linuxodyssey/quest-base image: linuxodyssey/quest-base
# networks:
# - players
build: build:
context: . context: .
dockerfile: ./quests/Dockerfile dockerfile: ./quests/Dockerfile
extra_hosts:
- 'host.docker.internal:host-gateway'
networks: networks:
internal: internal:
players: players:
name: linux-odyssey-players name: linux-odyssey-players
internal: true internal: false
+2
View File
@@ -30,6 +30,8 @@ RUN chmod 600 /home/${USERNAME}/.ssh/authorized_keys \
&& chmod 700 /home/${USERNAME}/.ssh \ && chmod 700 /home/${USERNAME}/.ssh \
&& chown -R ${USERNAME}:${USERNAME} /home/${USERNAME}/.ssh && chown -R ${USERNAME}:${USERNAME} /home/${USERNAME}/.ssh
RUN apt-get install -y net-tools
RUN service ssh start RUN service ssh start
EXPOSE 22 EXPOSE 22
+2 -1
View File
@@ -77,7 +77,8 @@ async function connectContainer(socket: Socket, next: (err?: Error) => void) {
stream, stream,
} }
next() next()
} catch { } catch (err) {
console.error(err)
next(new Error('Failed to start container.')) next(new Error('Failed to start container.'))
} }
} }
+1 -1
View File
@@ -31,7 +31,7 @@ function getUrl(key: string): string {
function createConfig() { function createConfig() {
const isProduction = process.env.NODE_ENV === 'production' const isProduction = process.env.NODE_ENV === 'production'
const host = get('HOST', 'localhost') const host = get('HOST', '0.0.0.0')
const port = Number(get('PORT', 3000)) const port = Number(get('PORT', 3000))
const baseUrl = get('BASE_URL', `http://${host}:${port}`) const baseUrl = get('BASE_URL', `http://${host}:${port}`)
const url = new URL(baseUrl) const url = new URL(baseUrl)
+25 -18
View File
@@ -4,45 +4,52 @@ import logger from '../utils/logger.js'
const engine = new Docker() const engine = new Docker()
const containerOptions = { const newContainerOptions = (
name: string,
imageId: string,
options: {
binds?: string[]
} = {}
): Docker.ContainerCreateOptions => ({
// AttachStdin: true, // AttachStdin: true,
// AttachStdout: true, // AttachStdout: true,
// AttachStderr: true, // AttachStderr: true,
// Tty: true, // Tty: true,
// OpenStdin: true, // OpenStdin: true,
// StdinOnce: false, // StdinOnce: false,
name,
Image: getQuestImage(imageId),
HostConfig: { HostConfig: {
NetworkMode: config.docker.network, NetworkMode: 'linux-odyssey-players', // config.docker.network,
PortBindings: { // PortBindings: {
'22/tcp': [ // '22/tcp': [
{ // {
HostPort: '11122', // random port // HostIP: '0.0.0.0',
}, // HostPort: '11122', // random port
], // },
}, // ],
// },
Binds: options.binds,
ExtraHosts: ['host.docker.internal:host-gateway'],
}, },
ExposedPorts: { ExposedPorts: {
'22/tcp': {}, // '22/tcp': {},
}, },
} as Docker.ContainerCreateOptions })
export function createContainer( export function createContainer(
name: string, name: string,
imageId: string imageId: string
): Promise<Docker.Container> { ): Promise<Docker.Container> {
const option = { let binds: string[] = []
...containerOptions,
name,
Image: getQuestImage(imageId),
}
if (!config.isProduction && config.docker.mountQuest && imageId !== 'base') { if (!config.isProduction && config.docker.mountQuest && imageId !== 'base') {
logger.info('Mounting quest folder', imageId) logger.info('Mounting quest folder', imageId)
option.HostConfig!.Binds = [ binds = [
`${config.projectRoot}/quests/${imageId}/home:/home/commander`, `${config.projectRoot}/quests/${imageId}/home:/home/commander`,
`${config.projectRoot}/packages/container:/usr/local/lib/container`, `${config.projectRoot}/packages/container:/usr/local/lib/container`,
] ]
} }
const option = newContainerOptions(name, imageId, { binds })
return engine.createContainer(option) return engine.createContainer(option)
} }
+1 -1
View File
@@ -72,7 +72,7 @@ async function main() {
app.use('/api/v1', apiRouter) app.use('/api/v1', apiRouter)
app.use(errorHandler) app.use(errorHandler)
server.listen(config.port, config.host, () => { server.listen(config.port, '0.0.0.0', () => {
logger.info(`Server listening at ${config.baseUrl}`) logger.info(`Server listening at ${config.baseUrl}`)
}) })
} }