<h3>Context</h3>
<p>I am running Airflow, and trying to run a proof of concept for a Docker container using Airflow's <a href="https://airflow.apache.org/docs/apache-airflow/1.10.4/_api/airflow/operators/docker_operator/index.html" rel="nofollow noreferrer">DockerOperator</a>. I am deploying to Kubernetes (EKS), but not using Kubernetes Executor yet. Given that I am running pods, by using the <code>DockerOperator</code> I will be running (to my understanding) Docker in Docker.</p>
<p>Whenever I run my task, I am receiving the Error: <code>ERROR - Error while fetching server API version</code>. The error happens both on <code>docker-compose</code> as well as EKS (kubernetes).</p>
<h3>My current status</h3>
<p>Thi is how my airflow Dockerfile looks like:</p>
<pre class="lang-sh prettyprint-override"><code>FROM apache/airflow:1.10.14-python3.8
# Use airflow user for pip installs and other things.
USER root
# Copying Airflow requirements
USER airflow
COPY requirements.txt /tmp/requirements.txt
# Installing requirements. Using airflow user (docs: https://airflow.apache.org/docs/apache-airflow/stable/production-deployment.html)
RUN pip install --no-cache-dir -r /tmp/requirements.txt
</code></pre>
<p>This is how the dag I am trying to run looks like:</p>
<pre class="lang-py prettyprint-override"><code>with DAG(
        dag_id='task',
        default_args=dict(
            start_date=days_ago(0)
        ),
        schedule_interval='@daily'
) as dag:

    task_1 = DockerOperator(
        dag=dag,
        task_id='docker_task',
        image='centos:latest',
        api_version=&quot;auto&quot;,
        docker_url='unix://var/run/docker.sock',
        command='/bin/sleep 30'
    )
