이것이 내 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'
이제 우분투 서버에서 쉘 스크립트를 통해 일부 내용을 추가해야합니다. 그것이 가능한지 확실하지 않습니다.
nginx/links
존재하지 않는 경우에 새 요소 추가- 새로운
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에 대한 의견을 부탁드립니다.