Trying to find a creative solution to assign an unused host:container port while submitting docker run from a range of possible ports. I am very close to figuring it out and trying to see if below approach works smartly. So far, I have decided to do following:
$ docker ps -a --format "{{.Ports}}"
0.0.0.0:8780->8080/tcp
0.0.0.0:4022->4022/tcp, 0.0.0.0:5022->5022/tcp
0.0.0.0:4013->4013/tcp, 0.0.0.0:5013->5013/tcp, 0.0.0.0:6013->6013/tcp
$ docker ps -a --format "{{.Ports}}" | sed -re 's/^.*->[0-9][0-9]([0-9]+)\/tcp.*$/\1/'
80
13
-p 40${UUID}:40${UUID} \
-p 50${UUID}:50${UUID} \
-p 60${UUID}:60${UUID} \
Is it possible for a bash one-liner to achieve what I am trying to achieve? python approach will also work. If there is any better way to achieve above, Im keen to learn as well. Expected output for UUID below will return 2 digit value between 0-50 but not 13,22,80.
UUID=`docker ps -a --format "{{.Ports}}" | sed -re 's/^.*->[0-9][0-9]([0-9]+)\/tcp.*$/\1/' | <additional parsing logic>`
Much thanks in advance. Interesting that docker does not have a way to dynamically manage port assignments based on range so have to go this path.
UPDATE: The list of ports on host and container need to configured in the above way due to firewall limitations. On host, only specific range is opened for tcp. While, on container the same ports are assigned to specific conf values. Hence both host/container ports cannot be dynamically assigned without setting on environment.
This should produce the expected result :
UUID=$(docker ps -a --format "{{.Ports}}" | python -c 'import re,sys; last2 = set(int(m.group(2)) for m in (re.match(r"(\d+\.){3}\d+:\d*(\d{2})", line) for line in sys.stdin) if m); print(next((str(i).zfill(2) for i in range(51) if i not in last2), ""))')
echo $UUID