Skip to content
Latchkey

Molecule "Cannot connect to the Docker daemon" (docker-in-docker) in CI

When the Molecule step runs inside a container: job, that container has no Docker daemon of its own. The docker driver then cannot reach /var/run/docker.sock to create platform instances.

What this error means

Molecule create fails with "Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?" only when the job runs in a container.

Molecule
Cannot connect to the Docker daemon at unix:///var/run/docker.sock.
Is the docker daemon running?
CRITICAL Ansible return code was 2, command was: ... create ...

Common causes

The job container has no Docker daemon

A container: job isolates the step from the host; there is no dockerd inside it and no socket mounted, so the driver cannot create instances.

The host socket was not shared into the container

Without mounting the host /var/run/docker.sock, the containerized job cannot use the runner host's daemon.

How to fix it

Run Molecule directly on the runner, not in a container

The simplest fix on GitHub-hosted runners is to run the Molecule step on the host, where Docker is already available.

.github/workflows/ci.yml
jobs:
  molecule:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - run: pip install "molecule-plugins[docker]" ansible-core docker
      - run: molecule test

Mount the host socket if a container job is required

If the job must run in a container, share the host Docker socket so the driver can reach the daemon.

.github/workflows/ci.yml
jobs:
  molecule:
    runs-on: ubuntu-latest
    container:
      image: python:3.12
      volumes:
        - /var/run/docker.sock:/var/run/docker.sock

How to prevent it

  • Prefer running Molecule on the runner host where Docker exists.
  • Mount /var/run/docker.sock into container jobs that need Docker.
  • Confirm docker info from inside the job before running Molecule.

Frequently asked questions

What causes ""Cannot connect to the Docker daemon""?
A container: job isolates the step from the host; there is no dockerd inside it and no socket mounted, so the driver cannot create instances.
How do I fix "Cannot connect to the Docker daemon"?
The simplest fix on GitHub-hosted runners is to run the Molecule step on the host, where Docker is already available.

Related guides

References

Latchkey auto-heals failures like this one - detected, fixed, and retried without you. Start free → 30-day trial · No credit card