【Docker】配置文件

news/2024/10/9 1:37:52/

问题

学习Docker期间会涉及到docker的很多配置文件,可能会涉及到的会有:

/usr/lib/systemd/system/docker.service  【docker用于被systemd管理的配置文件】

/etc/systemd/system/docker.service.d【覆盖配置文件的存放处】

/etc/systemd/system/multi-user.target.wants/docker.service【开机启动的配置文件软链接】

/etc/sysconfig/docker【这个配置文件是用来兼容老版本的linux的,一般不建议使用,而是用下面的daemon.json方式】

/etc/docker/daemon.json【这个配置文件是json格式,跨平台的,也是docker官方推荐的】

这么多配置文件,一开始也会有点蒙,到底应该配置那个文件才行。自己整理了一些,做下记录方便以后复习。

介绍

1、/usr/lib/systemd/system/docker.service

/usr/lib/systemd/system/docker.service 文件是 systemd 用来管理 Docker 服务的单元文件。systemd 【【Linux】Systemd介绍-网络整理-CSDN博客】是一个系统和服务管理器,用于 Linux 操作系统中启动和管理系统服务。这个 .service 文件定义了如何启动、停止以及管理 Docker 守护进程(daemon),简单说这个配置文件其实是在linux系统中已经使用了systemd 的情况下,各软件安装后形成的一个配置文件,比如docker.service配置文件里面的内容就是根据systemd规范来写的配置,告诉systemd 进程怎么管理和启动docker。内容如下:

[Unit]
Description=Docker Application Container Engine
Documentation=https://docs.docker.com
After=network-online.target docker.socket firewalld.service containerd.service time-set.target
Wants=network-online.target containerd.service
Requires=docker.socket[Service]
Type=notify
# the default is not to use systemd for cgroups because the delegate issues still
# exists and systemd currently does not support the cgroup feature set required
# for containers run by docker
ExecStart=/usr/bin/dockerd '--config-file=/etc/docker/daemon.json 【这个是我自己后面加的】' -H fd:// --containerd=/run/containerd/containerd.sock
ExecReload=/bin/kill -s HUP $MAINPID
TimeoutStartSec=0
RestartSec=2
Restart=always# Note that StartLimit* options were moved from "Service" to "Unit" in systemd 229.
# Both the old, and new location are accepted by systemd 229 and up, so using the old location
# to make them work for either version of systemd.
StartLimitBurst=3# Note that StartLimitInterval was renamed to StartLimitIntervalSec in systemd 230.
# Both the old, and new name are accepted by systemd 230 and up, so using the old name to make
# this option work for either version of systemd.
StartLimitInterval=60s# Having non-zero Limit*s causes performance problems due to accounting overhead
# in the kernel. We recommend using cgroups to do container-local accounting.
LimitNPROC=infinity
LimitCORE=infinity
# Older systemd versions default to a LimitNOFILE of 1024:1024, which is insufficient for many
# applications including dockerd itself and will be inherited. Raise the hard limit, while
# preserving the soft limit for select(2).
LimitNOFILE=1024:524288# Comment TasksMax if your systemd version does not support it.
# Only systemd 226 and above support this option.
TasksMax=infinity# set delegate yes so that systemd does not reset the cgroups of docker containers
Delegate=yes# kill only the docker process, not all processes in the cgroup
KillMode=process
OOMScoreAdjust=-500[Install]
WantedBy=multi-user.target

一般不建议直接修改文件里面的内容,因为在版本升级时,会直接覆盖这个文件,到时自己的配置就会被覆盖,那么针对这种情况怎么办? 然后下面介绍第二个配置

2、/etc/systemd/system/docker.service.d/

/etc/systemd/system/docker.service.d/ 目录用于存放 Docker 服务单元文件【就是指/usr/lib/systemd/system/docker.service这个文件】的覆盖配置。这个目录允许系统管理员在不直接修改原始服务单元文件的情况下,对 Docker 服务进行自定义配置。这种方法的好处是,当你更新 Docker 包时,这些覆盖配置不会被新版本的默认配置文件所覆盖,从而保持了你所做的更改。意思就是说你可以在docker.service.d目录下新建xx.conf配置,然后里面指定[Service]或者[Unit]等,然后systemd启动docker时会用这个xx.conf配置文件中的[Service]配置项覆盖/usr/lib/systemd/system/docker.service文件中对应的[service]项中对应的配置

