<pre class="lang-py prettyprint-override"><code>def list_img():
    client = docker.from_env()
    k=1
    img_list = client.images.list()
    for img in img_list:
        print(k," ",img)
        k+=1
    return "Images are listed successfully "
</code></pre>

<p>I want to list all the docker images using the python3 docker sdk inside a runing docker container deployed in minikube.</p>

<pre class="lang-py prettyprint-override"><code>import docker 
def list_img():
    client = docker.from_env()
    k=1
    img_list = client.images.list()
    for img in img_list:
        print(k," ",img)
        k+=1
list_img()
</code></pre>

<p><strong>Dockerfile</strong></p>

<pre class="lang-py prettyprint-override"><code>FROM python:3.6-slim
RUN apt upgrade
RUN apt update
RUN pip3 install flask
# RUN pip3 install kubernetes
RUN pip3 install docker

WORKDIR  /dckr_sdk
COPY  . /dckr_sdk

EXPOSE  5022
CMD [ "python3","flask4_bld_dckr_img.py" ]
</code></pre>

<p><strong>And I am getting  the below error</strong></p>

<pre class="lang-sh prettyprint-override"><code>File "/usr/local/lib/python3.6/site-packages/docker/transport/unixconn.py", line 43, in connect
    sock.connect(self.unix_socket)
urllib3.exceptions.ProtocolError: ('Connection aborted.', FileNotFoundError(2, 'No such file or directory'))

File "/usr/local/lib/python3.6/site-packages/requests/adapters.py", line 498, in send
    raise ConnectionError(err, request=request)
requests.exceptions.ConnectionError: ('Connection aborted.', FileNotFoundError(2, 'No such file or directory'))
</code></pre>

<p>Using docker in a docker container can be done in 2 ways:</p>

<ol>
<li>Using the docker deamon of the host machine.</li>
<li>Really installing and using docker within another docker.</li>
</ol>

<p>Method 2 is generally not what you want to do as it has a lot of unwanted side effects. For method 1 you need to:</p>

<ol>
<li>Install the docker-cli in your image, add this to your <code>Dockerfile</code> or adapt it to the base image you are using:</li>
</ol>

<pre><code># Install docker
RUN apt-get update
RUN apt-get -y install apt-transport-https ca-certificates curl gnupg2 software-properties-common
RUN curl -fsSL https://download.docker.com/linux/debian/gpg | apt-key add -
RUN add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/debian $(lsb_release -cs) stable"
</code></pre>

<ol start="2">
<li>When running the docker container, you need to mount your host machine's docker deamon socket:</li>
</ol>

<pre><code>docker run -v /var/run/docker.sock:/var/run/docker.sock docker-image-name
</code></pre>

<p>This should do the trick and enable you to use the python docker sdk in your own container. More on docker-in-docker here: <a href="https://jpetazzo.github.io/2015/09/03/do-not-use-docker-in-docker-for-ci/" rel="noreferrer">https://jpetazzo.github.io/2015/09/03/do-not-use-docker-in-docker-for-ci/</a></p>
