patternyamldockerMinor
docker-compose multiple services in a loop
Viewed 0 times
dockerloopservicescomposemultiple
Problem
Trying to optimize definition of multiple services with
Here is the original, "dumb" variant of the
Is there some sort of an iterator to compact this to just one block like the following?
docker-compose.ymlHere is the original, "dumb" variant of the
docker-compose.yml:version: '3'
services:
s1:
build: .
ports:
- "5001:9000"
volumes:
- ../www1:/var/www
s2:
build: .
ports:
- "5002:9000"
volumes:
- ../www2:/var/www
s3:
build: .
ports:
- "5003:9000"
volumes:
- ../www3:/var/wwwIs there some sort of an iterator to compact this to just one block like the following?
version: '3'
services:
for i in 1,2,3:
s$i:
build: .
ports:
- "500$i:9000"
volumes:
- ../www$i:/var/wwwSolution
Not in pure Docker Compose. If you have a plain-text templating engine you like (Mustache, Jinja, m4, ...) you can ask it to do this for you.
You tagged this as shell-script, and Compose can take the YAML configuration on stdin, so in principle one option is to write a shell script, and then pipe that into
`#!/bin/sh
buildComposeYaml() {
cat
(Note that YAML is indentation-sensitive, and the script is mixing indentation for the script itself, the YAML embedded in the script, and the end-of-heredoc markers; so this will work for a simple tool but it wouldn't be my long-term choice.)
You tagged this as shell-script, and Compose can take the YAML configuration on stdin, so in principle one option is to write a shell script, and then pipe that into
docker-compose:`#!/bin/sh
buildComposeYaml() {
cat
(Note that YAML is indentation-sensitive, and the script is mixing indentation for the script itself, the YAML embedded in the script, and the end-of-heredoc markers; so this will work for a simple tool but it wouldn't be my long-term choice.)
Context
StackExchange DevOps Q#11318, answer score: 5
Revisions (0)
No revisions yet.