Skip to content

[JENKINS-34207] Display ports exposed and mapped by container#40

Merged
jglick merged 4 commits intojenkinsci:masterfrom
alecharp:JENKINS-34207
Jun 29, 2016
Merged

[JENKINS-34207] Display ports exposed and mapped by container#40
jglick merged 4 commits intojenkinsci:masterfrom
alecharp:JENKINS-34207

Conversation

@alecharp
Copy link
Copy Markdown
Member

@alecharp alecharp commented Apr 13, 2016

JENKINS-34207

Can be useful when the container is started with random port mapping.

@reviewbybees

@ghost
Copy link
Copy Markdown

ghost commented Apr 13, 2016

This pull request originates from a CloudBees employee. At CloudBees, we require that all pull requests be reviewed by other CloudBees employees before we seek to have the change accepted. If you want to learn more about our process please see this explanation.

@jglick
Copy link
Copy Markdown
Member

jglick commented Apr 13, 2016

How would you use this in a script? Seems like it would be more useful to run docker-inspect to return the mapping in a machine-readable format.

@alecharp
Copy link
Copy Markdown
Member Author

It could be useful for:

def app = docker.build 'foo/bar:${env.BUILD_ID}
def container = app.run '-P'
try {
  def ip = container.port 8080
  input "http://${ip}"
} finally {
  container.stop
}

it could be used the same way to trigger integration-tests with the port, with a curl or something like that.

@alecharp
Copy link
Copy Markdown
Member Author

So I looked at the docker-inspect. We could use it but the syntax would be

docker inspect --format='{{(index (index .NetworkSettings.Ports "8080/tcp") 0).HostIp}}:{{(index (index .NetworkSettings.Ports "8080/tcp") 0).HostPort}}' ${container.id}

to have the same output and we would still have to write it to a file and then def ip = readFile 'the-file'.

@alecharp
Copy link
Copy Markdown
Member Author

I just saw that my code was totally incorrect. I pushed a new one which is valid.

@jenkinsadmin
Copy link
Copy Markdown
Member

Thank you for this pull request! Please check this document for how the Jenkins project handles pull requests.

@alecharp
Copy link
Copy Markdown
Member Author

@jglick don't you think it worth include this in the plugin? Redirecting the output of the docker port command is more a hack than an integration, no?

@ndeloof
Copy link
Copy Markdown
Contributor

ndeloof commented Apr 19, 2016

LGTM
From a usage point of view, what's the benefit vs using sh 'docker port $id 8080' ?

@alecharp
Copy link
Copy Markdown
Member Author

The issue with the docker port command is that you need to write the output to a file, read the file to assign it to a variable, and then remove the file if you want to clean up. So

def container = docker.build('foo/bar:123').run('-P')
def ip = container.port 8080

is currently written as

def container = docker.build('foo/bar:123').run('-P')
sh "docker port ${container.id} 8080 > tmp-file"
def ip = readFile('tmp-file').trim()
sh 'rm tmp-file'

with the possibility that the tmp-file is existing and used for the build.

@ndeloof
Copy link
Copy Markdown
Contributor

ndeloof commented Apr 19, 2016

ok, then a better fix would be to get some descent support for returning value from a sh pipeline step, equivalent to bash foo = $(some command)

anyway, still LGTM as improvement to docker-pipeline DSL

@jglick
Copy link
Copy Markdown
Member

jglick commented May 12, 2016

returning value from a sh pipeline step

Yes, JENKINS-26133 is well known.

Anyway I must still be missing something fundamental. The newly added method merely runs docker port and leaves the output—which is formatted for human consumption—in the build log, where it cannot be used by the script. So this seems totally pointless.

If you have some actual use case, write a functional test proving your addition works.

@jglick
Copy link
Copy Markdown
Member

jglick commented May 12, 2016

formatted for human consumption

Well, at least that is what ports produces. port produces compact output. But again not in a way that the script could consume it.

@alecharp
Copy link
Copy Markdown
Member Author

Well, at least that is what ports produces. port produces compact output. But again not in a way that the script could consume it.

I agree for ports, we could remove it as not script-readable. However, for port I disagree. The output is 123.123.123.123:1234 which can be used to provide container entry point to reach HTTP API required for selenium tests or manual validation through an input step.

@jglick
Copy link
Copy Markdown
Member

jglick commented Jun 22, 2016

So, delete ports, and fix port to actually return a value, rather than printing something to the log (and write a test proving it).

" def img = docker.image('httpd:2.4.12')\n" +
" def container = img.run('-p 12345:80')\n" +
" def port = container.port(80)\n" +
" container.stop()\n" +
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

🐜 safer to use

def port = img.withRun('-p 12345:80') {c -> c.port(80)}

@jglick
Copy link
Copy Markdown
Member

jglick commented Jun 22, 2016

🐝 but

Merge conflict in src/test/java/org/jenkinsci/plugins/docker/workflow/DockerDSLTest.java

@alecharp
Copy link
Copy Markdown
Member Author

I'm trying to resolve the conflict without adding useless (to this PR) commit in the branch

@jglick
Copy link
Copy Markdown
Member

jglick commented Jun 22, 2016

resolve the conflict without adding useless (to this PR) commit

Not sure what you mean. You need merely

git merge master

resolve, commit, and push. What is the problem?

@jglick jglick closed this Jun 22, 2016
@jglick jglick reopened this Jun 22, 2016
@jglick
Copy link
Copy Markdown
Member

jglick commented Jun 22, 2016

And please avoid rebasing. It makes it impossible to see your incremental changes.

@jglick
Copy link
Copy Markdown
Member

jglick commented Jun 22, 2016

🐝

@alecharp
Copy link
Copy Markdown
Member Author

merging would have "imported" the commits on master here. Those commits would have nothing to do with the PR, when rebase just move the current branch to the new head of master. I understand it makes the incremental changes impossible to watch, but the commit graph in the end if much more easy to ready imo.

@jglick
Copy link
Copy Markdown
Member

jglick commented Jun 29, 2016

merging would have "imported" the commits on master here

No it does not. GitHub displays only those commits which are specific to the branch (not in the base branch).

@jglick jglick merged commit 87cd55d into jenkinsci:master Jun 29, 2016
@alecharp alecharp deleted the JENKINS-34207 branch June 30, 2016 08:19
@jglick jglick changed the title Display ports exposed and mapped by container [JENKINS-34207] Display ports exposed and mapped by container Jul 25, 2016
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.

4 participants