Merge pull request #13 from warmthdawn/feat_extraBinding

添加额外路径挂载
This commit is contained in:
Suwings 2022-02-13 15:06:18 +08:00 committed by GitHub
commit a0a6eb8f27
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 37 additions and 9 deletions

View File

@ -27,6 +27,7 @@ import { EventEmitter } from "events";
import { IInstanceProcess } from "../../instance/interface";
import fs from "fs-extra";
import { commandStringToArray } from "../base/command_parser";
import path from "path";
// 用户身份函数
const processUserUid = process.getuid ? process.getuid : () => 0;
@ -141,6 +142,28 @@ export default class DockerStartCommand extends InstanceCommand {
exposedPorts[`${publicAndPrivatePort[1]}/${protocol}`] = {};
}
// 解析额外路径挂载
const extraVolumes = instance.config.docker.extraVolumes;
const extraBinds = [];
for (let it of extraVolumes) {
if (!it) continue;
const element = it.split(":");
if (element.length != 2) continue;
let [hostPath, containerPath] = element;
if (path.isAbsolute(containerPath)) {
containerPath = path.normalize(containerPath);
} else {
containerPath = path.normalize(path.join("/workspace/", containerPath));
}
if (path.isAbsolute(hostPath)) {
hostPath = path.normalize(hostPath);
} else {
hostPath = path.normalize(path.join(process.cwd(), hostPath));
}
extraBinds.push(`${hostPath}:${containerPath}`);
}
// 内存限制
let maxMemory = undefined;
if (instance.config.docker.memory) maxMemory = instance.config.docker.memory * 1024 * 1024;
@ -179,6 +202,8 @@ export default class DockerStartCommand extends InstanceCommand {
logger.info(`工作目录: ${cwd}`);
logger.info(`网络模式: ${instance.config.docker.networkMode}`);
logger.info(`端口映射: ${JSON.stringify(publicPortArray)}`);
if(extraBinds.length > 0)
logger.info(`额外挂载: ${JSON.stringify(extraBinds)}`);
logger.info(`网络别名: ${JSON.stringify(instance.config.docker.networkAliases)}`);
if (maxMemory) logger.info(`内存限制: ${maxMemory} MB`);
logger.info(`类型: Docker 容器`);
@ -201,7 +226,7 @@ export default class DockerStartCommand extends InstanceCommand {
ExposedPorts: exposedPorts,
HostConfig: {
Memory: maxMemory,
Binds: [`${cwd}:/workspace/`],
Binds: [`${cwd}:/workspace/`, ...extraBinds],
AutoRemove: true,
CpusetCpus: cpusetCpus,
CpuPeriod: cpuPeriod,

View File

@ -55,6 +55,7 @@ export default class InstanceConfig {
containerName: "",
image: "",
ports: [],
extraVolumes: [],
memory: null,
networkMode: "bridge",
networkAliases: [],

View File

@ -129,6 +129,7 @@ export default class Instance extends EventEmitter {
configureEntityParams(this.config.docker, cfg.docker, "image", String);
configureEntityParams(this.config.docker, cfg.docker, "memory", Number);
configureEntityParams(this.config.docker, cfg.docker, "ports");
configureEntityParams(this.config.docker, cfg.docker, "extraVolumes");
configureEntityParams(this.config.docker, cfg.docker, "maxSpace", Number);
configureEntityParams(this.config.docker, cfg.docker, "io", Number);
configureEntityParams(this.config.docker, cfg.docker, "network", Number);

View File

@ -27,6 +27,7 @@ export interface IDockerConfig {
image: string;
memory: number; //以字节为单位的内存限制。
ports: string[];
extraVolumes: string[];
maxSpace: number;
network: number;
io: number;