Disable logging for one container in Docker-Compose

I have a web application launched using Docker compose that I want to disable all logging for (or at the very least print it out to syslog instead of a file).

When my web application works it can quickly generate an 11GB log file on startup so this eats up my disk space very fast.

I'm aware that normal docker has logging options for its run command but in Docker Compose I use

docker-compose up

in the application folder to start my application. How would I enable this functionality in my case? I'm not seeing a specific case anywhere online.

58808 次浏览

您应该能够使用 伐木功能。尝试设置驱动程序为 none

logging:
driver: none

完整的例子:

services:
website:
image: nginx
logging:
driver: none

在 docker-compose 的最新版本中,如果所有服务都禁用了日志记录,那么 docker-compose 将以分离模式行事。要强制使用附加模式,你可以添加一个简单的静默服务,如下所示:

services:
website:
image: nginx
logging:
driver: none


force-attach:
image: bash
command: tail -f /dev/null

对于一个快速配置示例,类似于

version: '3'
services:
postgres:
image: postgres:9.6
logging:
driver: none


作为第二种选择,您可能对不完全删除日志记录(‘ none’值的作用)感兴趣,但是在 Syslog中存储某些服务的日志时,存在相应的驱动程序,例如:

certbot:
image: certbot/certbot
restart: unless-stopped
logging:
driver: "syslog"

注意,syslog 守护进程必须在主机上运行

这并不完全符合要求,但是从2021年9月开始(在 Compose V2上) ,--attach参数允许选择要侦听的服务。

例如,docker compose up --attach your-service只显示 your-service的日志。

永远不要禁用日志记录,相反,你可以对你的服务设置一些限制:

version: '3'
services:
app1:
image: kong
logging:
options:
max-size: "10m"
max-file: "3"