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

+2 -1
View File
@@ -77,7 +77,8 @@ async function connectContainer(socket: Socket, next: (err?: Error) => void) {
stream,
}
next()
} catch {
} catch (err) {
console.error(err)
next(new Error('Failed to start container.'))
}
}
+1 -1
View File
@@ -31,7 +31,7 @@ function getUrl(key: string): string {
function createConfig() {
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 baseUrl = get('BASE_URL', `http://${host}:${port}`)
const url = new URL(baseUrl)
+25 -18
View File
@@ -4,45 +4,52 @@ import logger from '../utils/logger.js'
const engine = new Docker()
const containerOptions = {
const newContainerOptions = (
name: string,
imageId: string,
options: {
binds?: string[]
} = {}
): Docker.ContainerCreateOptions => ({
// AttachStdin: true,
// AttachStdout: true,
// AttachStderr: true,
// Tty: true,
// OpenStdin: true,
// StdinOnce: false,
name,
Image: getQuestImage(imageId),
HostConfig: {
NetworkMode: config.docker.network,
PortBindings: {
'22/tcp': [
{
HostPort: '11122', // random port
},
],
},
NetworkMode: 'linux-odyssey-players', // config.docker.network,
// PortBindings: {
// '22/tcp': [
// {
// HostIP: '0.0.0.0',
// HostPort: '11122', // random port
// },
// ],
// },
Binds: options.binds,
ExtraHosts: ['host.docker.internal:host-gateway'],
},
ExposedPorts: {
'22/tcp': {},
// '22/tcp': {},
},
} as Docker.ContainerCreateOptions
})
export function createContainer(
name: string,
imageId: string
): Promise<Docker.Container> {
const option = {
...containerOptions,
name,
Image: getQuestImage(imageId),
}
let binds: string[] = []
if (!config.isProduction && config.docker.mountQuest && imageId !== 'base') {
logger.info('Mounting quest folder', imageId)
option.HostConfig!.Binds = [
binds = [
`${config.projectRoot}/quests/${imageId}/home:/home/commander`,
`${config.projectRoot}/packages/container:/usr/local/lib/container`,
]
}
const option = newContainerOptions(name, imageId, { binds })
return engine.createContainer(option)
}
+1 -1
View File
@@ -72,7 +72,7 @@ async function main() {
app.use('/api/v1', apiRouter)
app.use(errorHandler)
server.listen(config.port, config.host, () => {
server.listen(config.port, '0.0.0.0', () => {
logger.info(`Server listening at ${config.baseUrl}`)
})
}