I added a non-root user in docker group, and an other service run as this non-root user connect to docker daemon. but the service can not work.
I do a test example for this :
root@# systemctl start docker.service
root@# gpasswd -a tiger docker
create a systemd service in tiger:
[Service]
ExecStart=/home/tiger/connectdocker
Restart=always
StartLimitInterval=0
Delegate=true
KillMode=process
[Install]
WantedBy=default.target
the /home/tiger/connectdocker
like this:
docker run -itd busybox 2> connectdocker.log
start this service:
tiger@# systemctl --user enable connectdocker.service
tiger@# systemctl --user start connectdocker.service
and the result:
Thu Jul 21 00:59:15 CST 2016
Cannot connect to the Docker daemon. Is the docker daemon running on this host?
but I can connect to docker.sock with tiger :
tiger@# docker run -itd busybox
997e99f959cfd5500319935ec17677775da9d367d203a11efef8b42161c3ee64
for prove that, I change the /var/run/docker.sock
group from docker to tiger, and the connectdocker service can connect to docker daemon.
change /var/run/docker.sock
:
ls -l /run/docker.sock
srw-rw---- 1 root docker 0 Jul 21 00:33 /run/docker.sock
to:
ls -l /run/docker.sock
srw-rw---- 1 root tiger 0 Jul 21 00:33 /run/docker.sock
Best Answer
You should use the
User=
directive in yoursystemd
service.https://www.freedesktop.org/software/systemd/man/systemd.exec.html#User=
I would also recommend moving your script from a home directory to a standard path, like
/usr/local/bin
or something of the like.You should also ensure the ordering of your
connectdocker.service
by giving it theAfter=docker.service
andRequires=docker.service
. As it is written theconnectdocker.service
is probably trying to start around the same time as thedocker.service
, and you'd need to wait fordocker.service
to be up before you can connect to it.https://www.freedesktop.org/software/systemd/man/systemd.unit.html#Requires=
https://www.freedesktop.org/software/systemd/man/systemd.unit.html#Before=