Docker scale with deterministic port binding : <p>I would like to scale a <code>wildfly</code> container having exposed multiple ports with deterministic results.</p>

<p><strong>docker-compose.yml</strong></p>

<pre><code>version: '3'
services:
  wildfly-server:
    build:
      context: .
      dockerfile: Dockerfile
      args:
        admin_user: admin
        admin_password: admin
    deploy:
      resources:
          limits:
            memory: 1.5G
            cpus: "1.5"
    restart: always
    ports:
      - "8000-8099:8080"
      - "8100-8199:9990"
      - "8200-8299:8787"
    expose:
      - "8080"
      - "9990"
      - "8787"
</code></pre>

<p><strong>Dockerfile</strong></p>

<pre><code>FROM jboss/wildfly:16.0.0.Final

# DOCKER ENV VARIABLES
ENV WILDFLY_HOME /opt/jboss/wildfly
ENV STANDALONE_DIR ${WILDFLY_HOME}/standalone
ENV DEPLOYMENT_DIR ${STANDALONE_DIR}/deployments
ENV CONFIGURATION_DIR ${STANDALONE_DIR}/configuration

RUN ${WILDFLY_HOME}/bin/add-user.sh ${admin_user} ${admin_password} --silent

# OPENING DEBUG PORT
RUN rm ${WILDFLY_HOME}/bin/standalone.conf
ADD standalone.conf ${WILDFLY_HOME}/bin/

# SET JAVA ENV VARS
RUN rm ${CONFIGURATION_DIR}/standalone.xml
ADD standalone.xml ${CONFIGURATION_DIR}/
</code></pre>

<p><strong>Command to start</strong></p>

<pre><code>docker-compose up --build --force-recreate --scale wildfly-server=10
</code></pre>

<p>It almost works as I want to, but there is some port discrepancy. When I create the containers, I want them to have incremental ports for each container to be exposed as follows:</p>

<pre><code>machine_1 8001, 8101, 82001
machine_2 8002, 8102, 82002
machine_3 8003, 8103, 82003 
</code></pre>

<p>But what I get as a result is not deterministic and looks like this:</p>

<pre><code>machine_1 8001, 8102, 82003
machine_2 8002, 8101, 82001
machine_3 8003, 8103, 82002 
</code></pre>

<p>The problem is that every time I run the compose up command, the ports are different for each container.</p>

<p>Example output:</p>

<pre><code>CONTAINER ID  COMMAND                  CREATED             STATUS              PORTS                                                                    NAMES
0232f24fbca4  "/opt/jboss/wildfly/…"   5 minutes ago       Up 5 minutes        0.0.0.0:8028-&gt;8080/tcp, 0.0.0.0:8231-&gt;8787/tcp, 0.0.0.0:8126-&gt;9990/tcp   wildfly-server_7
13a6a365a552  "/opt/jboss/wildfly/…"   5 minutes ago       Up 5 minutes        0.0.0.0:8031-&gt;8080/tcp, 0.0.0.0:8230-&gt;8787/tcp, 0.0.0.0:8131-&gt;9990/tcp   wildfly-server_10
bf8260d9874d  "/opt/jboss/wildfly/…"   5 minutes ago       Up 5 minutes        0.0.0.0:8029-&gt;8080/tcp, 0.0.0.0:8228-&gt;8787/tcp, 0.0.0.0:8129-&gt;9990/tcp   wildfly-server_6
3d58f2e9bdfe  "/opt/jboss/wildfly/…"   5 minutes ago       Up 5 minutes        0.0.0.0:8030-&gt;8080/tcp, 0.0.0.0:8229-&gt;8787/tcp, 0.0.0.0:8130-&gt;9990/tcp   wildfly-server_9
7824a73a09f5  "/opt/jboss/wildfly/…"   5 minutes ago       Up 5 minutes        0.0.0.0:8027-&gt;8080/tcp, 0.0.0.0:8227-&gt;8787/tcp, 0.0.0.0:8128-&gt;9990/tcp   wildfly-server_3
85425462259d  "/opt/jboss/wildfly/…"   5 minutes ago       Up 5 minutes        0.0.0.0:8024-&gt;8080/tcp, 0.0.0.0:8224-&gt;8787/tcp, 0.0.0.0:8124-&gt;9990/tcp   wildfly-server_2
5be5bbe8e577  "/opt/jboss/wildfly/…"   5 minutes ago       Up 5 minutes        0.0.0.0:8026-&gt;8080/tcp, 0.0.0.0:8226-&gt;8787/tcp, 0.0.0.0:8127-&gt;9990/tcp   wildfly-server_8
2512fc0643a3  "/opt/jboss/wildfly/…"   5 minutes ago       Up 5 minutes        0.0.0.0:8023-&gt;8080/tcp, 0.0.0.0:8223-&gt;8787/tcp, 0.0.0.0:8123-&gt;9990/tcp   wildfly-server_5
b156de688dcb  "/opt/jboss/wildfly/…"   5 minutes ago       Up 5 minutes        0.0.0.0:8025-&gt;8080/tcp, 0.0.0.0:8225-&gt;8787/tcp, 0.0.0.0:8125-&gt;9990/tcp   wildfly-server_4
3e9401552b0a  "/opt/jboss/wildfly/…"   5 minutes ago       Up 5 minutes        0.0.0.0:8022-&gt;8080/tcp, 0.0.0.0:8222-&gt;8787/tcp, 0.0.0.0:8122-&gt;9990/tcp   wildfly-server_1
</code></pre>

<p><strong><em>Question</em></strong></p>

<p>Is there any way to make the port distribution deterministic? Like disable parallel running to have serial checks on the available ports or any other method? The only alternative I found is to have a <code>yml</code>  template and generate all the necessary files (like 10 if I need 10 containers etc). Are there any alternative solutions?</p>
