> **Key Takeaways** > > * Docker container console logs are stored as files on the host's disk, **not in system memory**. > * The `docker logs` command reads and displays these stored files. > * To prevent log files from growing indefinitely, it's crucial to **configure log rotation options (`max-size`, `max-file`)**. ## 1. Where and How Are Logs Stored? {#sec-37c2a29cff35} Docker captures both standard output (stdout) and standard error (stderr) streams from containers and processes them via a **Logging Driver**. If no specific configuration is made, Docker defaults to using the `json-file` logging driver. * **Storage Location**: Container logs are stored as JSON-formatted files in a specific path on the host system. The typical path is: * `/var/lib/docker/containers/[컨테이너_ID]/[컨테이너_ID]-json.log` * **`docker logs` Command**: This command doesn't display content from memory; rather, it reads the aforementioned JSON log files and presents them in the terminal. ![Docker container log file example](/media/whitedec/blog_img/ff3498a786c645ecb031d714a053d4a4.webp) ## 2. What if Logs Keep Accumulating and Cause Disk Space Issues? {#sec-4e6257ec2aba} Yes, that's correct. If no action is taken, log files can grow indefinitely and consume all of the host's disk space. To prevent this, Docker provides **Log Rotation** options to limit the size and number of log files. For example, you can add the following options when running a `docker run` command: ``` docker run -d \ --log-driver json-file \ --log-opt max-size=10m \ --log-opt max-file=3 \ nginx ``` * `--log-opt max-size=10m`: Limits the maximum size of a single log file to **10MB**. * `--log-opt max-file=3`: Limits the maximum number of log files to **3**. If the number exceeds 3, the oldest file is deleted. With this setting, the logs for that container will use a maximum of 30MB (10MB * 3 files) of disk space. *** ## 3. Configuring in `docker-compose.yml` {#sec-890442ea3ac0} With Docker Compose, you can easily configure logging options for each service directly within the `docker-compose.yml` file. Think of it as the `--log-opt` flags from the `docker run` command, but in YAML format. Here's an example of applying log rotation to a service named `my-app`: ``` services: my-app: image: your-app-image # ... other configurations ... deploy: resources: limits: memory: 512M # 👇 This section configures logging. logging: driver: "json-file" # The logging driver to use (can be omitted as it's the default) options: max-size: "10m" # Maximum size per file: 10MB max-file: "3" # Maximum number of files: 3 ``` * **`logging`**: This key initiates the logging configuration for the service. * **`driver`**: Specifies the logging driver to use. `json-file` is the default. * **`options`**: Configures options to be passed to the driver. * **`max-size`**: Identical to `docker run`'s `--log-opt max-size`. * **`max-file`**: Identical to `docker run`'s `--log-opt max-file`. Now, when you start this service with `docker-compose up`, the `my-app` container's logs will automatically rotate, using a maximum of 30MB (10MB * 3 files) of disk space. ## 4. Setting Defaults for All Containers {#sec-1a0729e53c34} If you find it cumbersome to apply options to each container individually and wish to enforce a default policy across the entire system, modifying the **Docker Daemon configuration** file is the best approach. 1. Open or create the `/etc/docker/daemon.json` file. (If the file doesn't exist, create a new one.) 2. Add the following content: ```json { "log-driver": "json-file", "log-opts": { "max-size": "10m", "max-file": "3" } } ``` 3. Restart the Docker daemon. ```bash sudo systemctl restart docker ``` With this configuration, **all containers** created in the future will automatically adhere to this policy unless specific logging settings are overridden. Of course, if you specify different logging options for a particular container in a `docker-compose.yml` file or via the `docker run` command, **individual settings will take precedence** over the daemon's default configuration. * **Per-service Configuration**: Setting logging options within the `logging` section of a `docker-compose.yml` file is generally the most common and convenient method. * **Global Default Configuration**: To apply a uniform policy to all containers on the server, modify the `/etc/docker/daemon.json` file.