Enumerate port ranges into docker config #3558
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
When using
docker run
with a port range exposed, Docker enumerates every port within the range into theConfig.ExposedPorts
map but the ECS Agent just puts the range as a string into the config. This becomes a problem when containers themselves aren't exposing ports (the Dockerfile doesn't contain anEXPOSE
instruction for example) but customers are exposing ports using ECS task definitions and also using dynamic host port binding.For example, assume that a container was built without an
EXPOSE
instruction in the Dockerfile but the customer's task definition lists a container port range of 80-90 with the protocol being TCP. Currently, there would be no way of reporting the bound ports since"80-90/tcp"
gets added to theConfig.ExposedPorts
map and Docker seems to ignore anything that is not a singular port in the above map when it is reporting the host ports bound through theNetworkSettings.Ports
map.Implementation details
Using
nat.ParsePortRangeToInt()
, we get the start and the end of the port range. Once we have that, we individually build anat.Port
instance for each of the ports in the range and then we add them to the list of Docker exposed ports.Testing
Unit tests were run and manual testing was also done by building the new agent with the changes and then running tasks on the instance with the new agent running.
Manual testing results
Config.ExposedPorts
after$ docker run -d -p 9090 --name dr-sp public.ecr.aws/kulshres/simple-container
Config.ExposedPorts
after$ docker run -d -p 9090-9095 --name dr-pr public.ecr.aws/kulshres/ranges
Config.ExposedPorts
after running$ aws ecs run-task --task-definition SimpleContainer --launch-type EC2 --count 1
Config.ExposedPorts
after running$ aws ecs run-task --task-definition Ranges --launch-type EC2 --count 1
Config.ExposedPorts
after running$ aws ecs run-task --task-definition SimpleContainer --launch-type EC2 --count 1
and after making the changeConfig.ExposedPorts
after running$ aws ecs run-task --task-definition Ranges --launch-type EC2 --count 1
and after making the changeNew tests cover the changes: yes
Description for the changelog
Fix: enumerate port ranges into the docker config
Licensing
By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.