Skip to content
Go back

Understanding and Optimizing Docker’s daemon.json File

· Updated:
By KingPin 6 min read
Understanding and Optimizing Docker’s daemon.json File

Introduction

The daemon.json file is a crucial configuration file for Docker that allows administrators to customize various aspects of the Docker daemon’s behavior. Here’s the purpose of this file, common configuration options, and best practices for optimizing Docker performance and security.

The Need for daemon.json

The daemon.json file serves several important purposes:

Location and Format

On Linux systems, the daemon.json file is typically located at /etc/docker/daemon.json. If it doesn’t exist, you can create it. The file uses JSON format, which is easy to read and modify.

Rootless Docker Configuration

When using rootless Docker, the location of the daemon.json file is different. For rootless mode, the configuration file is located at ~/.config/docker/daemon.json in the user’s home directory. This separate location ensures that rootless Docker configurations don’t interfere with system-wide Docker settings and allows individual users to have their own custom Docker daemon configurations. Remember to create this file if it doesn’t exist, and ensure it has the correct permissions for the user running rootless Docker.

Common Configuration Options

Let’s explore some common options that can be set in the daemon.json file, along with their purposes and examples:

1. Storage Driver

{
"storage-driver": "overlay2"
}

2. Live Restore

{
"live-restore": true
}

3. DNS Settings

{
"dns": ["8.8.8.8", "1.1.1.1"]
}

4. Logging

{
"log-driver": "json-file",
"log-opts": {
"max-size": "10m",
"max-file": "3"
}
}

5. Registry Mirrors

{
"registry-mirrors": ["https://mirror.gcr.io"]
}

6. Insecure Registries

{
"insecure-registries": ["myregistry.example.com:5000"]
}

7. Default Address Pools

{
"default-address-pools": [
{"base":"172.80.0.0/16","size":24},
{"base":"172.90.0.0/16","size":24}
]
}

My Production Configuration

Now, let’s examine the configuration I use in my production machines and discuss why these settings make sense for a production environment:

{
"storage-driver": "overlay2",
"live-restore": true,
"dns": ["9.9.9.10", "1.1.1.1", "8.8.8.8"],
"max-concurrent-downloads": 5,
"max-concurrent-uploads": 5,
"log-driver": "json-file",
"log-opts": {
"max-size": "5m",
"max-file": "3"
}
}

Whats in this Configuration:

This configuration in my opinion demonstrates a focus on performance, stability, and efficient resource management, which are crucial in a production environment.

Best Practices

Conclusion

The daemon.json file is a powerful tool for customizing Docker’s behavior to suit your specific needs. By understanding the available options and following best practices, you can optimize Docker’s performance, security, and resource usage in your environment. Regular review and thoughtful configuration of this file can enhance your Docker experience, whether you’re a system administrator, DevOps professional, or developer.

Common Questions

Where is the Docker daemon.json file located?

The daemon.json file lives at /etc/docker/daemon.json on Linux, and you create it if it does not exist. Rootless Docker reads ~/.config/docker/daemon.json in the user’s home directory instead. Create that file too if it is missing, with correct permissions for the user running rootless Docker.

How do I stop Docker logs from filling my disk?

Set the json-file log driver with max-size and max-file under log-opts. The production configuration uses 5m and 3. Size and file count limits help prevent log files from consuming too much disk space. Incorrect values can still lead to disk space issues or loss of log data, so set both options.

Can I change the Docker storage driver without recreating containers?

No. Changing the storage driver requires recreating all containers and images. The post recommends overlay2 for most Linux distributions because of its performance and stability. The older aufs and devicemapper drivers were removed from recent Docker Engine releases, so avoid them.

What does live-restore do in Docker?

The live-restore option keeps containers running while the Docker daemon is down. Container availability improves during daemon updates or crashes. The downside is that live-restore may leave Docker with an inconsistent state if the daemon stays down for an extended period.


Share this post on:

Send a Webmention

Written about this post on your own site? Send a webmention and it'll show up above once verified.


Previous Post
Why Your Docker Container Ignores Ctrl+C
Next Post
Techniques for Writing Robust, Reliable Bash Scripts

Discussion

Powered by Garrul . Sign in with GitHub or Google, or post anonymously.

Related Posts