28

I have changed /etc/default/docker with DOCKER_OPTS="-H tcp://127.0.0.1:2375 -H unix:///var/run/docker.sock" (docker version 1.4.1 in ubuntu 14.04), but it do not take any effect for me (not listening at port 2375). It seems that docker do not read this initial config file because I found export http_proxy enviroment do not work too.

Only sudo docker -H tcp://127.0.0.1:2375 -H unix:///var/run/docker.sock -d works.

It really confused me!

Forage
  • 2,726
  • 2
  • 18
  • 20
seanlook
  • 907
  • 2
  • 9
  • 13

9 Answers9

38

According to docker documentation, The recommended way to configure the daemon flags and environment variables for your Docker daemon is to use a systemd drop-in file.

So, for this specific case, do the following:

  1. Use the command sudo systemctl edit docker.service to open an override file for docker.service in a text editor.

  2. Add or modify the following lines, substituting your own values.

    [Service]
    ExecStart=
    ExecStart=/usr/bin/dockerd -H tcp://127.0.0.1:2375 -H unix:///var/run/docker.sock
    
  3. Save the file.

  4. Reload the systemctl configuration.

     $ sudo systemctl daemon-reload
    
  5. Restart Docker:

     $ sudo systemctl restart docker.service
    
  6. Check to see whether the change was honored by reviewing the output of netstat to confirm dockerd is listening on the configured port.

    $ sudo netstat -lntp | grep dockerd
    tcp        0      0 127.0.0.1:2375          0.0.0.0:*               LISTEN      3758/dockerd
    
Camilo Silva
  • 8,283
  • 4
  • 41
  • 61
  • The recommended way didn't completely work for me because -H is already set. I had to also use part of the previous method https://stackoverflow.com/a/44055659/467082 – blackbox Feb 12 '18 at 18:20
  • Note that the duplicate `ExecStart=` line is not a typo. It is required to force systemd to replace the exec command and not add a new one. – David Dombrowsky May 16 '23 at 20:15
15

See here for later versions of Debian/Ubuntu that use systemd.

This link explains how to correctly modify a systemd unit file to work with DOCKER_OPTS: https://github.com/docker/docker/issues/9889

Essentially you create a /etc/systemd/system/docker.service.d/docker.conf file and specify your overrides there.

I had to do something like the following in the aforementioned file to launch docker with the DOCKER_OPTS environment variable in a systemd environment:

[Unit]
Description=Docker Application Container Engine
Documentation=https://docs.docker.com
After=network.target docker.socket
Requires=docker.socket

[Service]
EnvironmentFile=-/etc/default/docker
ExecStart=
ExecStart=/usr/bin/docker -d $DOCKER_OPTS -H fd://
MountFlags=slave
LimitNOFILE=1048576
LimitNPROC=1048576
LimitCORE=infinity

[Install]
WantedBy=multi-user.target

Current docker install process seems to neglect the systemd unit file.

Ross
  • 472
  • 1
  • 4
  • 11
  • I was successful with a more minimal docker.conf that just overrides the parts of `/lib/systemd/system/docker.service` that I wanted to change. – Aneel Nov 05 '16 at 20:54
8

I had the same issue.

Ubuntu 14.10 uses systemd instead of sysv-init/upstart. Maybe you should look into /lib/systemd/system/docker.service to change the options.

Luís Cruz
  • 14,780
  • 16
  • 68
  • 100
kuttifunk
  • 141
  • 3
  • 8
  • For completeness, you'll then need to issue the following commands (with `sudo` if necessary) `systemctl daemon-reload` and `service docker restart` – David Carboni Jul 06 '15 at 15:42
  • Also, `journalctl` is a useful command for seeing any messages if the service fails to start. – David Carboni Jul 06 '15 at 16:04
3

I've just run into the "same" problem.

I noticed that all the options in the /etc/default/docker are actually commented out by default.

I removed the # in front of DOCKER_OPTS, restarted and it worked as intended.

I think previous docker versions (1.3) didn't have these options commented out by default, at least I can't remember having to remove the #-sign.

Daniel
  • 117
  • 1
  • 11
1

After checking docker source code (config.go and flags.go), I would say that the options you can pass in $DOCKER_OPTS are options for docker itself, and -H or --host is an option for docker daemon. As a workaround to solve your problem, you can edit the init script file you are using to include the -H option there. For example:

  • If you are using upstart init system, edit the file /etc/init/docker.conf changing the exec line with exec "$DOCKER" -H "tcp://127.0.0.1:2375" -H "unix:///var/run/docker.sock" -d $DOCKER_OPTS
  • If you are using sysvinit init system, edit the file /etc/init.d/docker changing the starting lines with something like:

