[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 -H fd:// --containerd=/run/containerd/containerd.sock ExecReload=/bin/kill -s HUP $MAINPID TimeoutSec=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. LimitNOFILE=infinity LimitNPROC=infinity LimitCORE=infinity
# 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
[Install] WantedBy=multi-user.target
docker.socket服务
/usr/lib/systemd/system/docker.socket
1 2 3 4 5 6 7 8 9 10 11 12
[Unit] Description=Docker Socket for the API PartOf=docker.service
发现环境变量PATH是不相同的。之所以出现sudo: docker: commmand not found的问题,是因为在PATH=/sbin:/bin:/usr/sbin:/usr/bin下面找不到docker程序。
可以添加一个docker组来解决。
如果是通过二进制安装的话,就不会自动生成docker组,所以需要我们自己生成。
1
sudo groupadd docker
同时把用户加入到这个组中,并重启docker。
1 2 3
sudo gpasswd -a simon docker Adding user simon to group docker sudo systemctl restart docker
用户以docker组重新登录一下
1
newgrp docker
这时,就可以正常使用docker命令了。
那我们会问,如果不把用户加入docker组,不加sudo执行docker命令又会怎样呢?
1 2
docker ps Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Get http://%2Fvar%2Frun%2Fdocker.sock/v1.38/containers/json: dial unix /var/run/docker.sock: connect: permission denied
会发现当我们连接socket的时候,没法访问/var/run/docker.sock这个文件。
原因是,这个文件所属用户和用户组都是root的。我们用普通用户是没法访问的。
1 2
ls -l /var/run/docker.sock srw-rw----. 1 root root 0 Nov 21 21:27 /var/run/docker.sock