{
  "slug": "How-to-Fix--Cannot-Connect-to-Docker-Daemon--Error-on-Ubuntu-Linux-a2f25a79d786",
  "title": "How to Fix “Cannot Connect to Docker Daemon” Error on Ubuntu Linux",
  "subtitle": "If you’re seeing this error when running docker ps:",
  "excerpt": "If you’re seeing this error when running docker ps:",
  "date": "2025-09-26",
  "tags": [
    "Docker",
    "Linux"
  ],
  "readingTime": "2 min",
  "url": "https://medium.com/@mobinshaterian/how-to-fix-cannot-connect-to-docker-daemon-error-on-ubuntu-linux-a2f25a79d786",
  "hero": "https://cdn-images-1.medium.com/max/800/1*kPAm4QA836msrxtBotZzwQ.png",
  "content": [
    {
      "type": "heading",
      "level": 2,
      "text": "How to Fix “Cannot Connect to Docker Daemon” Error on Ubuntu Linux"
    },
    {
      "type": "paragraph",
      "html": "If you’re seeing this error when running <code>docker ps</code>:"
    },
    {
      "type": "code",
      "lang": "bash",
      "code": "Cannot connect to the Docker daemon at unix:///home/youruser/.docker/desktop/docker.sock. Is the\ndocker daemon running?"
    },
    {
      "type": "paragraph",
      "html": "But <code>sudo docker ps</code> works fine, you’re not alone. This is a common issue on Linux systems where Docker Engine is installed alongside (or after) Docker Desktop."
    },
    {
      "type": "paragraph",
      "html": "In this guide, you’ll learn why this happens and how to resolve it using Docker’s built-in context management permanently."
    },
    {
      "type": "image",
      "src": "https://cdn-images-1.medium.com/max/800/1*kPAm4QA836msrxtBotZzwQ.png",
      "alt": "How to Fix “Cannot Connect to Docker Daemon” Error on Ubuntu Linux",
      "caption": "",
      "width": 1024,
      "height": 1024
    },
    {
      "type": "heading",
      "level": 2,
      "text": "🔍 Why This Happens"
    },
    {
      "type": "paragraph",
      "html": "Docker provides two main ways to run on Linux:"
    },
    {
      "type": "list",
      "ordered": true,
      "items": [
        "Docker Engine — The standard, lightweight, command-line–first version (recommended for Linux).",
        "Docker Desktop — A GUI-focused application originally designed for macOS and Windows, now available on Linux (but less common)."
      ]
    },
    {
      "type": "paragraph",
      "html": "When you install Docker Desktop, it automatically:"
    },
    {
      "type": "list",
      "ordered": false,
      "items": [
        "Creates a Docker CLI context named <code>desktop-linux</code>.",
        "Sets this context as active.",
        "Configures it to use a custom socket at:<br><code>unix:///home/youruser/.docker/desktop/docker.sock</code>"
      ]
    },
    {
      "type": "paragraph",
      "html": "Later, if you:"
    },
    {
      "type": "list",
      "ordered": false,
      "items": [
        "Uninstall Docker Desktop, or",
        "Switch to using Docker Engine (via <code>apt install docker-ce</code>),"
      ]
    },
    {
      "type": "paragraph",
      "html": "…the <code>desktop-linux</code> context remains active, even though the socket file no longer exists. That’s why Docker CLI fails to connect—it’s looking in the wrong place."
    },
    {
      "type": "paragraph",
      "html": "Meanwhile, <code>sudo docker ps</code> works because <code>sudo</code> bypasses your user’s Docker context and uses the system default (<code>/var/run/docker.sock</code>)."
    },
    {
      "type": "heading",
      "level": 2,
      "text": "✅ Step-by-Step Fix"
    },
    {
      "type": "heading",
      "level": 2,
      "text": "1. Check Your Current Docker Contexts"
    },
    {
      "type": "paragraph",
      "html": "Run:"
    },
    {
      "type": "code",
      "lang": "bash",
      "code": "docker context ls"
    },
    {
      "type": "paragraph",
      "html": "You’ll likely see something like:"
    },
    {
      "type": "code",
      "lang": "text",
      "code": "NAME              DESCRIPTION       DOCKER ENDPOINT                                  ERRORdefault\n...               unix:///var/run/docker.sockdesktop-linux *   Docker Desktop\nunix:///home/user/.docker/desktop/docker.sock"
    },
    {
      "type": "paragraph",
      "html": "The <code>*</code> indicates the active context—in this case, the broken <code>desktop-linux</code>."
    },
    {
      "type": "heading",
      "level": 2,
      "text": "2. Switch to the default Context"
    },
    {
      "type": "paragraph",
      "html": "The <code>default</code> context points to the correct socket used by Docker Engine:"
    },
    {
      "type": "code",
      "lang": "bash",
      "code": "docker context use default"
    },
    {
      "type": "paragraph",
      "html": "Output:"
    },
    {
      "type": "code",
      "lang": "text",
      "code": "default"
    },
    {
      "type": "paragraph",
      "html": "✅ This tells Docker CLI to use <code>/var/run/docker.sock</code>—the standard location."
    },
    {
      "type": "heading",
      "level": 2,
      "text": "3. Test the Fix"
    },
    {
      "type": "code",
      "lang": "bash",
      "code": "docker ps"
    },
    {
      "type": "paragraph",
      "html": "It should now work without <code>sudo</code>!"
    },
    {
      "type": "heading",
      "level": 2,
      "text": "Final Check"
    },
    {
      "type": "paragraph",
      "html": "After applying the fix:"
    },
    {
      "type": "code",
      "lang": "bash",
      "code": "docker context ls    # Should show 'default *'\ndocker ps            # Should work without sudo\ndocker run --rm hello-world  # Test end-to-end"
    },
    {
      "type": "heading",
      "level": 2,
      "text": "🏁 Conclusion"
    },
    {
      "type": "paragraph",
      "html": "The “Cannot connect to the Docker daemon” error on Linux is almost always a configuration mismatch, not a broken installation. As you’ve seen, it typically stems from Docker CLI pointing to a non-existent Docker Desktop socket while your actual Docker Engine runs perfectly in the background."
    },
    {
      "type": "paragraph",
      "html": "By switching to the <code>default</code> Docker context, you align your CLI with the standard Docker Engine socket (<code>/var/run/docker.sock</code>)—restoring seamless, <code>sudo</code>-free container management. This simple fix resolves the issue permanently without affecting your system services or requiring a Docker reinstall."
    },
    {
      "type": "paragraph",
      "html": "Remember: On Linux, Docker Engine is the native and recommended choice. Docker Desktop can be useful for specific workflows, but it introduces complexity that often leads to conflicts like this one. If you don’t actively need its GUI features, sticking with Docker Engine ensures a cleaner, more predictable experience."
    },
    {
      "type": "paragraph",
      "html": "Now that your Docker environment is back on track, you can focus on what really matters — building, running, and shipping your containers with confidence. Happy coding! 🐳✨"
    }
  ]
}