Use this command:

start-stop-daemon --start --background \
    --no-close \
    --exec "$DOCKER" \
    --pidfile "$DOCKER_SSD_PIDFILE" \
    --make-pidfile \
    -- \
        -H "tcp://127.0.0.1:2375" \
        -H "unix:///var/run/docker.sock" \
        -d -p "$DOCKER_PIDFILE" \
        $DOCKER_OPTS >> \
        "$DOCKER_LOGFILE" 2>&1 
    log_end_msg $?
    ;;
muehsi
  • 588
  • 3
  • 19
Javier Cortejoso
  • 8,851
  • 3
  • 26
  • 27
  • Shoud `/etc/default/docker` be the docker daemon startup configuration file? Many articles (include [official docs](https://docs.docker.com/articles/networking/)) told me to set `DOCKER_OPTS` in `/etc/default/docker` not in bash enviroment. – seanlook Jan 06 '15 at 03:06
  • Yes, it should. And docker init scripts load /etc/default/docker. I have tested and in my environment (Linux Mint 17 -Ubuntu 14.04- and docker 1.4.1) works if I set `DOCKER_OPTS="-H tcp://127.0.0.1:2375 -H unix:///var/run/docker.sock"`. I'm using the default init system (upstart), and you should be using the same. Check the file `/etc/init/docker.conf` to see if it's loading the config file. It should appear: `if [ -f /etc/default/$UPSTART_JOB ]; then . /etc/default/$UPSTART_JOB; fi` – Javier Cortejoso Jan 06 '15 at 11:03
  • `/etc/default/docker` works now for me after reboot my ubuntu server. – seanlook Jan 13 '15 at 02:13
  • @Sean I find that after you make a change to the /etc/default/docker there is no need to restart the Ubuntu machine, just simply restart the docker service... – user3614014 Feb 23 '15 at 12:52
1

In reply to the systemd comments in combination with Ubuntu: Ubuntu 14.04 still uses Upstart, so the changes to /etc/default/docker should have had the desired effect. It isn't until 15.04 that Ubuntu started using systemd by default.

In case you have Ubuntu 15.04 or later and thus need to use systemd, or if you explicitly choose to use systemd before 15.04, the correct and simplest way to get the desired effect of the OP, a TCP socket, would be:

  1. Create the file /etc/systemd/system/docker.service.d/docker-tcp.conf
  2. Add the contents below
  3. Execute sudo systemctl daemon-reload
  4. Execute sudo systemctl restart docker

File contents:

[Service]
ExecStart=
ExecStart=/usr/bin/docker daemon --host=tcp://127.0.0.1:2375
Forage
  • 2,726
  • 2
  • 18
  • 20
1

Using the platform independent daemon-configuration-file seems much cleaner.

Add "hosts" to your /etc/docker/daemon.json (or /custom/path/to/daemon/config.json if you're using --config-file option while starting the dockerd):

{
...
"hosts": ["tcp://127.0.0.1:2375", "unix:///var/run/docker.sock"],
...
}

restart the daemon:

sudo systemctl restart docker
Kashyap
  • 15,354
  • 13
  • 64
  • 103
0

I had a similar challenge. When I started looking to begin moving some systems from Ubuntu 14.04 to Ubuntu 16.04. My goal was to use one dockerd configuration file with dockerd flags (DOCKER_OPTS) for both Ubuntu 16.04 (systemd) and Ubuntu 14.04 (Upstart) other than /etc/docker/daemon.json. I chose not to use /etc/docker/daemon.json for docker daemon configuration because json does not support comments.

I wanted a systemd design to use an override file, which only modifies dockerd flags. It uses the default Docker systemd configuration file (/lib/systemd/system/docker.service) for other Docker settings. Another objective was to customise systemd on each system after each change or boot.

It solves my challenge. It may help you.

https://github.com/BradleyA/docker-scripts/tree/master/dockerd-configuration-options

git clone https://github.com/BradleyA/docker-scripts
cd docker-scripts/dockerd-configuration-options
Bradley Allen
  • 596
  • 5
  • 5
0

Well i have face a lot of issue in spinning on demand slave from jenkins because of docker daemon port restrictions.

so i did

sudo systemctl edit docker.service

Added below section

[Service]
ExecStart=
ExecStart=/usr/bin/docker daemon --host=tcp://0.0.0.0:2375

Save the file

run below command

$ sudo systemctl daemon-reload
$ sudo systemctl restart docker.service

**NOTE: This will expose your daemon publicly and anyone with your ip and port can do any thing with your docker daemon **