3

I have an application which writes log into file (example /var/log/my_app).

Before Docker I used supervisor to start my app and logrotate.d for rotation logs. Logrotate.d runs script for supervisor to restart my app after log rotate (for delete old log file and create new one):

supervisor restart my_app

How I should do with Docker?

As I know if we use docker container with only one running app we should not use supervisor (start and restart will do docker). So how can I use logrotate.d for this?

  1. Create “volume” for log dir and setup logrotate.d to restart Docker container? But I think it’s not a good idea.

  2. Use logrotate.d within Docker container with my app? For each Docker image I will install logrotate.d and instead of run script for supervisor I should run script for close my app (kill -9 or something else).

Amol
  • 1,084
  • 10
  • 20
Mike
  • 860
  • 14
  • 24

2 Answers2

0

If you decide to move to Docker you should also adapt your application. Applications running in containers should write their logs to the console (system out). You can achieve that by using a CONSOLE appender in your logger configuration. Once that is done you can inspect your logs from outside the container with:

docker logs <container_name>

You can also follow the logs (like you would do with "tail -f"):

docker logs -f <container_name>

You can also change the logging driver used by your container and do more fancy stuff with your logs. See more details here: https://docs.docker.com/config/containers/logging/configure/

Mihai
  • 9,526
  • 2
  • 18
  • 40
  • Interesting, so I can do not use ligrotate.d anymore? – Mike Mar 31 '19 at 16:58
  • But if I use separate log files (example different tasks). In this case I cannot write logs to the console. – Mike Mar 31 '19 at 17:00
  • Maybe it is an idea to scale your containers and use one per task. This way you would really use the power of containers – Mihai Mar 31 '19 at 18:22
  • no, I mean, for example, Django app where each app writes to own log file. – Mike Apr 01 '19 at 07:26
  • Usually those different log files are declared inside the app to make debug easier. Dockerizing an app means a bit more than just adding a Dockerfile. It means a change in architecture as well. Your container should be only concerned with 1 things: to run the app. For managing the logs you should have a different container that does only that. Maybe look at an ELK stack (elastic search). All logs go into the same bucket and then you have optimized searchs that give you what you need – Mihai Apr 01 '19 at 07:41
  • My app (in docker) should write logs to the console, then docker get these logs and send to elastic search. Am I right? – Mike Apr 03 '19 at 11:57
  • Have a look here first: https://docs.docker.com/config/containers/logging/configure/. I worked a lot with fluentd so I think you should consider that setup. This can be later expanded with an ELK Stack – Mihai Apr 03 '19 at 12:31
0

There are two possible options

  1. Your application should write logs to console. Then the logs will be managed using docker logs command
  2. If application must write logs to a file, your application should rotate the logs. Most programming languages support log frameworks that provide this functionality.
Amol
  • 1,084
  • 10
  • 20