Docker Community Forums

Share and learn in the Docker community.

An iterator available in docker-compose 3.4?

I am using docker-compose healthcheck to test network connectivity between two network segments. I have iperf running in server mode within containers on one host and iperf in client mode within containers on a different host. Here is sample docker-compose healthcheck configuration on the client side:

    test: iperf3 -V --logfile iperf3_client_log -c -p 5200
    interval: 60s
    timeout: 40s
    start_period: 5s
    retries: 1

Each client container starts and connects to a different iperf server port (i.e. 5200-5209). This is easy enough if I create each container as a separate service within my compose file and hard code the port, however, that isn’t very scalable. I’d like to be able to pass healthcheck something like:

iterator: 5200-5209
test: iperf3 -V --logfile iperf3_client_log -c -p $iterator

and have docker-compose iterate through the port numbers exactly like it does if I specify a port range using the “ports” configuration option when using --scale in my “up” command. Is there an iterator variable available in docker compose I can use for this purpose?
Thank you,

You should be able to do a port range in compose with something like:

- 5200-5209:5200

Make sure you’re running compose 1.18 as this behavior was fixed in that release:

The ports command you reference will configure port mapping for the container … I don’t need the container ports mapped (external->internal) … just a way to change the port number in my test command as --scale iterates through starting containers. My client containers come up with their own ips, so don’t need to map ports.
I use port mapping on the server side, exactly as you mention.

Here is another example of someone looking for an iterator: Pass incremental variable to CMD for each scaled service in docker-compose

The reference to the docker compose file does not include any reference to such a thing.
Though, Swarm services are able to use template placeholder in some of the configuration elements to individualize each replica.

You might assign {{.Task.Slot}} to an env and use it in your entrypoint script to add the task slot on top of your base port.

1 Like