Zakaria Shahed
Zakaria Shahed

Reputation: 2697

Cannot connect to the Docker daemon on bash on Ubuntu windows

I am able to install docker, docker-compose and docker-machine

However when I try to run

root@DESKTOP-51NFMIM:~# docker ps
Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?

Is there any suggestion for solving this problem?

Upvotes: 58

Views: 108492

Answers (12)

shreyansp
shreyansp

Reputation: 793

DO NOT USER OTHER METHODS IN LATEST DOCKER DESKTOP

I had the same "Cannot connect to the Docker daemon" issue when trying to use docker inside WSL2.

I believe this has made been simpler in latest version of Docker Desktop.

Ref: https://learn.microsoft.com/en-us/windows/wsl/tutorials/wsl-containers

  • Update to latest (Mine is Docker Desktop 4.3.2 (72729))

    • May work in an earlier version, haven't checked when exactly the feature was made available.
  • Enable WSL 2 based engine: Settings > General > Use the WSL 2 based engine

  • Enable WSL integration in Settings > Resources > WSL Integration (attaching a pic, as the menu settings may change for different versions)

    • NOTE: strangely, even though my default WSL distro was Ubuntu, I had to explicitly enable it for Ubuntu for it to work
    • may need to restart Docker Desktop
    • I also did NOT need to turn on "Expose daemon on tcp://localhost:2375 without TLS" for it to work
  • NOTE: I initially attempted the old method of adding the 3 env variables as suggested in other posts.

    • However, using the latest Docker Desktop, this wasn't necessary, and removed the 3 variables from .bashrc
    $ echo >> ~/.bashrc <<EOF
    # Connect to Docker on Windows
    export DOCKER_CERT_PATH=/mnt/c/Users/YOUR_USERNAME/.docker/machine/certs
    export DOCKER_TLS_VERIFY=1
    export DOCKER_HOST='tcp://0.0.0.0:2375'
    EOF
    $ source ~/.bashrc
    

enter image description here

Upvotes: 5

zynth666
zynth666

Reputation: 68

Running WSL2 and WSL 2 based engine (not Hyper-V)

For some reason this toggle button under "Resources" -> "WSL Integration" was turned off. The moment I turned it on again everything worked as expected.

Make sure it is turned on for you before digging any deeper.

I assume I haven't set my Ubuntu distro as my default distro so the checkbox wasn't doing anything.

Resources panel

Upvotes: 1

Igor Micev
Igor Micev

Reputation: 1652

Exposing your Docker Host should follow these steps:

#1. create the following directory if it doesn't exist

sudo mkdir -p /etc/systemd/system/docker.service.d

#2. create options.conf configuration file with the following content

sudo nano /etc/systemd/system/docker.service.d/options.conf

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

#3. reload the systemd daemon

sudo systemctl daemon-reload

#4. restart docker

sudo systemctl restart docker

If you want to have an encrypted connection, then you should replace port 2375 with 2376, and set up certificates and daemon.json configuration files in /etc/docker/certs/ and /etc/docker/daemon.json. Example:

{
    "tlsverify": true,
    "tlscacert": "/etc/docker/certs/ca-cert.pem",
    "tlscert": "/etc/docker/certs/server-cert.pem",
    "tlskey": "/etc/docker/certs/server-key.pem",
    "host": "tcp://0.0.0.0:2376"
}

Upvotes: 0

UltimaWeapon
UltimaWeapon

Reputation: 790

The accepted answer is mostly correct. However, I wanted to specify that when using WSL2 + Docker Desktop:

  • You must unset DOCKER_HOST which was previously needed in WSL1
    • Mine was defined in ~/.bashrc in both Windows and WSL.
    • Delete in both. Also delete in Windows env variables via Control Panel.
    • Check with env | grep -i docker to make sure it's gone.
  • You must also set the correct settings in Docker Desktop
    • Uncheck Export daemon on tcp://localhost:2375 without TLS
    • Check Use the WSL2 based engine
    • Resources -> WSL Integration -> Check Enable integration with my default WSL distro

Now, you can do a simple docker info to check if you're running the same server version in WSL and in Windows (Powershell).

Upvotes: 3

Delink
Delink

Reputation: 161

I had the same issue, took me so many hours to figure it, I realized that running docker ps without root privileges gives this error, If any one else is experiencing the same you could try sudo docker ps it worked for me

Upvotes: 4

user8458126
user8458126

Reputation: 221

In addition to what was already said, I would like to share with you some issues that I had while setting up and the solutions that eventually allowed me to start working with a WSL2/Docker Desktop combo. This is not a complete guide, just another source of troubleshooting!

My goal was

  1. Start Experimenting with Docker.
  2. Setup my environment so it is as recommended and most comfortable.
  3. Basically to be able to work in a Linux Context while working with Docker Desktop.

How I went about it

  1. I upgraded my PC from Win 10 Home to Win 10 Pro, to avoid various issues I read that people were having with the Home edition.
  2. I had Docker Toolbox installed and then deleted it as well as the Oracle VM install I had on my PC. I had various small issues, read online that it's best to remove Oracle VM as well.
  3. I installed Docker Desktop.

Issues and their solutions

    1. A few of the environment variables from the old Docker Tools installation remained. This caused Docker Desktop to misbehave.
    Solution: I deleted all of them. (Any variable name that mentioned Docker)
    2. When trying to connect to Docker on localhost:2375 via Ubuntu WSL2 got the famous "...is docker even running" error. Worth mentioning that on cmd docker was working properly at this stage.
    Solution:
      - Purge a previous installation of Docker I had on my Ubuntu WSL2.
      - Make sure to delete all and any environment variables that mentioned Docker on my ubuntu WSL2.
      - Stop Hyper-v, Rebooted, Started Hyper-V Rebooted on Windows.
      - Install the latest version of Docker via `apt-get`.

