Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[docker] Helper function to get exposed host ports for -P flag #807

Merged

Conversation

ThelonKarrde
Copy link
Contributor

@ThelonKarrde ThelonKarrde commented Mar 13, 2021

Relates to: docker module

The problem:
If you have at least two concurrent builds of the same image with exposed ports to host on the same build agent (on whatever CI tool you use) you unable to use direct port mapping. So you must use -P flag for the run command to get random host port assignments and then use the inspect function to get a list of exposed and mapped ports and choose the port that you want to test from this list. However, if you have more than two ports you might not know under what index will be this port or you need to check Dockerfile if you changed the order and make changes to test. Otherwise, you can write your own helper to search in the port list and migrated it from one project/test to another.
It's sort of painful to maintain and uncomfortable to use.

The solution:
I simply added a helper for host port search to the ContainerInspect structure as a function.
I'm not sure is it a convenient place for this helper, so let me know if I need to move it somewhere else in the package or it might just doesn't make sense :)

p.s. I also write a test to make sure that the helper works as expected, as an "error code" I'm using 0 value.

@ThelonKarrde ThelonKarrde changed the title [docker] Function to get exposed host ports for -P flag [docker] Helper function to get exposed host ports for -P flag Mar 13, 2021
Copy link
Contributor

@yorinasub17 yorinasub17 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your contribution! The LGTM, except for one doc change. Will kick off a build shortly so you get one test cycle.

modules/docker/inspect.go Outdated Show resolved Hide resolved
Copy link
Contributor

@yorinasub17 yorinasub17 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@yorinasub17 yorinasub17 merged commit 6f5944e into gruntwork-io:master Mar 17, 2021
@ThelonKarrde ThelonKarrde deleted the feature/docker_port_selector branch March 17, 2021 15:27
This was referenced Mar 17, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants