Question: Has anyone been able to work with the js-yaml library to write a docker compose like this?个
上下文:
我正在处理DotNet模板,并为我们的团队提供了一些预定义的应用程序架构模式.我想更进一步,为整个生态系统创建docker compose
个.
主要问题是我需要更改令牌并写入YAML.
我已经在模板中try 了以下两种方法,无法获得有效的YAML输出.
command: "./daprd -app-id ecosystemtemplate-${application} -app-port ${EXPOSED_PORT:-4443} --resources-path /app/config"
command: [ "./daprd", "-app-id", "ecosystemtemplate-${application}", "-app-port", "${EXPOSED_PORT:-4443}", "--resources-path", "/app/config"
我如何让js-yaml
库尊重被引用的项目,并应该保留引用?
取以上command
个YAML并通过json2yaml.com运行,YAML-&>JSON和JSON-&>YAML看起来都是正确的.为了重新创建它,我首先将模板YAML复制到UI中,然后复制json并再次粘贴,从而更新YAML输出,这看起来是正确的.
如果我使用NodeJS运行它,我在YAML中会得到不正确的输出.image
、command
和network_mode
都需要引号.我已经用了太多的方式来列出这一点.
当PoC转移到产品就绪的企业级应用程序时,PoC将变得更加参与.
示例:
根据模板,我创建了一个Solution.json
文件,其中包含生态系统模板使用的信息.
以下代码用于读取Solution.json
async function createDockerComposeYaml () {
console.log("Creating Docker Compose");
const fileName = 'docker-compose.yml';
const applicationDescriptor = '${application}';
await (async () => {
try {
await fs.unlink(fileName);
} catch (e) {
// Swallow the error.
// Normally happens when the file does not exist the first time.
}
})();
try {
const dockerComposeYaml = await fs.readFile('./docker-templates/compose/Docker-compose-template.yml', 'utf-8');
const serviceYamlTokenized = await fs.readFile('./docker-templates/compose/Docker-compose-service-template.yml', 'utf-8');
const daprSidecarYamlTokenized = await fs.readFile('./docker-templates/compose/Docker-compose-service-dapr-sidecar-template.yml', 'utf-8');
let dockerCompose = YAML.load(dockerComposeYaml);
solutions.forEach(solution => {
// Build the service for the application.
let serviceYaml = serviceYamlTokenized.replaceAll('${application}', solution.name.toLowerCase());
serviceYaml = serviceYaml.replace('${dockerfile}', solution.dockerfile);
const service = YAML.load(serviceYaml);
// Build the service for the application dapr sidecar.
const serviceSidecarYaml = daprSidecarYamlTokenized.replaceAll('${application}', solution.name.toLowerCase());
const serviceSidecar = YAML.load(serviceSidecarYaml);
if (dockerCompose.services === null) {
dockerCompose.services = [];
}
dockerCompose.services.push(service);
dockerCompose.services.push(serviceSidecar);
});
writeFile(fileName, YAML.dump(dockerCompose, { 'forceQuotes': true }));
} catch(e) {
console.log(e);
}
}
内容:
Solution.json
{
"name": "DddTemplate",
"template": "ddd",
"dockerfile": "DddTemplate/src/DddTemplate.Api/Dockerfile",
"launchSettings": "DddTemplate/src/DddTemplate.Api/Properties/launchSettings.json"
}
Docker-compose-template.yml
version: '3.7'
name: EcosystemTemplate
volumes:
services:
Docker-compose-service-template.yml
${application}:
image: ${DOCKER_REGISTRY-}${application}:${IMAGE_TAG}
build:
context: .
dockerfile: "${dockerfile}"
args:
- IMG_REGISTRY=${REGISTRY:-mcr.microsoft.com}
environment:
- ASPNETCORE_URLS=${ASPNETCORE_URLS:-https://*:4443}
ports:
- "43301:${EXPOSED_PORT:-4443}"
volumes:
- ~/.aspnet/https:/https:ro
Docker-compose-service-dapr-sidecar-template.yml
${application}-dapr:
image: "daprio/daprd:latest"
command: "./daprd -app-id ${application} -app-port ${EXPOSED_PORT:-4443} --resources-path /app/config"
depends_on:
- ${application}
network_mode: "service:${application}"
volumes_from:
- ${application}