7

Docker container can only to daemon mode: I run catalina.sh to start the tomcat.
But the problem is my log will not appear in catalina.out.

I can look at `docker logs , but this certainly cannot run in a production environment.
I would like to ask how, in production environment, can I have the Tomcat log stored in the document and without the container stopping?

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
zhangyi
  • 71
  • 1
  • 1
  • 2

4 Answers4

4

If you look at the official tomcat docker image, it runs

CMD ["catalina.sh", "run"]

That is enough to starts tomcat in the foreground, displaying the logs on the console.
But, as you said, that might not populate catalina.out.

An alternative would be:

CMD service tomcat start && tail -f /var/lib/tomcat/logs/catalina.out
Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
3

There are options in this (off topic) question to make the tomcat logs run in the foreground.

But to answer your actual question, the docker logs command is the usual way to get logs from a container. You can also find them on the host as they live in a file.

But the best way is to use an external logging service to collect and aggregate the logs, so you don't have to log in to the production server. Logentries is one example (though it's far from perfect). Splunk is another. The Docker logging drivers docs may help.

Community
  • 1
  • 1
Nauraushaun
  • 1,484
  • 12
  • 10
0

You can override CMD like this:

CMD bash -c "catalina.sh run | tee -a logs/catalina.out"

This way you can show and store the log output at the same time.

But if you even want to store all errors:

CMD bash -c "catalina.sh run |& tee -a logs/catalina.out"

And store into volume like this:

docker run --rm -it -v /some/place:/usr/local/tomcat/logs yourImage
-1

You need to have one process running in foreground in docker container to have the container running.

I use a hack with all my docker images. Create a script run.sh with the following code

#!/bin/sh

service tomcat start
tail -f /dev/null

Make sure before you run the run.sh file in docker, change the permissions.

Addition to Dockerfile will be

COPY run.sh ./run.sh
RUN chmod 755 ./run.sh
CMD ["./run.sh"]
Vaibhav Ranglani
  • 215
  • 1
  • 2
  • 14
  • 2
    Your hack is bad practice! It means docker logs becomes useless, getting the tomcat logs becomes very difficult, and your process can exit without the container exiting. These are all bad things! See the accepted answer here for a better (more dockerey) way :): http://stackoverflow.com/questions/14010448/how-to-start-tomcat-with-output-on-console-in-linux – Nauraushaun Jun 28 '16 at 11:46
  • Cannot say it is a bad practice, but there might be a better way to do stuff. Since docker needs a process to run in the foreground. I have an ejabbered server installed on ubuntu which is started as a service, in that case one needs to have a foreground process. – Vaibhav Ranglani Jun 28 '16 at 13:02