</code></pre>
<p>This is the stack trace of the error I am getting:</p>
<pre><code>[2020-12-29 14:18:52,601] {taskinstance.py:1150} ERROR - Error while fetching server API version: ('Connection aborted.', FileNotFoundError(2, 'No such file or directory'))
Traceback (most recent call last):
  File &quot;/home/airflow/.local/lib/python3.8/site-packages/urllib3/connectionpool.py&quot;, line 670, in urlopen
    httplib_response = self._make_request(
  File &quot;/home/airflow/.local/lib/python3.8/site-packages/urllib3/connectionpool.py&quot;, line 392, in _make_request
    conn.request(method, url, **httplib_request_kw)
  File &quot;/usr/local/lib/python3.8/http/client.py&quot;, line 1255, in request
    self._send_request(method, url, body, headers, encode_chunked)
  File &quot;/usr/local/lib/python3.8/http/client.py&quot;, line 1301, in _send_request
    self.endheaders(body, encode_chunked=encode_chunked)
  File &quot;/usr/local/lib/python3.8/http/client.py&quot;, line 1250, in endheaders
    self._send_output(message_body, encode_chunked=encode_chunked)
  File &quot;/usr/local/lib/python3.8/http/client.py&quot;, line 1010, in _send_output
    self.send(msg)
  File &quot;/usr/local/lib/python3.8/http/client.py&quot;, line 950, in send
    self.connect()
  File &quot;/home/airflow/.local/lib/python3.8/site-packages/docker/transport/unixconn.py&quot;, line 43, in connect
    sock.connect(self.unix_socket)
FileNotFoundError: [Errno 2] No such file or directory

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File &quot;/home/airflow/.local/lib/python3.8/site-packages/requests/adapters.py&quot;, line 439, in send
    resp = conn.urlopen(
  File &quot;/home/airflow/.local/lib/python3.8/site-packages/urllib3/connectionpool.py&quot;, line 726, in urlopen
    retries = retries.increment(
  File &quot;/home/airflow/.local/lib/python3.8/site-packages/urllib3/util/retry.py&quot;, line 410, in increment
    raise six.reraise(type(error), error, _stacktrace)
  File &quot;/home/airflow/.local/lib/python3.8/site-packages/urllib3/packages/six.py&quot;, line 734, in reraise
    raise value.with_traceback(tb)
  File &quot;/home/airflow/.local/lib/python3.8/site-packages/urllib3/connectionpool.py&quot;, line 670, in urlopen
    httplib_response = self._make_request(
  File &quot;/home/airflow/.local/lib/python3.8/site-packages/urllib3/connectionpool.py&quot;, line 392, in _make_request
    conn.request(method, url, **httplib_request_kw)
  File &quot;/usr/local/lib/python3.8/http/client.py&quot;, line 1255, in request
    self._send_request(method, url, body, headers, encode_chunked)
  File &quot;/usr/local/lib/python3.8/http/client.py&quot;, line 1301, in _send_request
    self.endheaders(body, encode_chunked=encode_chunked)
  File &quot;/usr/local/lib/python3.8/http/client.py&quot;, line 1250, in endheaders
    self._send_output(message_body, encode_chunked=encode_chunked)
  File &quot;/usr/local/lib/python3.8/http/client.py&quot;, line 1010, in _send_output
    self.send(msg)
  File &quot;/usr/local/lib/python3.8/http/client.py&quot;, line 950, in send
    self.connect()
  File &quot;/home/airflow/.local/lib/python3.8/site-packages/docker/transport/unixconn.py&quot;, line 43, in connect
    sock.connect(self.unix_socket)
urllib3.exceptions.ProtocolError: ('Connection aborted.', FileNotFoundError(2, 'No such file or directory'))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File &quot;/home/airflow/.local/lib/python3.8/site-packages/docker/api/client.py&quot;, line 214, in _retrieve_server_version
    return self.version(api_version=False)[&quot;ApiVersion&quot;]
  File &quot;/home/airflow/.local/lib/python3.8/site-packages/docker/api/daemon.py&quot;, line 181, in version
    return self._result(self._get(url), json=True)
  File &quot;/home/airflow/.local/lib/python3.8/site-packages/docker/utils/decorators.py&quot;, line 46, in inner
    return f(self, *args, **kwargs)
  File &quot;/home/airflow/.local/lib/python3.8/site-packages/docker/api/client.py&quot;, line 237, in _get
    return self.get(url, **self._set_request_timeout(kwargs))
  File &quot;/home/airflow/.local/lib/python3.8/site-packages/requests/sessions.py&quot;, line 543, in get
    return self.request('GET', url, **kwargs)
  File &quot;/home/airflow/.local/lib/python3.8/site-packages/requests/sessions.py&quot;, line 530, in request
    resp = self.send(prep, **send_kwargs)
  File &quot;/home/airflow/.local/lib/python3.8/site-packages/requests/sessions.py&quot;, line 643, in send
    r = adapter.send(request, **kwargs)
  File &quot;/home/airflow/.local/lib/python3.8/site-packages/requests/adapters.py&quot;, line 498, in send
    raise ConnectionError(err, request=request)
requests.exceptions.ConnectionError: ('Connection aborted.', FileNotFoundError(2, 'No such file or directory'))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File &quot;/home/airflow/.local/lib/python3.8/site-packages/airflow/models/taskinstance.py&quot;, line 984, in _run_raw_task
    result = task_copy.execute(context=context)
  File &quot;/home/airflow/.local/lib/python3.8/site-packages/airflow/operators/docker_operator.py&quot;, line 260, in execute
    self.cli = APIClient(
  File &quot;/home/airflow/.local/lib/python3.8/site-packages/docker/api/client.py&quot;, line 197, in __init__
    self._version = self._retrieve_server_version()
  File &quot;/home/airflow/.local/lib/python3.8/site-packages/docker/api/client.py&quot;, line 221, in _retrieve_server_version
    raise DockerException(
docker.errors.DockerException: Error while fetching server API version: ('Connection aborted.', FileNotFoundError(2, 'No such file or directory'))
</code></pre>
<h3>What I have tried</h3>
<ol>
<li>Mount the socket into docker compose <code>/var/run/docker.sock:/var/run/docker.sock:ro</code></li>
</ol>
<p>First, that gives me a new error to:</p>
<pre><code>ERROR - Error while fetching server API version: ('Connection aborted.', PermissionError(13, 'Permission denied'))
</code></pre>
<p>Second, how will I be able to mount on Kubernetes? I would guess that complicates things</p>
<ol start="2">
<li>Install docker within my container and try to give privileges to the airflow user like:</li>
</ol>
<pre><code>FROM apache/airflow:1.10.14-python3.8
# Use airflow user for pip installs and other things.
USER root
# Docker
RUN curl -sSL https://get.docker.com/ | sh  
# Usermod
RUN usermod -aG docker airflow
# Copying Airflow requirements
USER airflow
COPY requirements.txt /tmp/requirements.txt
# Installing requirements. Using airflow user (docs: https://airflow.apache.org/docs/apache-airflow/stable/production-deployment.html)
RUN pip install --no-cache-dir -r /tmp/requirements.txt
</code></pre>
<p>But that also did not work.</p>
<ol start="3">
<li>Mounting the socket into the DockerOperator task like:</li>
</ol>
<pre class="lang-py prettyprint-override"><code>    task_1 = DockerOperator(
        dag=dag,
        task_id='docker_task',
        image='centos:latest',
        api_version=&quot;auto&quot;,
        docker_url='unix://var/run/docker.sock',
        command='/bin/sleep 30',
        volumes=['/var/run/docker.sock:/var/run/docker.sock:ro'],
    )
</code></pre>
<p>But that also has had no effect</p>

<p>Copying down from comment:</p>
<p>The direct issue is likely that the docker control socket file is owned by something like <code>root:docker</code> on the host, and assuming Airflow isn't running as root (which it shouldn't) then you would need to specifically set it to run with the same gid as the <code>docker</code> group on the host machine (which probably has no matching group inside the container, which could confuse other things).</p>
<p>The real answer is to use the KubernetesPodOperator instead. DockerOperator won't work at all once you stop using Docker on the underlying host which will be happening soon now that dockershim is deprecated.</p>
