Redirect log files generated in docker container to stdout / stderr
I have the following Dockerfile:
FROM ubuntu:16.04
RUN apt-get update
VOLUME ["/LOGS"]
COPY ./testServer .
ENTRYPOINT ./testServer 8600
"testServer" has log files to write to. They are located in the "LOGS" directory. Each time "testServer" is started, a new log is created. What I wanted to do was "push" the last log file into a directory under stdout / stderr.
I tried adding:
CMD ["/bin/sh", "-c", "tail $( ls -Art /LOGS | tail -n 1 ) > out_server.log 2>&1"]
to the Dockerfile (and restored the image after that), but it didn't work.
How can I do that?
TIA
source to share
There are two problems here.
-
You have it installed
ENTRYPOINT
and you are trying to run a command withCMD
. Docker starts a container with one process, and when you define both, it isCMD
added toENTRYPOINT
as additional cli arguments. That the container is working since pid 1:/bin/sh -c './testServer 8600 /bin/sh -c "tail $( ls -Art /LOGS | tail -n 1 ) > out_server.log 2>&1"'
If testServer does not run additional arguments, they will never be used.
-
If the command you are running did work, it will print everything to / out _server.log inside the container, not to stdout, and stop as soon as it reaches the end of the input. If your pid is 1, the container will exit at that point as well.
To fix this, you can create entrypoint.sh to be something like:
#!/bin/sh
./testServer 8600 &
sleep 2 # give testServer time to create the newest log
exec tail -f $( ls -Art /LOGS | tail -n 1 )
This entry point runs testServer in the background and then runs the tail with exec. Exec replaces pid 1 so that signals are transmitted.
Update your Dockerfile to:
FROM ubuntu:16.04
# This apt-get line shouldn't be needed unless something else
# needs the possibly outdated package repo list
# RUN apt-get update
# Removing this volume, you can create it from a docker-compose.yml
# VOLUME ["/LOGS"]
COPY entrypoint.sh testServer /
RUN chmod 755 /entrypoint.sh /testServer
ENTRYPOINT [ "/entrypoint.sh" ]
For more details on why I removed the line VOLUME
, see my blog post here .
source to share