mirror of
https://github.com/linux-odyssey/linux-odyssey.git
synced 2026-11-08 03:20:12 +08:00
feat: connect containers to custom network
- Export the `get` function in `utils/env.js` - Change the Docker image in `docker.js` from `ubuntu` to `lancatlin/quest-helloworld` - Remove the `createContainer` and `getContainer` functions from `docker.js` - Add a `network` constant in `docker.js` that gets the Docker network from the config - Connect the container to the network in `getOrCreateContainer` function in `docker.js` - Add `API_ENDPOINT` environment variable in the `attachContainer` function in `docker.js` Signed-off-by: wancat <wancat@wancat.cc>
This commit is contained in:
1 parent
189c87df5a
commit
943c11714c
3 files changed
+15
-22
No files matched your search
@@ -1,14 +1,9 @@
|
|||||||
function get(key, defaultValue) {
|
import { get } from './utils/env.js'
|
||||||
const value = process.env[key]
|
|
||||||
if (value != null) {
|
|
||||||
return value
|
|
||||||
}
|
|
||||||
return defaultValue
|
|
||||||
}
|
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
host: get('HOST', 'localhost'),
|
host: get('HOST', 'localhost'),
|
||||||
port: get('PORT', 3000),
|
port: get('PORT', 3000),
|
||||||
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'),
|
||||||
}
|
}
|
||||||
@@ -1,9 +1,10 @@
|
|||||||
import Docker from 'dockerode'
|
import Docker from 'dockerode'
|
||||||
|
import config from '../config.js'
|
||||||
|
|
||||||
const engine = new Docker()
|
const engine = new Docker()
|
||||||
|
|
||||||
const containerOptions = {
|
const containerOptions = {
|
||||||
Image: 'ubuntu',
|
Image: 'lancatlin/quest-helloworld',
|
||||||
AttachStdin: true,
|
AttachStdin: true,
|
||||||
AttachStdout: true,
|
AttachStdout: true,
|
||||||
AttachStderr: true,
|
AttachStderr: true,
|
||||||
@@ -13,19 +14,7 @@ const containerOptions = {
|
|||||||
StdinOnce: false,
|
StdinOnce: false,
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function createContainer() {
|
const network = engine.getNetwork(config.dockerNetwork)
|
||||||
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
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function getOrCreateContainer(id) {
|
export async function getOrCreateContainer(id) {
|
||||||
console.log(`Getting container: ${id}`)
|
console.log(`Getting container: ${id}`)
|
||||||
@@ -40,6 +29,8 @@ export async function getOrCreateContainer(id) {
|
|||||||
...containerOptions,
|
...containerOptions,
|
||||||
name: id,
|
name: id,
|
||||||
})
|
})
|
||||||
|
await network.connect({ Container: container.id })
|
||||||
|
|
||||||
await container.start()
|
await container.start()
|
||||||
console.log(container.id)
|
console.log(container.id)
|
||||||
return container
|
return container
|
||||||
@@ -54,7 +45,7 @@ export async function attachContainer(container, { token }) {
|
|||||||
AttachStderr: true,
|
AttachStderr: true,
|
||||||
Cmd: ['/bin/bash'],
|
Cmd: ['/bin/bash'],
|
||||||
Tty: true,
|
Tty: true,
|
||||||
Env: [`TOKEN=${token}`],
|
Env: [`TOKEN=${token}`, `API_ENDPOINT=http://app:3000`],
|
||||||
})
|
})
|
||||||
|
|
||||||
const execOutput = await exec.start({
|
const execOutput = await exec.start({
|
||||||
|
|||||||
@@ -0,0 +1,7 @@
|
|||||||
|
export function get(key, defaultValue) {
|
||||||
|
const value = process.env[key]
|
||||||
|
if (value != null) {
|
||||||
|
return value
|
||||||
|
}
|
||||||
|
return defaultValue
|
||||||
|
}
|
||||||
Reference in new issue
Block a user