title: Docker (compose): connection refused without sudo
body: <p>All was fine until &quot;something&quot;. After that &quot;something&quot;, I could use <code>docker</code> and <code>docker-compose</code> with <code>sudo</code> but without it the <code>docker</code> would hang indefinitely and the <code>docker-compose</code> would return connection stack traces.</p>
  <p>Doesn't work:</p>
  <ul>
  <li>Running <code>docker</code> without sudo hangs indefinitely. Temporarily fixed by exporting <code>DOCKER_HOST=tcp://127.0.0.1:2375</code>.</li>
  <li><code>docker-compose</code> without sudo returns with <code>connection refused</code>. It's not about connecting to specific container but for all basic commands such as <code>docker-compose ps</code>.</li>
  </ul>
  <p>Proof that user belongs to <code>docker</code> group:</p>
  <pre><code>kretyn@junk$ groups kretyn
  kretyn : kretyn adm cdrom sudo dip plugdev lpadmin lxd sambashare docker
  </code></pre>
  <h3>Solution hints</h3>
  <p>I had a docker context activated that was pointing at existing but inactive host. Removing that context and unsetting <code>DOCKER_HOST</code> allowed the <code>docker</code> command to work correctly. That's a surprise since I reinstalled (purged everything I could find) the docker before writing this question.</p>
  <p>Removing context isn't sufficient. Docker Compose required explicit <code>docker context use default</code> otherwise it wouldn't understand.</p>
  <p>The problem with <code>docker-compose ps</code> is still present.</p>
  <h2>Stack trace on <code>docker-compose</code></h2>
  <pre><code>kretyn@junk$ docker-compose ps
  Traceback (most recent call last):
    File &quot;urllib3/connection.py&quot;, line 159, in _new_conn
    File &quot;urllib3/util/connection.py&quot;, line 84, in create_connection
    File &quot;urllib3/util/connection.py&quot;, line 74, in create_connection
  ConnectionRefusedError: [Errno 111] Connection refused
  
  During handling of the above exception, another exception occurred:
  
  Traceback (most recent call last):
    File &quot;urllib3/connectionpool.py&quot;, line 670, in urlopen
    File &quot;urllib3/connectionpool.py&quot;, line 392, in _make_request
    File &quot;http/client.py&quot;, line 1255, in request
    File &quot;http/client.py&quot;, line 1301, in _send_request
    File &quot;http/client.py&quot;, line 1250, in endheaders
    File &quot;http/client.py&quot;, line 1010, in _send_output
    File &quot;http/client.py&quot;, line 950, in send
    File &quot;urllib3/connection.py&quot;, line 187, in connect
    File &quot;urllib3/connection.py&quot;, line 171, in _new_conn
  urllib3.exceptions.NewConnectionError: &lt;urllib3.connection.HTTPConnection object at 0x7f762c4e3e20&gt;: Failed to establish a new connection: [Errno 111] Connection refused
  
  During handling of the above exception, another exception occurred:
  
  Traceback (most recent call last):
    File &quot;requests/adapters.py&quot;, line 439, in send
    File &quot;urllib3/connectionpool.py&quot;, line 726, in urlopen
    File &quot;urllib3/util/retry.py&quot;, line 446, in increment
  urllib3.exceptions.MaxRetryError: HTTPConnectionPool(host='127.0.0.1', port=2375): Max retries exceeded with url: /version (Caused by NewConnectionError('&lt;urllib3.connection.HTTPConnection object at 0x7f762c4e3e20&gt;: Failed to establish a new connection: [Errno 111] Connection refused'))
  
  During handling of the above exception, another exception occurred:
  
  Traceback (most recent call last):
    File &quot;docker/api/client.py&quot;, line 214, in _retrieve_server_version
    File &quot;docker/api/daemon.py&quot;, line 181, in version
    File &quot;docker/utils/decorators.py&quot;, line 46, in inner
    File &quot;docker/api/client.py&quot;, line 237, in _get
    File &quot;requests/sessions.py&quot;, line 543, in get
    File &quot;requests/sessions.py&quot;, line 530, in request
    File &quot;requests/sessions.py&quot;, line 643, in send
    File &quot;requests/adapters.py&quot;, line 516, in send
  requests.exceptions.ConnectionError: HTTPConnectionPool(host='127.0.0.1', port=2375): Max retries exceeded with url: /version (Caused by NewConnectionError('&lt;urllib3.connection.HTTPConnection object at 0x7f762c4e3e20&gt;: Failed to establish a new connection: [Errno 111] Connection refused'))
  
  During handling of the above exception, another exception occurred:
  
  Traceback (most recent call last):
    File &quot;docker-compose&quot;, line 3, in &lt;module&gt;
    File &quot;compose/cli/main.py&quot;, line 80, in main
    File &quot;compose/cli/main.py&quot;, line 189, in perform_command
    File &quot;compose/cli/command.py&quot;, line 60, in project_from_options
    File &quot;compose/cli/command.py&quot;, line 152, in get_project
    File &quot;compose/cli/docker_client.py&quot;, line 41, in get_client
    File &quot;compose/cli/docker_client.py&quot;, line 170, in docker_client
    File &quot;docker/api/client.py&quot;, line 197, in __init__
    File &quot;docker/api/client.py&quot;, line 221, in _retrieve_server_version
  docker.errors.DockerException: Error while fetching server API version: HTTPConnectionPool(host='127.0.0.1', port=2375): Max retries exceeded with url: /version (Caused by NewConnectionError('&lt;urllib3.connection.HTTPConnection object at 0x7f762c4e3e20&gt;: Failed to establish a new connection: [Errno 111] Connection refused'))
  [46145] Failed to execute script docker-compose
  </code></pre>
  <h2>Versions/builds</h2>
  <h3>Linux distro</h3>
  <pre><code>kretyn@junk$ uname -a
  Linux junk 5.8.0-43-generic #49~20.04.1-Ubuntu SMP Fri Feb 5 09:57:56 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux
  </code></pre>
  <h3>Docker</h3>
  <pre><code>kretyn@junk$ sudo docker system info
  Client:
   Context:    default
   Debug Mode: false
   Plugins:
    app: Docker App (Docker Inc., v0.9.1-beta3)
    buildx: Build with BuildKit (Docker Inc., v0.5.1-docker)
  
  Server:
   Containers: 9
    Running: 8
    Paused: 0
    Stopped: 1
   Images: 44
   Server Version: 20.10.3
   Storage Driver: overlay2
    Backing Filesystem: extfs
    Supports d_type: true
    Native Overlay Diff: true
   Logging Driver: json-file
   Cgroup Driver: cgroupfs
   Cgroup Version: 1
   Plugins:
    Volume: local
    Network: bridge host ipvlan macvlan null overlay
    Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog
   Swarm: inactive
   Runtimes: io.containerd.runc.v2 io.containerd.runtime.v1.linux runc
   Default Runtime: runc
   Init Binary: docker-init
   containerd version: 269548fa27e0089a8b8278fc4fc781d7f65a939b
   runc version: ff819c7e9184c13b7c2607fe6c30ae19403a7aff
   init version: de40ad0
   Security Options:
    apparmor
    seccomp
     Profile: default
   Kernel Version: 5.8.0-43-generic
   Operating System: Ubuntu 20.04.2 LTS
   OSType: linux
   Architecture: x86_64
   CPUs: 12
   Total Memory: 15.29GiB
   Name: meland
   ID: 3PE6:4AWU:LGRR:HOBJ:XUCJ:ER4H:ZEF5:MWUJ:COEG:WPK5:YH2U:FKUF
   Docker Root Dir: /var/lib/docker
   Debug Mode: false
   Registry: https://index.docker.io/v1/
   Labels:
   Experimental: false
   Insecure Registries:
    127.0.0.0/8
   Live Restore Enabled: false
  
  WARNING: No blkio weight support
  WARNING: No blkio weight_device support
  </code></pre>
  <h3>Docker-compose</h3>
  <pre><code>kretyn@junk$ sudo docker-compose version
  docker-compose version 1.28.0, build d02a7b1a
  docker-py version: 4.4.1
  CPython version: 3.9.0
  OpenSSL version: OpenSSL 1.1.1d  10 Sep 2019
  
  </code></pre>