Publishing to random ports is useful as then you can have multiple containers running at the same. We need this for our CI -setup. But there's also requirement for accessing container from outside, so we need to resolve the port published by -P in build scripts.
"docker inspect" is a command which can be used for this. It takes "--format=template" parameter, which can be used to output information about container.
So following bash script resolves public port for given container name and exposed port.
#!/bin/bash
set -o nounset
set -o errexit
function resolvePort() {The magic happens in
local container=$1
local exposedPort=$2
local port=$(docker inspect --format='{{range $p, $conf := .NetworkSettings.Ports}}{{if eq $p "'$exposedPort'/tcp"}}{{(index $conf 0).HostPort}}{{end}} {{end}}' $container)
echo $port
}
{{range $p, $conf := .NetworkSettings.Ports}}{{if eq $p "'$exposedPort'/tcp"}}{{(index $conf 0).HostPort}}{{end}} {{end}}In easier to read format:
{{range $p, $conf := .NetworkSettings.Ports}}The "{{range $p, $conf := .NetworkSettings.Ports}}" iterates over ports configuration. It is like map, and one key-value -pair looks something like this
{{if eq $p "'$exposedPort'/tcp"}}
{{(index $conf 0).HostPort}}
{{end}}
{{end}}
"80/tcp": [
{
"HostIp": "0.0.0.0",
"HostPort": "49101"
}
]
$p is the key and $conf is the value. $p is the exposed port and its value is something like "80/tcp".
Then there's {{if eq $p "'$exposedPort'/tcp"}}, which is simple comparison.
The value of $conf is a array, and in this use case, we just need the first value (also there is just one). So in
{{(index $conf 0).HostPort}}(index $conf 0) gives just that, and then (index $conf 0).HostPort returns 49101.
This can be then used
readonly publicPort=$(resolvePort containerName 80)We might have been able to avoid this by using another container for tests and doing container linking, but this seems to work okay. And I wanted to learn about docker inspect --format :)
curl localhost:$publicPort
Ei kommentteja :
Lähetä kommentti