1

I have to run more than one containers with the same application. However, the logging logic is under the application and will write it to a log file.

Under the situation, how can i separate logs for different instance for application without modifying the application logging code.

EDIT

  • we want the log to be persistent.
  • we also want the log files to be stored under the same directory in host machine, like /var/log/app/*.log. This is mainly used to be consistent with former settings.
  • we can not modify the logging logic is that we are under testing about using docker. We need the app to both run under docker and normal situation without modifying code just for adding support for test in docker.
  • currently, we want to use shared volume, since all the applications developed almost all write log to a file.
2
  • I think we need some more information. Why aren't the logs already separate in their own containers? Are they writing to a shared volume or to an outside logging service of some kind? Commented Jun 12, 2016 at 4:30
  • @BMitch, we may not want to modify code just adjusting for test in container. we use shared volume. Commented Jun 12, 2016 at 5:16

1 Answer 1

2

As mentioned, if that log file is within the container, that same file will differ from container to container, provided each instance of the application is launched in its own container.

But you can also monitor the log of each container with the docker logs --follow <containerID> command.
And you can change where those logs are directed, with the log driver.

we want the log to be persistent
we also want the log files to be stored under the same directory in host machine, like /var/log/app/*.log

One option is to mount a different host file to the same container file for each application instance:

docker run -v /var/log/app/app1.log:/path/to/log ... docker run -v /var/log/app/app2.log:/path/to/log ... docker run -v /var/log/app/app3.log:/path/to/log ... 

can I get the docker container id with an env for something else and append it as an suffix?

When you launch your containers (docker run), there is no ID yet.

But once a container is launched, you can get its ID with docker ps and make symlink to your log file.

ln -s app1.log app1_ID.log 
Sign up to request clarification or add additional context in comments.

8 Comments

we want the log to be persistent. And, all log are write to a log file not stdout or stderr, can i in this case to read the log of each container with the docker logs --follow <containerID>
we also want the log files to be stored under the same directory in host machine, like /var/log/app/*.log. This is mainly used to be consistent with former settings. :)
@andy OK, I have edited the answer accordingly. The key idea is that, within a container, the app logs to the same file. But when launching the container, you can map that file to any host file you want.
thanks for your answers. However, can i get the docker container id with an env for something else and append it as an suffix.
@andy Not when launching them (since the containers are not yet launched, there are no ID yet). But once they are launched docker ps --format "{{.ID}}" will list only the container IDs, and you can create symlinks between app1.log and app1_<ID>.log (the latter being a link to the former). That way, you have a view of logs per ID (linked to the actual host log files)
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.