简体   繁体   中英

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

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM