Skip to content

Instantly share code, notes, and snippets.

@rickalm
Last active December 6, 2018 17:43
Show Gist options
  • Save rickalm/10e766c8904a67a219738ccd6b0207bf to your computer and use it in GitHub Desktop.
Save rickalm/10e766c8904a67a219738ccd6b0207bf to your computer and use it in GitHub Desktop.
How to discover Docker Public ports with AWS ECS
# When wanting to search for port 7000 (example) you would query the ECS agent endpoint searching for your docker
# container by its ID ($HOSTNAME). You can then parse the resulting json yourself, or use JQ to find the HostPort
# curl -sS http://172.17.0.1:51678/v1/tasks?dockerid=$HOSTNAME \
# | jq '.Containers[].Ports[] | select (.ContainerPort == 7000) | .HostPort'
# The following will create env vars identifying the public port numbers for mapped private ports
# (example assuming docker is exposing 80,443 and 8080)
#
# PUBLIC_PORT_80=32777
# PUBLIC_PORT_80=32778
# PUBLIC_PORT_8080=32779
# If you have have changed your default docker network ip address range then you will need to replace the 172.17.0.1 with
# the address of your docker0 interface in the script below (or a non-standard ECS agent port replace 51678)
# Refactored to allow sourcing this file to utilize its lookup functions
fetch_private_ports() {
curl -sS http://172.17.0.1:51678/v1/tasks?dockerid=${HOSTNAME} \
| jq -r '.Containers[].Ports[].ContainerPort'
}
fetch_public_port() {
curl -sS http://172.17.0.1:51678/v1/tasks?dockerid=${HOSTNAME} \
| jq -r '.Containers[].Ports[] | select (.ContainerPort == '$1') | .HostPort'
}
for port in $(fetch_private_ports); do
publicport=$(fetch_public_port ${port})
eval export PUBLIC_PORT_${port}=$publicport
echo export PUBLIC_PORT_${port}=$publicport
done
@rickalm
Copy link
Author

rickalm commented Dec 4, 2018

you can either download and use this in your own code or eval this script directly which is why it also echo's its export commands besides eval'ing them. The following is intended as a one-liner

eval $(curl -sS 'https://gist.githubusercontent.com/rickalm/10e766c8904a67a219738ccd6b0207bf/raw/0921dca6142a0afd77f696dd7ba15f598ef637d2/expose_ecs_public_ports.sh' | bash)

or

source <(curl -sS 'https://gist.githubusercontent.com/rickalm/10e766c8904a67a219738ccd6b0207bf/raw/0921dca6142a0afd77f696dd7ba15f598ef637d2/expose_ecs_public_ports.sh')

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment