18

I'm trying to configure docker (version 17.03.1-ce) in ubuntu 16.04 using configuration file /etc/docker/daemon.json to add an host:

{
  "debug": true,
  "hosts": ["tcp://0.0.0.0:1234", "unix:///var/run/docker.sock"],
  "dns"  : ["8.8.8.8","8.8.4.4"]
}

when I try to restart docker.. it fails

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

watching on systemctl status docker.service:

Starting Docker Application Container Engine...
docker-slave-ubuntu-build dockerd[24806]: unable to configure the Docker daemon with file /etc/docker/daemon.json: 
the following directives are specified both as a flag and in the configuration file: 
hosts: (from flag: [fd://], from file: [tcp://0.0.0.0:4243 unix:///var/run/docker.sock])

Where I can remove the mentioned flag ? I have to modify maintainer's script ?

Gas
  • 420
  • 1
  • 5
  • 10
  • What maintainer script? Can you elaborate on how you installed Docker so someone can try and reproduce the problem? – Andy Shinn May 18 '17 at 16:07

5 Answers5

23

It looks like this is an issue merging configuration from both the command line and configuration file. The default systemd unit file is specifying -H fd:// and it conflicts with your tcp://0.0.0.0:1234 and unix:///var/run/docker.sock.

There are a number of GitHub issues on the subject:

They don't seem to consider this a bug. But it is definitely an annoyance. A workaround is to copy the default unit file and remove the -H fd:// from it:

$ sudo cp /lib/systemd/system/docker.service /etc/systemd/system/
$ sudo sed -i 's/\ -H\ fd:\/\///g' /etc/systemd/system/docker.service
$ sudo systemctl daemon-reload
$ sudo service docker restart
Andy Shinn
  • 26,561
  • 8
  • 75
  • 93
23

For systemd, my preferred method is to deploy a simple override file (you may need to first create the directory):

$ cat /etc/systemd/system/docker.service.d/override.conf
# Disable flags to dockerd, all settings are done in /etc/docker/daemon.json
[Service]
ExecStart=
ExecStart=/usr/bin/dockerd

This removes the -H ... default flag from dockerd along with any other options and lets you manage docker from the daemon.json file. This also allows docker to make changes to their startup scripts as long as they don't modify the ExecStart and you'll continue to receive those changes without maintaining your own copy of the docker.service.

After creating this file, run systemctl daemon-reload; systemctl restart docker.

BMitch
  • 231,797
  • 42
  • 475
  • 450
  • Wouldn't you have to copy `/lib/systemd/system/docker.service` to `/etc/systemd/system/` as the default unit is in a built-in directory? – Andy Shinn May 19 '17 at 19:48
  • No, you can override a setting in the /lib service without copying it to /etc. But you do need the empty `ExecStart=` to unset the value so you can reconfigure it. – BMitch May 19 '17 at 19:50
4

I found this on the Docker docs and it worked on Docker 18.09.1 and Centos 8:

To work around this problem, create a new file /etc/systemd/system/docker.service.d/docker.conf with the following contents, to remove the -H argument that is used when starting the daemon by default.

[Service]
ExecStart=
ExecStart=/usr/bin/dockerd

Then reload

systemctl daemon-reload

The reason is:

Docker listens on a socket by default. On Debian and Ubuntu systems using systemd, this means that a host flag -H is always used when starting dockerd. If you specify a hosts entry in the daemon.json, this causes a configuration conflict (as in the above message) and Docker fails to start.

Here is the link: https://docs.docker.com/config/daemon/#troubleshoot-conflicts-between-the-daemonjson-and-startup-scripts

Amaury Gz
  • 41
  • 5
1

In my case I tried to add both the daemon.json under /etc/docker and a *.conf file under /etc/systemd/system/docker.service.d. It turned out it was enough to have a .conf file only (in my case called override.conf):

[Service]
ExecStart=
ExecStart=/usr/bin/dockerd -H tcp://0.0.0.0:2375

this way I could expose the docker socket.

Laura Liparulo
  • 2,849
  • 26
  • 27
0

I had copied the daemon.json from a website. After running

sudo systemctl stop docker
/usr/sbin/dockerd

it showed me a better error message that stated that I had a strange invisible character in the daemon.json file