Docker daemon won't start via systemctl

I installed docker-ce via Install Docker Engine on Ubuntu on a Ubuntu 20.04 machine (AWS EC2). I see

$ sudo docker info
Client: Docker Engine - Community
 Version:    27.3.1
 Context:    default
 Debug Mode: false
 Plugins:
  buildx: Docker Buildx (Docker Inc.)
    Version:  v0.17.1
    Path:     /usr/libexec/docker/cli-plugins/docker-buildx
  compose: Docker Compose (Docker Inc.)
    Version:  v2.29.7
    Path:     /usr/libexec/docker/cli-plugins/docker-compose

Server:
ERROR: Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?
errors pretty printing info

I see

$ ls -al /var/run/docker.sock 
srw-rw---- 1 root docker 0 Dec  3 23:39 /var/run/docker.sock

But starting the service fails

$ sudo systemctl start docker.service
Job for docker.service failed because the control process exited with error code.
See "systemctl status docker.service" and "journalctl -xe" for details.

…and…

$ journalctl -xe
-- Automatic restarting of the unit docker.service has been scheduled, as the result for
-- the configured Restart= setting for the unit.
Dec 03 23:49:50 ip-10-23-21-53 systemd[1]: Stopped Docker Application Container Engine.
-- Subject: A stop job for unit docker.service has finished
-- Defined-By: systemd
-- Support: http://www.ubuntu.com/support
-- 
-- A stop job for unit docker.service has finished.
-- 
-- The job identifier is 2019 and the job result is done.
Dec 03 23:49:50 ip-10-23-21-53 systemd[1]: docker.service: Start request repeated too quickly.
Dec 03 23:49:50 ip-10-23-21-53 systemd[1]: docker.service: Failed with result 'exit-code'.
-- Subject: Unit failed
-- Defined-By: systemd
-- Support: http://www.ubuntu.com/support
-- 
-- The unit docker.service has entered the 'failed' state with result 'exit-code'.
Dec 03 23:49:50 ip-10-23-21-53 systemd[1]: Failed to start Docker Application Container Engine.
-- Subject: A start job for unit docker.service has failed
-- Defined-By: systemd
-- Support: http://www.ubuntu.com/support

-- A start job for unit docker.service has finished with a failure.
-- 
-- The job identifier is 2019 and the job result is failed.
Dec 03 23:49:50 ip-10-23-21-53 systemd[1]: docker.socket: Failed with result 'service-start-limit-hit'.
-- Subject: Unit failed
-- Defined-By: systemd
-- Support: http://www.ubuntu.com/support
-- 
-- The unit docker.socket has entered the 'failed' state with result 'service-start-limit-hit'.

I can however start it with

$ nohup sudo dockerd &
$ docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES

I’ve rebooted the machine. What am I missing? Thanks.

Hmmm, I see Cannot start Docker daemon using systemctl. But his solution was following the steps on the link I started with, which didn’t work for me.

You will need logs to find the problem. Just because the latest logs are repeating, doesn’t mean there is no log at the beginning that helps. So you will need to go back to a point when the original error happened. The moment the systemd service restarts too many times, you will get nothing except that you can’t start it because it was already started too many times.

Every error could be different so a solution that works for someone might not work for you, but without logs, there is not much we can say.

The problam can be any kind of incompatibility or wrong config file. Since dockerd started when you manually ran it, I assume there is either something in the systemd service or in the daemon config, or something in both that are not compatible with eachother. What I noticed recently on one server is that the systemd service included “-H fd://”, but the daemon config already contained hosts and the daemon stopped because this parameter can be defined in either the daemon config or as a command line argument, but not in both. At least not in recent Docker versions.

Thanks you, I’ll get the logs and post them here.

I see this

Dec 04 03:30:29 ip-10-23-22-55 systemd[1]: Starting Docker Application Container Engine...
Dec 04 03:30:29 ip-10-23-22-55 dockerd[1035]: Status: unknown shorthand flag: 'g' in -g
Dec 04 03:30:29 ip-10-23-22-55 dockerd[1035]: See 'dockerd --help'., Code: 125
Dec 04 03:30:29 ip-10-23-22-55 systemd[1]: docker.service: Main process exited, code=exited, status=1/FAILURE
Dec 04 03:30:29 ip-10-23-22-55 systemd[1]: docker.service: Failed with result 'exit-code'.
Dec 04 03:30:29 ip-10-23-22-55 systemd[1]: Failed to start Docker Application Container Engine.
Dec 04 03:30:31 ip-10-23-22-55 systemd[1]: docker.service: Scheduled restart job, restart counter is at 2.
Dec 04 03:30:31 ip-10-23-22-55 systemd[1]: Stopped Docker Application Container Engine.

I found the issue.

# cat /etc/systemd/system/docker.service.d/docker.root.conf 
[Service]
ExecStart=
ExecStart=/usr/bin/dockerd -g /docker -H fd://

I removed the -g /docker since the data-root directory is already designated in this file,

# cat /etc/docker/daemon.json
{
  "data-root": "/docker"
}

Did s systemctl daemon-reload and the systemctl start docker command worked. Thanks all!