spring-boot-docker-postgres icon indicating copy to clipboard operation
spring-boot-docker-postgres copied to clipboard

Adding Docker --tty results in --rm no longer working

Open robert-bor opened this issue 8 years ago • 5 comments

This is the standard command:

docker run --rm --tty -e POSTGRES_PASSWORD=${password} -p ${port}:5432 --name ${containerName} ${imageName}:${imageVersion}

The recent addition of --tty resulted in the --rm no longer working. Used containers now clog the system, which is not so bad as long as force-clean is in place and the container names are more or less the same over projects. When this is not the case, the problem is worse, since containers will have to be manually removed to free up port bindings.

--tty was added to address issue #14 . Note that before, the buffer of the Docker output log refused to show the last bit of output, ie the part which contains the second showing of the verification text.

Make sure to have both aspects working, ie signing off on a reliable verification text and removing the container when the process is destroyed.

robert-bor avatar Mar 27 '17 11:03 robert-bor

The concept solution is to get rid of --tty. The best way to do this is to make use of Docker own's docker logs [CONTAINER]. This returns the entire log.

The stderr log can be read the same way it is read now. Alternatively, it could be redirected from docker logs to a file location (eg, https://github.com/docker/docker/issues/7440#issuecomment-51365165).

Advantage to this approach is that the unreliable stdout writing / checking process can be replaced by a more robust, flushing process supplied by Docker itself. Disadvantage is that the whole output will need to be parsed. For bigger log files, this could add up.

robert-bor avatar Mar 27 '17 12:03 robert-bor

Note that logging (LOGGER) the lines will be more of a challenge, since this was based on a continuous stream of information. A possible solution could be to keep track of what was already logged.

robert-bor avatar Mar 28 '17 11:03 robert-bor

Try running it with -i --tty

docker run --rm -i --tty -e POSTGRES_PASSWORD=${password} -p ${port}:5432 --name ${containerName} ${imageName}:${imageVersion}

From: docker run --help

-i, --interactive    Keep STDIN open even if not attached

terencewestphal avatar Mar 29 '17 21:03 terencewestphal

@terencewestphal It works like a charm on the command-line. However, if called from Java, it throws an error:

> the input device is not a TTY

robert-bor avatar Mar 30 '17 06:03 robert-bor

Using docker logs ${containerName} does not have the desired effect. Whereas running the command from the CLI, gets the entire log, running it within Java, gives a partial (excluding the non-flushed part of the log). Specifically for Postgres, the non-flushed part contains the 2nd startup validation text.

The branch is saved in poc/21-docker-logs-instead-of-file-read.

Will pursue a different solution. --tty will be kept in placed and calling @PreDestroy will trigger the explicit removal of the container. See #27

robert-bor avatar Apr 13 '17 15:04 robert-bor