Linux入门教程:Docker管理指南(5) – 使用systemd控制和配置Docker,本文介绍如何自定义d


许多Linux发行版本使用systemd来启动docker daemon。本文介绍如何自定义docker设置的一些示例。

启动docker daemon

docker安装后,开始启动docker daemon。

$ sudo systemctl start docker # 或旧的发行版本,使用 $ sudo service docker start

设置docker开机启动:
$ sudo systemctl enable docker
# 或旧的发行版本,使用
$ sudo chkconfig docker on

自定义docker daemon选项

有几种方法来配置docker daemon的参数和环境变量。
推荐的方法是使用systemd的drop-in文件。这些是在/etc/systemd/system/docker.service.d目录的命名为.conf的本地文件。也可以放置在/etc/systemd/system/docker.service文件,会覆盖默认的/lib/systemd/system/docker.service文件。
不过如果你使用包管理器安装了docker,那么可能EnvironmentFile已经存在,为了向后兼容,在/etc/systemd/system/docker.service.d放置一下.conf文件,内容如下:

[Service] EnvironmentFile=-/etc/sysconfig/docker EnvironmentFile=-/etc/sysconfig/docker-storage EnvironmentFile=-/etc/sysconfig/docker-network ExecStart= ExecStart=/usr/bin/dockerd $OPTIONS \           $DOCKER_STORAGE_OPTIONS \           $DOCKER_NETWORK_OPTIONS \           $BLOCK_REGISTRY \           $INSECURE_REGISTRY

检查docker.service是否使用了EnvironmentFile:

$ systemctl show docker | grep EnvironmentFile   EnvironmentFile=-/etc/sysconfig/docker (ignore_errors=yes)

或者找出service文件放置的位置:

$ systemctl show --property=FragmentPath docker   FragmentPath=/usr/lib/systemd/system/docker.service   $ grep EnvironmentFile /usr/lib/systemd/system/docker.service   EnvironmentFile=-/etc/sysconfig/docker

你可以使用一个覆盖文件来自定义docker daemon选项。位于/usr/lib/systemd/system或/lib/systemd/system目录的文件包含了默认的选项,不应该去编辑它。

运行时目录和存储驱动

你可能想控制docker镜像,容器和volumes占用的硬盘空间,这个可以把它们移动到一个单独的分区。
在这个示例中,我们假设你的docker.service文件类似如下:

[Unit] Description=Docker Application Container Engine Documentation=https://docs.docker.com After=network.target   [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 ExecReload=/bin/kill -s HUP $MAINPID # Having non-zero Limit*s causes performance problems due to accounting overhead # in the kernel. We recommend using cgroups to do container-local accounting. LimitNOFILE=infinity LimitNPROC=infinity LimitCORE=infinity # Uncomment TasksMax if your systemd version supports it. # Only systemd 226 and above support this version. #TasksMax=infinity TimeoutStartSec=0 # 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   [Install] WantedBy=multi-user.target

我们可以在/etc/systemd/system/docker.service.d目录放置一个drop-in文件,包含如下内容:

[Service] ExecStart= ExecStart=/usr/bin/dockerd --graph="/mnt/docker-data" --storage-driver=overlay

可以在这个文件设置其它的环境变量,例如,HTTP_PROXY。
要更改ExecStart配置,可以在一行空的ExecStart的下一行放置一个新的ExecStart:

[Service] ExecStart= ExecStart=/usr/bin/dockerd --bip=172.17.42.1/16

HTTP proxy

此示例覆盖了默认的docker.service文件。
如果你的主机需要通过Http代理服务器连网,你需要在docker systemd service文件配置一个http代理。
1.为docker service创建一个systemd drop-in目录:

$ mkdir /etc/systemd/system/docker.service.d

2.创建/etc/systemd/system/docker.service.d/http-proxy.conf文件,添加HTTP_PROXY环境变量:

[Service] Environment="HTTP_PROXY=http://proxy.example.com:80/"

3.可以使用NO_PROXY指定不需要代理的一些地址:

Environment="HTTP_PROXY=http://proxy.example.com:80/" "NO_PROXY=localhost,127.0.0.1,docker-registry.somecorporation.com"

4.重载配置

$ sudo systemctl daemon-reload

5.验证配置是否已经加载:

$ systemctl show --property=Environment docker Environment=HTTP_PROXY=http://proxy.example.com:80/

6.重启docker

$ sudo systemctl restart docker

手动创建systemd单元文件

如果你不是使用包管理器安装的docker,但想整合docker进systemd便于管理。可以从https://github.com/docker/docker/tree/master/contrib/init/systemd下载两个单元文件安装到/etc/systemd/system目录即可。

相关内容