2

This is my first time working with docker. Please take note that I am using Docker Desktop on windows with WSL 2.

I am working with a peace of legacy software that needs to connect to the docker daemon to spin up new containers. The setup instruction for this software say I need to set DOCKER_OPTS='-H tcp://127.0.0.1:4243 -H unix:///var/run/docker.sock'. As of right now I havent found a way to set DOCKER_OPTS in windows, but adding it to the hosts should work.

The issue that I'm having is when I try to add hosts to the C:\Users\<User>\.docker\daemon.json file (or through the ui) docker fails to start. It doesn't matter what I add into the hosts object, just the hosts object causes docker to crash on startup.

From what I understand it looks like this is a known issue, however all solutions that I can found seem unanswered or for Linux and not windows.

I have tried using the -H command, It doesn't look like anything changes with the command.

How can I add hosts to the daemon.json file without docker crashing or, at the least, set the DOCKER_OPTS value?

I am on: Docker version 19.03.8, build afacb8b Windows Build 19041.329 WSL 2

I have looked through these.

DOCKER_OPTS do not work in config file /etc/default/docker

Unable to start docker after configuring hosts in daemon.json

Enable Remote Docker API on Windows Host - Adding daemon.json breaks docker

Change "hosts" / "-h" Docker for Windows in daemon.json

docker stack trace:

Docker.Core.DockerException:
Failed to start
   at Docker.LinuxkitDaemonStartup.<StartAsync>d__5.MoveNext() in C:\workspaces\stable-2.3.x\src\github.com\docker\pinata\win\src\Docker.Desktop\LinuxkitDaemonStartup.cs:line 59
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Docker.Engines.WSL2.LinuxWSL2Engine.<DoStartAsync>d__23.MoveNext() in C:\workspaces\stable-2.3.x\src\github.com\docker\pinata\win\src\Docker.Desktop\Engines\WSL2\LinuxWSL2Engine.cs:line 149
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Docker.ApiServices.StateMachines.TaskExtensions.<WrapAsyncInCancellationException>d__0.MoveNext() in C:\workspaces\stable-2.3.x\src\github.com\docker\pinata\win\src\Docker.ApiServices\StateMachines\TaskExtensions.cs:line 29
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Docker.ApiServices.StateMachines.StartTransition.<DoRunAsync>d__5.MoveNext() in C:\workspaces\stable-2.3.x\src\github.com\docker\pinata\win\src\Docker.ApiServices\StateMachines\StartTransition.cs:line 67
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at Docker.ApiServices.StateMachines.StartTransition.<DoRunAsync>d__5.MoveNext() in C:\workspaces\stable-2.3.x\src\github.com\docker\pinata\win\src\Docker.ApiServices\StateMachines\StartTransition.cs:line 92
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Docker.ApiServices.StateMachines.EngineStateMachine.<StartAsync>d__14.MoveNext() in C:\workspaces\stable-2.3.x\src\github.com\docker\pinata\win\src\Docker.ApiServices\StateMachines\EngineStateMachine.cs:line 72
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Docker.Engines.Engines.<RestartAsync>d__29.MoveNext() in C:\workspaces\stable-2.3.x\src\github.com\docker\pinata\win\src\Docker.Desktop\Engines\Engines.cs:line 274

Edit: It looks like the default behavior of docker is to have the host flag set on startup to a default value. Docker will crash if you set the hosts object in the config file because is will have an object conflict where you trying to set something that already exists. I have no idea why docker did it this way but ya, docker is working as intended, not working at all.

The solution that I used was linux, yes that's right, don't use windows as there is no way to set the hosts object. In linux you can clear the startup config on launch and then set the hosts object without issue.

Spik330
  • 502
  • 4
  • 6
  • 19

2 Answers2

2

I'm also experiencing this issue. My current workaround is to forward to the port outside of docker. I run this command in an admin command prompt:

netsh interface portproxy add v4tov4 listenport=2375 listenaddress=192.168.1.20 connectport=2375 connectaddress=127.0.0.1

It even sticks around after restarts which you can check by:

netsh interface portproxy show all

While this doesn't fix that docker crashes on editing the host file, it will allow you to proxy the daemon host to a different port.

user3704512
  • 151
  • 10
  • This was the only thing that worked for me. However, I had to add a firewall rule to be able to connect from other machines. See here: https://davidhamann.de/2019/06/20/setting-up-portproxy-netsh/ – rolve Feb 27 '21 at 10:41
1

I had a similar error when I tried to enable connecting to the Docker server via TCP. For this, I added a "host" configuration to the /etc/docker/daemon.json file, with this configuration Docker started to fail

After researching, I found the following information in the official Docker documentation:

 "If you use a daemon.json file and also pass options to the dockerd command manually or using start-up scripts, and these options conflict, Docker fails to start with an error such as:"

Docker Doc

With this information, I was able to solve my problem. I modified my Docker startup command (in my case, I modified the /lib/systemd/system/docker.service file), removing the -H parameter, leaving the command as follows: ExecStart=/usr/bin/dockerd

In this way I managed to solve my problem to start Docker and enable connection to Docker server via TCP!

In my case the TCP connection is very important because I run Docker inside WSL on Windows. (Using Docker Desktop directly on Windows, even if integrated with WSL, consumes significantly more resources.)

I hope this helps!