在这个目录下,你可以创建一个或多个以 .conf 结尾的文件,每个文件都可以包含 [Service][Unit][Install] 等节,用来覆盖 /usr/lib/systemd/system/docker.service 中相应的设置。

例如,如果你想添加一些环境变量或者修改 ExecStart 命令行参数,可以创建一个名为 override.conf 的文件:

sudo mkdir -p /etc/systemd/system/docker.service.d
sudo nano /etc/systemd/system/docker.service.d/override.conf

然后,在 override.conf 文件中添加如下内容:

[Service]
Environment="DOCKER_OPTS=--storage-driver=overlay2"
ExecStart=
ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock

上面的例子中:

  • Environment 行添加了一个环境变量。
  • ExecStart= 清除了原来的 ExecStart 值。
  • 新的 ExecStart 指定了新的启动命令。

编辑完成后,你需要重新加载 systemd 配置并重启 Docker 服务来应用更改:

sudo systemctl daemon-reload
sudo systemctl restart docker

使用这种方式,你可以灵活地调整 Docker 服务的行为,而不需要担心未来的软件更新会破坏你的定制设置。同时,这也遵循了 systemd 的最佳实践,即尽量避免直接修改 /usr/lib/systemd/system/docker.service 的原版单元文件。

3、/etc/systemd/system/multi-user.target.wants/docker.service

这个文件是一个软链接,它指向:

[root@localhost ]# systemctl enable docker
Created symlink from /etc/systemd/system/multi-user.target.wants/docker.service to /usr/lib/systemd/system/docker.service.
这个配置文件指向的还是: /usr/lib/systemd/system/docker.service

4、/etc/sysconfig/docker

以下是 通义千问给出的答案:

/etc/sysconfig/docker 是一个配置文件,主要用于基于 System V init 或者与之兼容的初始化系统的 Linux 发行版(如一些较旧版本的 CentOS 和 RHEL)中。这个文件用来设置 Docker 守护进程(daemon)启动时的环境变量和其他一些选项。它允许系统管理员在不直接修改 Docker 服务单元文件的情况下定制 Docker 的行为。

主要用途
  • 设置环境变量:你可以在 /etc/sysconfig/docker 文件中定义环境变量,这些变量会在 Docker daemon 启动时被使用。例如,可以设置 DOCKER_OPTS 来传递额外的命令行参数给 Docker daemon。
  • 自定义 Docker 配置:除了环境变量,还可以设置其他配置项,比如日志级别、存储驱动等,具体取决于你的 Docker 版本和发行版的具体实现。

一个典型的 /etc/sysconfig/docker 文件可能看起来像这样:

# /etc/sysconfig/docker 示例
# 设置 Docker daemon 的选项
DOCKER_OPTS="--storage-driver=devicemapper --dns 8.8.8.8 --dns 8.8.4.4"# 其他可能的设置
# 日志级别
DOCKER_LOGLEVEL="info"

在这个例子中:

  • DOCKER_OPTS 变量用于指定 Docker daemon 的启动参数,比如指定了存储驱动为 devicemapper 并设置了 DNS 服务器。
  • DOCKER_LOGLEVEL 可以用来设定 Docker 的日志级别,这取决于 Docker 的具体实现和支持的选项。
使用场景

如果你的系统还在使用 System V init 脚本来管理服务,那么 /etc/sysconfig/docker 就是定制 Docker 守护进程配置的主要方式之一。然而,随着越来越多的现代 Linux 发行版采用 systemd 作为默认的服务管理系统,通常会推荐使用 /etc/docker/daemon.json 来进行 Docker daemon 的配置,并且可以通过创建覆盖文件(如 /etc/systemd/system/docker.service.d/override.conf)来进一步调整 systemd 服务单元的行为。

