diff --git a/server/src/config.js b/server/src/config.js index 7304fb5..a228473 100644 --- a/server/src/config.js +++ b/server/src/config.js @@ -1,14 +1,9 @@ -function get(key, defaultValue) { - const value = process.env[key] - if (value != null) { - return value - } - return defaultValue -} +import { get } from './utils/env.js' export default { host: get('HOST', 'localhost'), port: get('PORT', 3000), db: get('MONGO_URL', 'mongodb://localhost:27017/odyssey-test'), jwtSecret: get('JWT_SECRET', 'secret'), + dockerNetwork: get('DOCKER_NETWORK', 'linux-odyssey-players'), } diff --git a/server/src/utils/docker.js b/server/src/utils/docker.js index 449bf4b..7f5189b 100644 --- a/server/src/utils/docker.js +++ b/server/src/utils/docker.js @@ -1,9 +1,10 @@ import Docker from 'dockerode' +import config from '../config.js' const engine = new Docker() const containerOptions = { - Image: 'ubuntu', + Image: 'lancatlin/quest-helloworld', AttachStdin: true, AttachStdout: true, AttachStderr: true, @@ -13,19 +14,7 @@ const containerOptions = { StdinOnce: false, } -export async function createContainer() { - const container = await engine.createContainer({ - ...containerOptions, - }) - await container.start() - return container -} - -export async function getContainer(id) { - const container = await engine.getContainer(id) - await container.start() - return container -} +const network = engine.getNetwork(config.dockerNetwork) export async function getOrCreateContainer(id) { console.log(`Getting container: ${id}`) @@ -40,6 +29,8 @@ export async function getOrCreateContainer(id) { ...containerOptions, name: id, }) + await network.connect({ Container: container.id }) + await container.start() console.log(container.id) return container @@ -54,7 +45,7 @@ export async function attachContainer(container, { token }) { AttachStderr: true, Cmd: ['/bin/bash'], Tty: true, - Env: [`TOKEN=${token}`], + Env: [`TOKEN=${token}`, `API_ENDPOINT=http://app:3000`], }) const execOutput = await exec.start({ diff --git a/server/src/utils/env.js b/server/src/utils/env.js new file mode 100644 index 0000000..2db79cc --- /dev/null +++ b/server/src/utils/env.js @@ -0,0 +1,7 @@ +export function get(key, defaultValue) { + const value = process.env[key] + if (value != null) { + return value + } + return defaultValue +}