I have three containers of the same image deployed in the same host, how to do the mounting?

I use docker swarm to deploy a cluster of a spring-boot application on the same host. The working directory for the docker container is /app, and I do the configuring so that the log files are put in folder /app/logs. I want to see the log files in the directory /opt/logs of the host machine, so in the docker-compose.yml file, I have

       volume:
         - /opt/logs:/app/logs

but since I have a cluster of containers for the same springboot application, all log files in different containers will be mounted to the same /opt/logs on the same host machine, then there is conflict.

What can I do to deal with such an issue?

You might want to try service template placeholders in your volume mapping.
I did use {{.Task.Slot}} as a postfix to volume paths in the past, though I don’t recall the exact usage.
Probably it was on the right hand side of the mapping, in combination with an env variable to set the target folder in the container.

see:

Though, the easiest approach would be to make the hostname of the container part of the filename of the logs.

" to make the hostname of the container part of the filename of the logs"

then the container will have multiple folders which are from other containers

Still, does it solve your requirement “I want to see the log files in the directory /opt/logs”?

Instead of logging into the filesystem your should prefer to log to STDOUT and let docker collect the logs.

it solves the problem, but just more storage-consuming and adds some copying overhead

“log to STDOUT and let docker collect the logs.”? what do you mean?

You use bind mounts which litteraly maps a host folder into a container folder. Why would there be more storage consumption and copy overhead? Frankly, your requirement to collect all container logs in the same host folder only seems appealing if they are replicas of the same service. If they are independend instances of the same image, I would suggest to map different host folders into each containers /app/logs folder.

With Docker you have severall approaches for logging:
– log to the STDOUT console and let docker handle the logs → make sure that your log appender logs to CONSOLE
– log to the filesystem → make sure that logs are rotated properly (this is your current approach)
– direct send logs to a logserver → for instance you can direct send logs to a gelf server or elastic search.

I prefer to log everything to STDOUT and use a log collector (fluentd, logstash, filebean,…) to send the logs to a datastore like elasticsearch.

Loging to STDOUT allows to use docker logs {container id/name} to get the logs.

they are replicas of the same service

1 log everything to STDOUT and use a log collector (fluentd, logstash, filebean,…) to send the logs to a datastore like elasticsearch

2 direct send logs to a logserver

I think the 2) is much better than 1)

thanks!