注意事项
  • 如果你正在使用的 Linux 发行版已经迁移到了 systemd,那么建议优先考虑使用 /etc/docker/daemon.json 和 systemd 的覆盖机制来配置 Docker。
  • 在修改任何配置文件之前,最好备份原始文件,以防出现问题时能够恢复。
  • 修改完配置文件后,需要重启 Docker 服务才能使更改生效。

5、/etc/docker/daemon.json

    一句话就是docker官方推荐使用这个跨平台的json格式文件来配置docker的 daemon进程。

所以除了/usr/lib/systemd/system/docker.service 其他的几种可以不关心.

看网络资料说:这个配置文件需要 docker 版本高于 1.12.6,没有测试过。

所以综合和上面的介绍,我个人认为笔记正确的配置是:

1、在/etc/systemd/system/docker.service.d/目录创建一个覆盖配置 ,我创建了一个override.conf文件,内容如下,

 

 然后:

systemctl daemon-reload

systemctl restart docker

: systemctl status docker 查看状态,可以看到Drop-In 指向了覆盖配置,说明覆盖配置使用了

然后我们可以在这个覆盖配置中的: ExecStart  启动命令项中加入: 其他配置文件记录

文件内容如下:

docker info 指令查看下:

说明配置起作用了。

网络参考:【docker配置参数详解---/etc/docker/daemon.json完整参数_docker daemon.json配置-CSDN博客】


http://www.ppmy.cn/news/1536400.html

相关文章

DFT ATPG coverage 详解

1. **定义** - **DFT (Design for Testability)**:可测试性设计是一种在芯片设计阶段就考虑如何提高芯片可测试性的方法。通过在设计中插入特定的测试结构,如扫描链、内建自测试(BIST)电路等,使得芯片在制造出来后能…

python爬虫 - 进阶requests模块

🌈个人主页:https://blog.csdn.net/2401_86688088?typeblog 🔥 系列专栏:https://blog.csdn.net/2401_86688088/category_12797772.html 目录 前言 一、SSL证书问题 (一)跳过 SSL 证书验证 &#xff0…

websockets库使用(基于Python)

主要参考资料: 【Python】websockets库的介绍及用法: https://blog.csdn.net/qq_53871375/article/details/135920231 python模块websockets,浏览器与服务器之间的双向通信: https://blog.csdn.net/randy521520/article/details/134752051 目录 websocke…

根据视频id查询播放量

声明:文章仅用于学习交流,如有侵权请联系删除 如何根据视频ID查询视频的播放数量 在数字化时代,视频内容的消费已成为人们日常生活的重要组成部分。无论是社交媒体平台上的短视频,还是视频分享网站上的长视频,了解视频的播放数量…

ARTS Week 43

Algorithm 本周的算法题为 1822. 数组元素积的符号 已知函数 signFunc(x) 将会根据 x 的正负返回特定值: 如果 x 是正数,返回 1 。 如果 x 是负数,返回 -1 。 如果 x 是等于 0 ,返回 0 。 给你一个整数数组 nums 。令 product 为数…

STM32的DMA技术介绍

DMA(Direct Memory Access,直接内存访问) 是一种允许外设直接与系统内存进行数据传输,而无需经过CPU的技术。在STM32微控制器中,DMA技术极大地提高了数据传输效率,降低了CPU的负担,从而提升系统…

JS基础总结

JS基础总结 WebAPI获取元素事件事件源的位置操作元素元素节点元素属性BOM对象操作元素综合示例(键盘移动活动表格) 执行上下文和执行栈执行上下文执行上下文的特点执行上下文的生命周期执行栈 作用域var let const的区别作用域链作用域和值类型引用类型的…

Prometheus之Pushgateway使用

Pushgateway属于整个架构图的这一部分 The Pushgateway is an intermediary service which allows you to push metrics from jobs which cannot be scraped. The Prometheus Pushgateway exists to allow ephemeral and batch jobs to expose their metrics to Prometheus. S…