How do I enable docker as a service without re-installing docker?

6,611

Update Aug 12 2019

For your systemd goals, you may want to simply install regular docker and not use it via snap. From:

We do this for several reasons:

  • the fact that systemd is used to launch services is an implementation detail of snapd that we do not want to expose to snap developers. It is entirely possible that another mechanism could be used on different distributions or in the future, and we don’t want snap developers to have to deal with transitions or making the snap only usable on distros with systemd
  • snaps can be installed on any number of systems with varying systemd versions. Exposing the unit files directly in the manner you describe could cause problems with snaps using units with newer systemd directives than the systemd on the system supports
  • the systemd unit specification is extremely rich and flexible and exposing the entirety of the unit specification would allow snaps to influence the system and other snaps in unpredictable and undesirable ways that are counter to snapd’s design surrounding application isolation, reverts, dependencies, etc, etc

Because of the above, we do not plan to wrap the entire systemd unit specification (as you said, that would be unreasonable) and we are exposing a subset of the functionality. Do note that the subset of functionality is being expanded and reevaluated based on feedback from the field (eg, most recently timers and the thread you mentioned 7). Importantly, we want to expose needed functionality to developers in a manner that is consistent with snapd’s design principles and work everywhere in the cross-distribution ecosystem.


Systemd setup with "Regular" docker

Configure docker to start on boot says:

Configure Docker to start on boot

Most current Linux distributions (RHEL, CentOS, Fedora, Ubuntu 16.04 and higher) use systemd to manage which services start when the system boots. Ubuntu 14.10 and below use upstart.

systemd

$ sudo systemctl enable docker

To disable this behavior, use disable instead.

$ sudo systemctl disable docker

Also Control Docker with systemd says:

Control Docker with systemd

Many Linux distributions use systemd to start the Docker daemon. This document shows a few examples of how to customize Docker’s settings.

Start the Docker daemon

Start manually

Once Docker is installed, you need to start the Docker daemon. Most Linux distributions use systemctl to start services. If you do not have systemctl, use the service command.

  • systemctl:

    $ sudo systemctl start docker

  • service:

    $ sudo service docker start


Manually create the systemd unit files

When installing the binary without a package, you may want to integrate Docker with systemd. For this, install the two unit files (service and socket) from the github repository to /etc/systemd/system.

docker.service

[Unit]
Description=Docker Application Container Engine
Documentation=https://docs.docker.com
After=network-online.target docker.socket firewalld.service
Wants=network-online.target
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 -H fd://
ExecReload=/bin/kill -s HUP $MAINPID
LimitNOFILE=1048576
# 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
# 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
# restart the docker process if it exits prematurely
Restart=on-failure
StartLimitBurst=3
StartLimitInterval=60s

[Install]
WantedBy=multi-user.target

docker.socket

[Unit]
Description=Docker Socket for the API
PartOf=docker.service

[Socket]
# If /var/run is not implemented as a symlink to /run, you may need to
# specify ListenStream=/var/run/docker.sock instead.
ListenStream=/run/docker.sock
SocketMode=0660
SocketUser=root
SocketGroup=docker

[Install]
WantedBy=sockets.target
Share:
6,611
Dave
Author by

Dave

Updated on September 18, 2022

Comments

  • Dave
    Dave almost 2 years

    I'm using Ubuntu 16.04. I would like to set up Docker to run as a service. I already have docker installed ...

    davea@blockshare_srvr:~$ which docker
    /snap/bin/docker
    davea@blockshare_srvr:~$ /snap/bin/docker -v
    Docker version 18.06.1-ce, build e68fc7a
    

    but when I try and check its status, I get the below error

    davea@blockshare_srvr:~$ sudo systemctl status docker
    Unit docker.service could not be found.
    

    How can I enable the service without re-installing docker?

  • Yaron
    Yaron almost 5 years
    As you can see, systemctl start docker returns that there is no unit file.
  • WinEunuuchs2Unix
    WinEunuuchs2Unix almost 5 years
    @Yaron I've updated answer with regular service and socket files but this is for regular docker not special snap docker like you've installed. I have to go now but will research more at end of day.
  • WinEunuuchs2Unix
    WinEunuuchs2Unix almost 5 years
    @Yaron I did some initial research and it appears snap and systemd don't work together by design. You might want to install regular docker from the links above.
  • Yaron
    Yaron almost 5 years
    I tend to disagree :) Snap is managing the services in a different manner: allprogrammingtutorials.com/tutorials/… - snap services docker
  • WinEunuuchs2Unix
    WinEunuuchs2Unix almost 5 years
    Here are some of the complexities snap docker users faced setting it up for systemd: github.com/docker/docker-snap/issues/22