Discovery agent to find a docker exposed port
10K+
A simple in go written agent that exposes a minimal REST api so a container can find the exposed port for a container port. The agent is just a workaround for the problem mentioned in https://github.com/docker/docker/issues/3778
Start the agent
docker run -p 5555:8080 -v /var/run/docker.sock:/var/run/docker.sock \
--name docker-discovery-agent npalm/docker-discovery-agent
Test the agent
export DOCKERHOST=$(ifconfig | grep -E "([0-9]{1,3}\.){3}[0-9]{1,3}" | \
grep -v 127.0.0.1 | awk '{ print $2 }' | cut -f2 -d: | head -n1)
docker run -it -p 80 --add-host="dockerhost:$(DOCKERHOST)" --rm centos /bin/bash
Now you are on the shell of the container. You can look up the exposed port as follow.
# find the container id
CONTAINER_ID=$(cat /proc/self/cgroup | grep "cpu:/" | sed 's/\([0-9]\):cpu:\/docker\///g')
# request the port binding for mapped port 80
curl "http://dockerhost:5555/container/${CONTAINER_ID}/portbinding?port=80&protocol=tcp"
The response will look like:
[{"HostIp":"0.0.0.0","HostPort":"12345"}]
Only a minimal API is implemented
Once the service is running the health check should response with a 200 status code.
response: 200 OK
resource: <host:port>/container/{id}/portbinding?port={port}&protocol={protocol}
response: A JSON resprenting the exposded port for container with id
{
"HostIp": "",
"HostPort": ""
}
Content type
Image
Digest
Size
6.8 MB
Last updated
almost 7 years ago
docker pull npalm/docker-discovery-agent