Comment out docker-compose block with bash script

huangapple go评论82阅读模式
英文:

Comment out docker-compose block with bash script

问题

我正在为本地开发制作一个使用Docker化应用的项目。我的公司有3个不同的域名,每个域名都有一个包含5个服务的docker-compose文件(共15个项目)。

如果我的项目用户只想部署他们域名下的一个服务,或者其他域名下的两个项目,我就需要在其他docker-compose文件中注释掉不需要部署的服务。

所以我的问题是,如何使用bash脚本注释掉docker-compose(Go)文件中的代码块?我希望能够根据上下文选择要注释的行。例如,在下面的示例中,我想注释掉ap2-php-fpm部分。我不能使用临时解决方案,因为还会有更多的项目进来。我必须使用bash脚本来修改Go语言脚本。

演示:

version: '3.3'

services:
  app-php-fpm:
    container_name: app
    build:
      context: ${src}/
    volumes:
        - $path:path
    networks:
      general-nt:
        aliases:
        - app
    expose:
        - "9000"

  ap2-php-fpm:
    container_name: app
    build:
      context: ${src}/
    volumes:
        - $path:path
    networks:
      general-nt:
        aliases:
        - app
    expose:
        - "9000"

networks:
  general-nt:
    external: true

我希望使用bash脚本将该文件修改为以下内容:

version: '3.3'

services:
  app-php-fpm:
    container_name: app
    build:
      context: ${src}/
    volumes:
        - $path:path
    networks:
      general-nt:
        aliases:
        - app
    expose:
        - "9000"

  # ap2-php-fpm:
  #   container_name: app
  #   build:
  #     context: ${src}/
  #   volumes:
  #       - $path:path
  #   networks:
  #     general-nt:
  #       aliases:
  #       - app
  #   expose:
  #       - "9000"

networks:
  general-nt:
    external: true
英文:

I am making a project for local development with dockerized apps. I have 3 different domain on my company that each domain has one docker-compose file with 5 services. (15 projects)

If User of my project wants to deploy only 1 service of their domain or/and 2 of the other domains projects, I have to comment out services in other docker-compose files that dont want to be deployed.

So my question is How can i comment out docker-compose(Go) files block with bash script? I want to choose the lines with their context. For example in below example i want to comment out ap2-php-fpm section. I cant make a work around solution because more projects incoming. I have to intervene go language script with bash script.

Demonstration

version: '3.3'

services:
  app-php-fpm:
    container_name: app
    build:
      context: ${src}/
    volumes:
        - $path:path
    networks:
      general-nt:
        aliases:
        - app
    expose:
        - "9000"

  ap2-php-fpm:
    container_name: app
    build:
      context: ${src}/
    volumes:
        - $path:path
    networks:
      general-nt:
        aliases:
        - app
    expose:
        - "9000"

networks:
  general-nt:
    external: true

I want to make this file as below with bash script.

version: '3.3'

services:
  app-php-fpm:
    container_name: app
    build:
      context: ${src}/
    volumes:
        - $path:path
    networks:
      general-nt:
        aliases:
        - app
    expose:
        - "9000"

  # ap2-php-fpm:
  #   container_name: app
  #   build:
  #     context: ${src}/
  #   volumes:
  #       - $path:path
  #   networks:
  #     general-nt:
  #       aliases:
  #       - app
  #   expose:
  #       - "9000"

networks:
  general-nt:
    external: true

答案1

得分: 0

对于许多实际用途来说,只需使用特定的服务名称运行 docker-compose up 可能已经足够了。如果你运行以下命令:

docker-compose up -d app-php-fpm

它将启动命令行上的服务以及它所依赖的任何其他服务,但不会启动其他服务。这样就避免了需要注释掉 YAML 文件的部分内容的需要。你可以以正常方式与容器进行交互。

英文:

For many practical purposes, it may be enough to run docker-compose up with specific service names. If you run

docker-compose up -d app-php-fpm

it will start the service(s) on the command line, and anything it depends_on:, but not anything else. That would avoid the need to comment out parts of the YAML file. You could otherwise interact with the containers normally.

huangapple
  • 本文由 发表于 2021年10月9日 06:50:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/69502421.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定