How can I get the hostname of a container using docker-java?

903 Views Asked by At

I can create, start and retrieve an instance of a docker container like this:

CreateContainerResponse response = dockerClient.createContainerCmd(imageId).exec();
String containerId = response.getId();
dockerClient.startContainerCmd(containerId).exec()

Container container = dockerClient.listContainersCmd()
    .withIdFilter(Collections.singletonList(containerId))
    .exec()
    .get(0);

However, the Container object does not appear to expose the hostname.

I can also inspect a running container like this:

InspectContainerResponse response = dockerClient.inspectContainerCmd(container.getId()).exec()

Again, the response doesn't contain the hostname. It does contain a hostnamePath which references a file where the hostname is stored but this requires root privileges to read which my app won't have.

I could substring the container id but this seems quite brittle. I'd also like to avoid shelling out to the external docker process if I don't have to.

Is there any way I can retrieve the hostname directly from docker-java?

1

There are 1 best solutions below

0
On
dockerClient.inspectContainerCmd(container.getId()).exec().getConfig().getHostName();