Now running on Ubuntu WSL2 "docker context ls" to check my setup should result in:

NAME        DESCRIPTION                               DOCKER ENDPOINT               KUBERNETES ENDPOINT   ORCHESTRATOR
default *   Current DOCKER_HOST based configuration   unix:///var/run/docker.sock                         swarm

Running Docker Info from Ubuntu WSL2:

Client:
 Context:    default
 Debug Mode: false
 Plugins:
  app: Docker App (Docker Inc., v0.9.1-beta3)
  buildx: Build with BuildKit (Docker Inc., v0.5.0-docker)
  scan: Docker Scan (Docker Inc., v0.5.0)

Server:
 Containers: 3
  Running: 0
  Paused: 0
  Stopped: 3
 Images: 3...

Upvotes: 0

only needs add a env variable in your system

enter image description here

Upvotes: -1

rob2universe
rob2universe

Reputation: 7583

If you are using docker desktop for windows (and do not require TLS for the connection) then go to the docker desktop setting, general section and enabled the checkbox "Expose daemon on tcp://localhost:2375 without TLS".

Update: in newer Docker desktop versions the WSL integration has moved to the resources section.

Upvotes: 1

for me this worked for WSL for windows:

  • 1> go to: Turn Windows features on or off
  • 2> deselect "Hyper-V", restart,
  • 3> go to "Turn Windows features on or off" again,
  • 4> select "Hyper-V" again
  • and restart a last time. Afterwards docker was reachable again.

assuming you have installed docker desktop for windows and Settings->General->Expose daemon on tcp://localhost:2375 without TLS is ticked

Upvotes: 13

Snake
Snake

Reputation: 531

The Docker client and server can now be installed and run purely in WSL without Docker Desktop for Windows if you are running Windows 10 version 1803 or greater. I have it working on the following WSL:

OS: Ubuntu 18.04 bionic [Ubuntu on Windows 10]
Kernel: x86_64 Linux 4.4.0-17763-Microsoft

Simply follow the same instructions to install on Ubuntu but make sure to choose a specific version to install. Presently, version 18.06.1~ce~3-0~ubuntu works fine but later versions up to 5:18.09.6~3-0~ubuntu-bionic have an issue with starting up a container. The following command will install the latest working version:

apt-get install docker-ce=18.06.1~ce~3-0~ubuntu

To get the Docker server running in WSL after installation, close all open terminals and start a new Ubuntu terminal as administrator (i.e., right click the Ubuntu shortcut and click 'Run as administrator'). Finally, run the following commands:

sudo cgroupfs-mount
sudo service docker start

sudo service docker start will have to be run each time Windows is rebooted. However, if you wish to avoid that, you can automate it using the Task Scheduler and a shell script by following the steps listed here.

Test that everything is working using:

docker run hello-world

Reference: https://medium.com/faun/docker-running-seamlessly-in-windows-subsystem-linux-6ef8412377aa

Upvotes: 32

Rami Sarieddine
Rami Sarieddine

Reputation: 5432

Found the solution on this post: https://blog.jayway.com/2017/04/19/running-docker-on-bash-on-windows/

Running docker against an engine on a different machine is actually quite easy, as Docker can expose a TCP endpoint which the CLI can attach to.

This TCP endpoint is turned off by default; to activate it, right-click the Docker icon in your taskbar and choose Settings, and tick the box next to “Expose daemon on tcp://localhost:2375 without TLS”.

With that done, all we need to do is instruct the CLI under Bash to connect to the engine running under Windows instead of to the non-existing engine running under Bash, like this:

$ docker -H tcp://0.0.0.0:2375 images

There are two ways to make this permanent – either add an alias for the above command or export an environment variable which instructs Docker where to find the host engine (NOTE: make sure to use single apostrophe's below):

$ echo "export DOCKER_HOST='tcp://0.0.0.0:2375'" >> ~/.bashrc
$ source ~/.bashrc

Now, running docker commands from Bash works just like they’re supposed to.

$ docker run hello-world

Successful response:

Hello from Docker!This message shows that your installation appears to be working correctly.

Upvotes: 70

VonC
VonC

Reputation: 1324168

Note: if you are using the Ubuntu from WSL (Windows Subsystem for Linux), do understand that the docker client is working, not the docker server (daemon).

See Microsoft/WSL issue 2114 and this thread.

For the server, you would still need to use only Docker for Windows and its Hyper-V VM.

Then, Microsoft/WSL issue 2102 adds:

I was able to make TLS work from inside WSL by changing DOCKER_CERT_PATH environment variable (which I got from running eval $(docker-machine.exe env --shell bash)) from "C:\C:\Users\mmarchini\.docker\machine\machines\default" to "/mnt/c/Users/mmarchini/.docker/machine/machines/default/" .
At least docker build seems to be working now, I'll try using docker-compose later.

See this script (from Matheus Marchini) to launch a docker-machine bash with the right setting:

#!/usr/bin/env python3

from subprocess import run, PIPE

completed_process = run(["docker-machine.exe", "env", "--shell", "bash"], stdout=PIPE)

docker_env = completed_process.stdout.decode("ascii")

for line in docker_env.split("\n"):
    if "DOCKER_CERT_PATH" in line:
        env_var, path, _ = line.split('"')
        path = path.replace("\\", "/")
        drive, path = path.split(":", 1)
        path = "/mnt/{}{}".format(drive.lower(), path)
        line = '{}"{}"'.format(env_var, path)
print(line)

Upvotes: 7

Related Questions