07/10/2014: How find the published port of a Docker container in Java
After I spin up Accumulo in a Docker container, well-known ports (like 2181 for Zookeeper) are not well-known any more. The internal private port (i.e., 2181) is exposed as a different public port (i.e., 49143). Java program trying to connect to Accumulo must automatically find the public port numbers.
The java code below finds the public port for Zookeeper for a Docker container named "walt". I don't know why the slash is needed in the image name.
In order to use the DockerClient object, I added the following to my pom.xml:
The java code below finds the public port for Zookeeper for a Docker container named "walt". I don't know why the slash is needed in the image name.
int wantedPublicPort = -1; String wantedContainerName = "/walt"; int wantedPrivatePort = 2181; String dockerURL = "http://127.0.0.1:4243"; String dockerUser = "medined"; String dockerPassword = "XXXXX"; String dockerEmail = "david.medinets@gmail.com"; DockerClient docker = new DockerClient(dockerURL); docker.setCredentials(dockerUser, dockerPassword, dockerEmail); List<Container> containers = docker.listContainersCmd().exec(); for (Container container : containers) { String[] names = container.getNames(); for (String name : container.getNames()) { if (name.equals(wantedContainerName)) { for (Container.Port port : container.getPorts()) { if (port.getPrivatePort() == wantedPrivatePort) { wantedPublicPort = port.getPublicPort(); } } } } } System.out.println("Zookeeper Port: " + wantedPublicPort);
In order to use the DockerClient object, I added the following to my pom.xml:
gt; gt;com.github.docker-java gt; gt;docker-java gt; gt;0.9.0 gt; gt;
07/10/2014: Finding Log Files Inside Docker Containers
As a simple lay programmer, I sometimes have trouble figuring out where log files are stored on unix systems. Sometimes logs are within application directories. Other times they are in /var/log. With Docker containers, this uncertainty is eliminated. How? By the 'docker diff' command. I will show why.
When connecting to a Docker-based system, you can see the running containers:
Then you can list changed files within the container using the image id or name.
Armed with this list you can confidently either look in /var/lib/docker or use the nsenter command to join the namespace of the container to read interesting files.
$ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 90a9f7122c02 medined/accumulo:latest /run.sh walt 9 hours ago Up 9 hours 0.0.0.0:49153->50070/tcp, 0.0.0.0:49154->50090/tcp, 0.0.0.0:49155->50095/tcp, 0.0.0.0:49156->8025/tcp, 0.0.0.0:49157->8030/tcp, 0.0.0.0:49158->8088/tcp, 0.0.0.0:49159->10020/tcp, 0.0.0.0:49160->19888/tcp, 0.0.0.0:49161->2181/tcp, 0.0.0.0:49162->22/tcp, 0.0.0.0:49163->8020/tcp, 0.0.0.0:49164->8050/tcp, 0.0.0.0:49165->8141/tcp walt
Then you can list changed files within the container using the image id or name.
$ docker diff walt ... D /data1/hdfs/dn/current/BP-1274135865-172.17.0.10-1404767453280/current/finalized/blk_1073741825_1001.meta ... A /var/log/supervisor/accumulo-gc-stderr---supervisor-5H7Rr7.log A /var/log/supervisor/accumulo-gc-stdout---supervisor-LK8wDU.log ... A /var/log/supervisor/namenode-stdout---supervisor-mciN4u.log A /var/log/supervisor/secondarynamenode-stderr---supervisor-EaluLZ.log A /var/log/supervisor/secondarynamenode-stdout---supervisor-Ap4Fri.log C /var/log/supervisor/supervisord.log A /var/log/supervisor/zookeeper-stderr---supervisor-CCwUGw.log A /var/log/supervisor/zookeeper-stdout---supervisor-lDiuIF.log C /var/run C /var/run/sshd.pid C /var/run/supervisord.pid
Armed with this list you can confidently either look in /var/lib/docker or use the nsenter command to join the namespace of the container to read interesting files.