0

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?

Rupert Madden-Abbott
  • 12,899
  • 14
  • 59
  • 74
  • What hostname are you looking for? The value `hostname` inside the container would return, the hostname it can be reached from other containers on the same Docker network, a way to reach the container from outside? (What will you do with this value once you have it?) – David Maze Oct 23 '20 at 13:38
  • @DavidMaze I am looking for the value `hostname` inside the container would return and also the hostname that can be used from other containers on the same Docker network. These appear to be the same thing (at least with the network configuration I have used) because if I ssh into container a and run hostname -s, then ssh into container b and ping the retrieved hostname, I get packets coming back. I need to set this value into a config file which must be shared between both containers and the config file must be identical between both containers (constraint of the 3rd party tool I am using). – Rupert Madden-Abbott Oct 23 '20 at 14:08

1 Answers1

1
dockerClient.inspectContainerCmd(container.getId()).exec().getConfig().getHostName();
toro
  • 13
  • 3