Skip to content

[JENKINS-39746] Add Container.exec to run a command in a running container#79

Open
hogarth-sv wants to merge 1 commit intojenkinsci:masterfrom
hogarth-sv:jenkins_26178
Open

[JENKINS-39746] Add Container.exec to run a command in a running container#79
hogarth-sv wants to merge 1 commit intojenkinsci:masterfrom
hogarth-sv:jenkins_26178

Conversation

@hogarth-sv
Copy link

This adds functionality to the Container object to be able to directly execute arbitrary commands in a running docker container.

@hogarth-sv hogarth-sv changed the title [Jenkins-26178] Add Container.exec to run a command in a running container [JENKINS-39746] Add Container.exec to run a command in a running container Nov 15, 2016
Hexadecimal ID of a running container.
</p>
</dd>
<dt><code>Container.exec([args, command])</code></dt>
Copy link
Member

Choose a reason for hiding this comment

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

There is only command, no args, and it is not optional, so I think you meant to write

Container.exec(command)

Choose a reason for hiding this comment

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

@hogarthj I think you should add the args attribute to exec in the same way Image.run does.

WorkflowRun b = story.j.assertBuildStatusSuccess(p.scheduleBuild2(0));
}
});
story.addStep(new Statement() {
Copy link
Member

Choose a reason for hiding this comment

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

Any particular reason you are doing two Jenkins restarts in the middle of this test? It seems gratuitous, since you are not testing resumption of builds.

Just fold these all into one step.

"node {\n" +
" writeFile file: 'Dockerfile', text: '# This is a test.\\n\\nFROM alpine\\nRUN chmod u+s /bin/busybox \\nENTRYPOINT [\\\"/bin/ping\\\" ] \\nCMD [\\\"localhost\\\"] \\n'\n" +
" def built = docker.build 'alpine-entrypoint-test'\n" +
" echo \"built ${built.id}\"\n" +
Copy link
Member

Choose a reason for hiding this comment

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

Why not just put this all into one build? Something along the lines of

node {
  // prep…
  def built = docker.build 'alpine-entrypoint-test'
  def con = built.run()
  try {
    def r = con.exec('echo 42')
    // verify result…
  } finally {
    con.stop()
  }
}

story.j.assertLogContains("the answer is 42", b);
DockerClient client = new DockerClient(new Launcher.LocalLauncher(StreamTaskListener.NULL), null, null);
String aetIID = client.inspect(new EnvVars(), "alpine-entrypoint-test", ".Id");
Fingerprint f = DockerFingerprints.of(aetIID);
Copy link
Member

Choose a reason for hiding this comment

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

Delete all this fingerprint checking; unrelated to the purpose of this test case. I suppose just copied from some older test.

}

public String exec(String command) {
docker.script.sh(script: "docker exec ${id} ${command}", returnStdout: true).trim()

Choose a reason for hiding this comment

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

This could be

public String exec(String args = '', String command) {
    docker.script.sh(script: "docker exec${args != '' ? ' ' + args : ''} ${id} ${command}", returnStdout: true).trim()
}

@bkmeneguello
Copy link

@hogarthj Any update on this?

@jglick
Copy link
Member

jglick commented Jun 16, 2017

I am not soliciting any new DSL functions. It is impossible to maintain the ones we already have.

@robhunt3r
Copy link

Any update on this? It would be good to have any way to actually run things in a running container without need of start/stop a new container each time... >.<

@smekkley
Copy link

Any update on this? Or similar feature available in Declarative Pipeline?
At the moment there is no way to run docker exec to override the entrypoint in a running docker instance, which forces us to run shell command again and again.

@ultimaweapon
Copy link

Any updates on this? It so ugly to do sh 'docker exec' instead of con.exec().

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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants