0

I'm trying to start a process (dstat system-tool) from inside a Java application running on Docker. Running the app on my local machine, the following code works:

LOG.debug("Start DSTAT data collecting...");
try {
    final ProcessBuilder processBuilder = new ProcessBuilder(DSTAT_COMMAND);
    processBuilder.redirectErrorStream(true);
    final Process process = processBuilder.start();
    try (BufferedReader processOutputReader = new BufferedReader(new InputStreamReader(process.getInputStream()))) {
        final String dstatResult = processOutputReader.lines()
                    .map(String::toString)
                    .collect(Collectors.joining(System.lineSeparator()));
        final boolean exitCode = process.waitFor(10, TimeUnit.SECONDS);
        LOG.debug("Dstat exit: {}", exitCode);
        LOG.debug("Dstat output: {}", dstatResult);
}

But starting the same with Docker there is no output from the dstat-process, my log is:

2016-07-31 08:31:56.984 DEBUG 15 --- [lector-thread-1] i.thesis.collector.dstat.DstatCollector : Start DSTAT data collecting... 2016-07-31 08:31:57.190 DEBUG 15 --- [lector-thread-1] i.thesis.collector.dstat.DstatCollector : Dstat exit: true 2016-07-31 08:31:57.190 DEBUG 15 --- [lector-thread-1] i.thesis.collector.dstat.DstatCollector : Dstat output:

I also tried Runtime.getRuntime().exec("dstat"), what causes hanging at

2016-07-30 19:18:41.199 DEBUG 11 --- [lector-thread-1] i.thesis.collector.dstat.DstatCollector  : Start DSTAT data collecting...

The dstat tool is correctly installed on the container, I can docker exec into, run the dstat command and get the expected output.

For completeness the Dockerfile:

FROM java:8-jre-alpine

RUN apk add --no-cache bash snappy

ARG FLINK_VERSION=1.0.3
ARG HADOOP_VERSION=27
ARG SCALA_VERSION=2.11

RUN set -x && \
  apk --update add --virtual build-dependencies curl && \
  # install dstat as collector source
  apk add dstat --update-cache --repository http://dl-3.alpinelinux.org/alpine/edge/testing/ --allow-untrusted && \
  curl -s $(curl -s https://www.apache.org/dyn/closer.cgi\?as_json\=1 | \
  awk '/preferred/ {gsub(/"/,""); print $2}')flink/flink-${FLINK_VERSION}/flink-${FLINK_VERSION}-bin-hadoop${HADOOP_VERSION}-scala_${SCALA_VERSION}.tgz | \
  tar xvz -C /usr/local/ && \
  ln -s /usr/local/flink-$FLINK_VERSION /usr/local/flink && \
  sed -i -e "s/echo \$mypid >> \$pid/echo \$mypid >> \$pid \&\& wait/g" /usr/local/flink/bin/flink-daemon.sh && \
  apk del build-dependencies && \
  rm -rf /var/cache/apk/*

  ADD collector-client-app.jar /usr/local/collector/collector-client-app.jar
  EXPOSE 9091

  ENV FLINK_HOME /usr/local/flink
  ENV PATH $PATH:$FLINK_HOME/bin
  ENV FLINK_JMX_PORT 9999

  ADD docker-entrypoint.sh $FLINK_HOME/bin/
  ENTRYPOINT ["docker-entrypoint.sh"]
  CMD ["sh", "-c"]

docker-entrypoint.sh:

if [ "$1" = "jobmanager" ]; then
    echo "Starting Job Manager"
    sed -i -e "s/jobmanager.rpc.address: localhost/jobmanager.rpc.address: `hostname -f`/g" $FLINK_HOME/conf/flink-conf.yaml
    sed -i -e "s/taskmanager.numberOfTaskSlots: 1/taskmanager.numberOfTaskSlots: `grep -c ^processor /proc/cpuinfo`/g" $FLINK_HOME/conf/flink-conf.yaml
    echo "env.java.opts: \"-Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=$FLINK_JMX_PORT -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false \"" >> $FLINK_HOME/conf/flink-conf.yaml
    echo "config file: " && grep '^[^\n#]' $FLINK_HOME/conf/flink-conf.yaml
    $FLINK_HOME/bin/jobmanager.sh start cluster &
elif [ "$1" = "taskmanager" ]; then
    echo "Starting Task Manager"
    echo "config file: " && grep '^[^\n#]' $FLINK_HOME/conf/flink-conf.yaml
    $FLINK_HOME/bin/taskmanager.sh start &
else
    $@
fi
echo "Start collector client..."
java -Djava.security.egd=file:/dev/./urandom -jar /usr/local/collector/collector-client-app.jar

and docker-compose.yml

version: '2'
services:
    flink-jobmanager:
    image: flink
    container_name: flink-jobmanager
    ports:
      - "8081:8081"
      - "9999:9999"
      - "9095:9091"
    command: jobmanager

It probably has something to do with docker's process-stdout-or-whatever handling, but I couldn't find anything helpful yet.

So thanks in advance for any ideas or suggestions!

Markus Lamm
  • 391
  • 1
  • 4
  • 15

0 Answers0