쉘 스크립트를 통해 yml 파일을 수정할 수 있습니까? – ‘443:443’ volumes:

이것이 내 docker-compose.yml의 모습입니다.

nginx:
  container_name: 'nginx'
  image: 'nginx:1.11'
  restart: 'always'
  ports:
    - '80:80'
    - '443:443'
  volumes:
    - '/opt/nginx/conf.d:/etc/nginx/conf.d:ro'
  links:
    - 'anything'

이제 우분투 서버에서 쉘 스크립트를 통해 일부 내용을 추가해야합니다. 그것이 가능한지 확실하지 않습니다.

  1. nginx/links존재하지 않는 경우에 새 요소 추가
  2. 새로운 newthing블록이없는 경우 블록 추가

새로운 내용은 다음과 같아야합니다.

nginx:
  container_name: 'nginx'
  image: 'nginx:1.11'
  restart: 'always'
  ports:
    - '80:80'
    - '443:443'
  volumes:
    - '/opt/nginx/conf.d:/etc/nginx/conf.d:ro'
    - '/etc/letsencrypt:/etc/letsencrypt'
  links:
    - 'anything'
    - 'newthing'

newthing:
  container_name: foo
  image: 'newthing:1.2.3'
  restart: always
  hostname: 'example.com'



답변

쉘 스크립트에서 직접 수행하지 않고 다른 언어를 사용하는 것이 좋으면 Perl, Python 등을위한 많은 yaml 라이브러리가 있습니다.

또 다른 옵션은 명령 행 yaml processor 를 설치하고 쉘 스크립트에서 호출하는 것입니다.


답변

이 사용 사례를 해결하기 위해 https://stedolan.github.io/jq/ 주위 래퍼 인 https://github.com/kislyuk/yq를 작성했습니다 .


답변

yaml_cli ( https://github.com/Gallore/yaml_cli )를 작성 하여 정확히 필요한 것을 수행했습니다. 파이썬을 기반으로합니다. 이것은 당신의 예제를위한 문법 일 것입니다 :

yaml_cli \
  -f docker-compose.yml \                # read from and save to file
  --list-append \                        # flag to append to lists instead of replacing existing values
  -s nginx:links newthing \              # add a value of type string; here you need --list-append
  -s newthing:container_name foo \       # key 'newthing' is created automatically
  -s newthing:image 'newthing:1.2.3' \   #
  -s newthing:restart always \           #
  -s newthing:hostname 'example.com'     #

yaml_cli에 대한 의견을 부탁드립니다.


답변

이 작업을 수행하려는 이유는 docker-compose 파일을 수정하는 것이므로 다른 대안은 JSON 파일을 사용하는 것입니다. Docker-compose는 이제 JSON 파일을 지원 합니다 . JSON의 명령 줄 조작에 대한 지원은 이미 매우 우수합니다 (예 : jq )


답변