How to start a docker container using fabric8 java docker client

293 views
Skip to first unread message

Kasun de Silva

unread,
Oct 24, 2016, 3:04:21 AM10/24/16
to fabric8
Hi, I need to do the $subject.

Do you guys point me to any examples?

At the moment I was able to do docker pull, docker push, tag and build operations[1].

I want to start a docker container can you guys point me to any samples.


Thanks,
Kasun

Kasun de Silva

unread,
Oct 24, 2016, 5:16:35 AM10/24/16
to fabric8
I tried following but no luck,

ContainerCreateResponse containerCreateResponse = dockerClient.container().createNew()
.withImage(imageRepoUrl + ":" + imageTag)
.done();


Can someone provide an example? 

Kurt Stam

unread,
Oct 24, 2016, 4:50:56 PM10/24/16
to Kasun de Silva, fabric8
Hi Kasun, are you executing this code inside a docker container? I'm asking because only 'privileged' docker containers can start other containers, and this brings about sone security issues. It's therefor discouraged to do so. If possible you should solve your problem another way.

cheers,
--Kurt
--
You received this message because you are subscribed to the Google Groups "fabric8" group.
To unsubscribe from this group and stop receiving emails from it, send an email to fabric8+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Kasun de Silva

unread,
Oct 25, 2016, 12:16:03 AM10/25/16
to Kurt Stam, fabric8
Hi Kurt,

Thanks for your response. No I'm not executing this code inside a docker container. I just want to execute docker api for run a docker image from and existing image. 

Following is a working example of a docker push I have written using fabric8 client. I need to do the same for docker run. Can you guys point me to an example?
public void pullDockerImage(String imageRepoUrl, String imageTag) throws AppCloudException {

if (log.isDebugEnabled()) {
log.debug("Docker image pull triggered for repo : " + imageRepoUrl + " with tag : " + imageTag);
}

final boolean[] dockerStatusCheck = new boolean[1];
dockerStatusCheck[0] = false;
try {
handle = dockerClient.image().withName(imageRepoUrl).pull()
.usingListener(new EventListener() {
@Override
public void onSuccess(String message) {
log.info("Pull Success:" + message);
pullDone.countDown();
dockerStatusCheck[0] = true;

}

@Override
public void onError(String message) {
log.error("Pull Failure:" + message);
pullDone.countDown();
}

@Override
public void onEvent(String event) {
log.info(event);
}
}).withTag(imageTag).fromRegistry();
pullDone.await();

} catch (InterruptedException e) {

String msg = "Error occurred while pulling docker image " + imageRepoUrl + " with tag : " + imageTag;
throw new AppCloudException(msg, e);
} finally {
try {
handle.close();
} catch (IOException e) {
log.warn("Error occurred while closing output handle after pulling docker image " + imageRepoUrl +
" with tag : " + imageTag);
}


--
Kasun de Silva


James Strachan

unread,
Oct 25, 2016, 1:17:21 AM10/25/16
to Kasun de Silva, Kurt Stam, fabric8
the fabric8 project uses kubernetes; so it doesn't usually run docker containers directly from Java code - as thats what kubernetes does for you.

You could noodle the docker-maven-plugin codebase which is pure docker

--
You received this message because you are subscribed to the Google Groups "fabric8" group.
To unsubscribe from this group and stop receiving emails from it, send an email to fabric8+unsubscribe@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--
James
-------
Red Hat

Twitter: @jstrachan
Email: james.s...@gmail.com
Blog: https://medium.com/@jstrachan/

open source microservices platform

Kasun de Silva

unread,
Oct 25, 2016, 1:22:33 AM10/25/16
to James Strachan, Kurt Stam, fabric8
I have seen this,

I gives the capability to invoke docker api direclty correct me if I'm wrong but thats how I understood it. 

Thanks,
Kasun
--
Kasun de Silva
Senior Software Engineer | Cloud TG

WSO2 Inc| Email : kas...@wso2.com | Mobile: +94 77 794 4260

Kasun de Silva

unread,
Oct 25, 2016, 5:39:40 AM10/25/16
to James Strachan, Ioannis Canellos, Kurt Stam, fabric8
Basically I need to execute these two api calls through fabric8 docker cient.  

curl -v -X POST -H "Content-Type: application/json" -d '{"Image": "tomcat:latest"}' http://localhost:2375/containers/create?name=hello

curl -v -X POST -H "Content-Type: application/json" -d http://localhost:2375/containers/hello/start?name=hello

Can you guys provide any sample code ?

James Strachan

unread,
Oct 25, 2016, 5:42:18 AM10/25/16
to Kasun de Silva, Ioannis Canellos, Kurt Stam, fabric8
the code I gave you is the only code I know of (which doesn't use the docker-client)

Ioannis Canellos

unread,
Oct 27, 2016, 4:41:30 AM10/27/16
to James Strachan, Kasun de Silva, Kurt Stam, fabric8
Hi Kasun,

I found a regression in the client which broke the create() operation. I fixed that and also added an full example that you will find handy: https://github.com/fabric8io/docker-client/pull/70/commits/4ae55decc91af9aa045f33b7032c64b5980cf39d

I'll cut a new release later today.
--
Ioannis Canellos

Twitter: iocanel

Kasun de Silva

unread,
Oct 27, 2016, 4:55:54 AM10/27/16
to Ioannis Canellos, James Strachan, Kurt Stam, fabric8
Thanks alot Ioannis!

Appreciate it.

Kasun de Silva

unread,
Oct 27, 2016, 5:00:15 AM10/27/16
to Ioannis Canellos, James Strachan, Kurt Stam, fabric8
Please let me know the working client version once you release.

Thanks,
Kasun
Reply all
Reply to author
Forward
0 new messages