Cannot Connect to Docker Daemon: Fixes for Linux, macOS, and WSL

Cannot Connect to Docker Daemon: Fixes for Linux, macOS, and WSL

You run docker ps, docker run hello-world, or docker compose up and Docker answers with this: Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running? The Docker CLI is installed, but it cannot reach the background service that actually creates containers.

The fastest fix is not to reinstall Docker. First find out whether the daemon is stopped, your CLI is using the wrong context, or your user cannot access the Docker socket.

Quick answer

Quick answer: Run docker info. If the client section appears but the server section fails, the Docker CLI is working but the daemon connection is broken. Then check the service, context, socket path, and user permissions in that order.

docker info

This error is about the relationship between the Docker client and the Docker daemon. If you need the background concept first, read the Docker Engine explainer; the short version is that the client sends requests and the daemon does the container work.

Fast diagnosis checklist

Use this checklist before changing anything. It separates service failures from context and permission problems.

docker context ls
docker context show
echo $DOCKER_HOST
systemctl status docker
ls -l /var/run/docker.sock
id -nGCode language: PHP (php)
  • If systemctl status docker says inactive or failed, start the service.
  • If docker context show points to desktop-linux or an old Desktop socket while you are using Linux Engine, switch context.
  • If /var/run/docker.sock exists but your user is not in the docker group, fix permissions carefully.
  • If DOCKER_HOST is set to a stale TCP or socket path, unset it for the current shell and retry.

Fix 1: start or restart Docker on Linux

On most systemd-based Linux distributions, the Docker daemon runs as the docker service. Start it, enable it for future boots, then check status.

sudo systemctl start docker
sudo systemctl enable docker
sudo systemctl status docker

Then test the connection again.

docker info
docker run hello-world

If the service fails to start, inspect the journal instead of retrying random commands.

sudo journalctl -u docker --no-pager -n 80

Fix 2: fix socket permissions on Linux

If Docker works with sudo but fails without it, the daemon is usually running and your user cannot access the Docker socket.

sudo docker ps
docker ps

If the first command works and the second fails, add your user to the docker group, then start a new login session.

sudo usermod -aG docker $USER
newgrp docker
id -nG
docker psCode language: PHP (php)

Security note: Membership in the docker group is effectively root-equivalent on many systems. On a shared server, do not add users casually. Use sudo docker ..., rootless Docker, or a tighter access policy when needed.

Fix 3: switch the Docker context

A common modern cause is a wrong Docker context. This happens after Docker Desktop updates, after switching between Docker Desktop and Docker Engine, or when a stale socket path remains in your config.

docker context ls
docker context show

If you use Docker Engine on Linux, the default context is often correct.

docker context use defaultCode language: JavaScript (javascript)

If you use Docker Desktop, especially on macOS or Windows, you may need the Desktop context.

docker context use desktop-linuxCode language: PHP (php)

Retry with docker info. If the error mentions a socket under ~/.docker/desktop/, that is a strong sign that context or Desktop integration is the real issue.

Fix 4: unset a stale DOCKER_HOST

DOCKER_HOST overrides where the Docker CLI sends requests. It is useful in CI or remote daemon setups, but it also causes confusing local failures when left in a shell profile.

echo $DOCKER_HOST
unset DOCKER_HOST
docker infoCode language: PHP (php)

If that fixes the error, remove or narrow the export from ~/.bashrc, ~/.zshrc, your CI variables, or your shell startup scripts.

Fix 5: Docker Desktop on macOS

On macOS, you normally do not start dockerd with systemctl. Docker Desktop owns the Linux VM that runs the daemon.

  • Open Docker Desktop and wait until it says the engine is running.
  • Run docker context ls and check whether desktop-linux exists.
  • Switch to the Desktop context if needed: docker context use desktop-linux.
  • Restart Docker Desktop if the socket path exists but the server section in docker info still fails.

If the original problem appears after a Desktop update, context reset is often faster than a full reinstall.

Fix 6: Docker Desktop with WSL2 on Windows

In WSL2, the Linux shell talks to Docker Desktop on Windows. If integration breaks, Docker may be running in Windows but unavailable inside your Ubuntu distro.

  • Open Docker Desktop.
  • Go to Settings → Resources → WSL Integration.
  • Enable integration for the distro you use.
  • Restart WSL, reopen the distro, and retry docker info.
wsl --shutdown

After reopening your distro, run:

docker context ls
docker info
docker run hello-world

Fix 7: CI errors at tcp://docker:2375

If the error mentions tcp://docker:2375 or tcp://localhost:2375, you are usually dealing with CI configuration, not your local Docker daemon.

In GitLab CI, Jenkins agents, and similar systems, the Docker CLI container needs access to a daemon through Docker-in-Docker, a mounted socket, or a remote builder. If the service is missing or the host is wrong, builds fail with the same daemon error.

Scope note: Keep CI fixes separate from local-machine fixes. A working local docker info does not prove that your GitLab runner, Jenkins agent, or containerized build job has access to a daemon.

Common mistakes

  • Checking docker --version and assuming Docker is healthy. That only proves the client is installed.
  • Adding your user to the docker group but not starting a new login shell.
  • Mixing Docker Desktop and Docker Engine, then using the wrong context.
  • Leaving DOCKER_HOST set globally after a CI or remote daemon experiment.
  • Trying random reinstalls before checking service status, context, and socket permissions.
  • Confusing daemon connectivity errors with registry errors. If the daemon works but an image pull fails, that is a different problem. For example, see the Docker manifest unknown guide.

What to run after the fix

Once the daemon responds, run a small command before going back to a real project.

docker info
docker run --rm hello-world

If your project uses Compose, validate the stack with a safe start command from the Docker Compose Up guide. If you need to stop or reset a stack during troubleshooting, use the safer patterns in the Docker Compose Down guide.

If the daemon is fixed but your container immediately exits, that is not this daemon error anymore. Check the command or entrypoint using the Dockerfile ENTRYPOINT guide.

FAQ

These are the common follow-up questions after the daemon connection itself is fixed.

Why does Docker say it cannot connect to the daemon if Docker is installed?

Because the Docker CLI and the Docker daemon are separate parts. The CLI can be installed and still fail if the daemon is stopped, unreachable, or blocked by permissions.

How do I check whether the Docker daemon is running?

Run docker info and inspect the Server section. On Linux, also run systemctl status docker. On Docker Desktop, check that Desktop says the engine is running and that your Docker context points to the right target.

Should I use sudo docker or add my user to the docker group?

For a personal dev machine, adding your user to the docker group is common. On shared or production machines, be careful because the docker group can effectively grant root-level control.

Why does Docker Desktop run but the CLI still fails?

The CLI may be using the wrong context, a stale socket path, or a WSL integration that is disabled or broken. Check docker context ls, switch to desktop-linux when appropriate, and restart Docker Desktop or WSL.

How do I fix this error in WSL2?

Enable WSL integration in Docker Desktop for your distro, run wsl –shutdown from PowerShell, reopen the distro, then retry docker info and docker run hello-world.

Nathan Cole Avatar

Leave a Reply

Your email address will not be published. Required fields are marked *