Docker Community Forums

Share and learn in the Docker community.

Docker SDK for python

I have been using the docker sdk for python sometime now and its pretty fun.
i have a script that checks whether a container is up or down and sends an email notification to myself based on the container’s state.
Im using python schedule library to run the application the script every 1 minute

Now, i want to dockerise that application.

which i have done successfully but it fails to run
Error below…

Traceback (most recent call last):
monitor    |   File "/usr/local/lib/python3.7/site-packages/urllib3/connectionpool.py", line 677, in urlopen
monitor    |     chunked=chunked,
monitor    |   File "/usr/local/lib/python3.7/site-packages/urllib3/connectionpool.py", line 392, in _make_request
monitor    |     conn.request(method, url, **httplib_request_kw)
monitor    |   File "/usr/local/lib/python3.7/http/client.py", line 1229, in request
monitor    |     self._send_request(method, url, body, headers, encode_chunked)
monitor    |   File "/usr/local/lib/python3.7/http/client.py", line 1275, in _send_request
monitor    |     self.endheaders(body, encode_chunked=encode_chunked)
monitor    |   File "/usr/local/lib/python3.7/http/client.py", line 1224, in endheaders
monitor    |     self._send_output(message_body, encode_chunked=encode_chunked)
monitor    |   File "/usr/local/lib/python3.7/http/client.py", line 1016, in _send_output
monitor    |     self.send(msg)
monitor    |   File "/usr/local/lib/python3.7/http/client.py", line 956, in send
monitor    |     self.connect()
monitor    |   File "/usr/local/lib/python3.7/site-packages/docker/transport/unixconn.py", line 43, in connect
monitor    |     sock.connect(self.unix_socket)
monitor    | FileNotFoundError: [Errno 2] No such file or directory
monitor    |
monitor    | During handling of the above exception, another exception occurred:
monitor    |
monitor    | Traceback (most recent call last):
monitor    |   File "/usr/local/lib/python3.7/site-packages/requests/adapters.py", line 449, in send
monitor    |     timeout=timeout
monitor    |   File "/usr/local/lib/python3.7/site-packages/urllib3/connectionpool.py", line 725, in urlopen
monitor    |     method, url, error=e, _pool=self, _stacktrace=sys.exc_info()[2]
monitor    |   File "/usr/local/lib/python3.7/site-packages/urllib3/util/retry.py", line 403, in increment
monitor    |     raise six.reraise(type(error), error, _stacktrace)
monitor    |   File "/usr/local/lib/python3.7/site-packages/urllib3/packages/six.py", line 734, in reraise
monitor    |     raise value.with_traceback(tb)
monitor    |   File "/usr/local/lib/python3.7/site-packages/urllib3/connectionpool.py", line 677, in urlopen
monitor    |     chunked=chunked,
monitor    |   File "/usr/local/lib/python3.7/site-packages/urllib3/connectionpool.py", line 392, in _make_request
monitor    |     conn.request(method, url, **httplib_request_kw)
monitor    |   File "/usr/local/lib/python3.7/http/client.py", line 1229, in request
monitor    |     self._send_request(method, url, body, headers, encode_chunked)
monitor    |   File "/usr/local/lib/python3.7/http/client.py", line 1275, in _send_request
monitor    |     self.endheaders(body, encode_chunked=encode_chunked)
monitor    |   File "/usr/local/lib/python3.7/http/client.py", line 1224, in endheaders
monitor    |     self._send_output(message_body, encode_chunked=encode_chunked)
monitor    |   File "/usr/local/lib/python3.7/http/client.py", line 1016, in _send_output
monitor    |     self.send(msg)
monitor    |   File "/usr/local/lib/python3.7/http/client.py", line 956, in send
monitor    |     self.connect()
monitor    |   File "/usr/local/lib/python3.7/site-packages/docker/transport/unixconn.py", line 43, in connect
monitor    |     sock.connect(self.unix_socket)
monitor    | urllib3.exceptions.ProtocolError: ('Connection aborted.', FileNotFoundError(2, 'No such file or directory'))
monitor    |
monitor    | During handling of the above exception, another exception occurred:
monitor    |
monitor    | Traceback (most recent call last):
monitor    |   File "src/run.py", line 1, in <module>
monitor    |     from analisa_monitor import monitor
monitor    |   File "/app/src/analisa_monitor/monitor.py", line 41, in <module>
monitor    |     schedule.run_pending()
monitor    |   File "/usr/local/lib/python3.7/site-packages/schedule/__init__.py", line 563, in run_pending
monitor    |     default_scheduler.run_pending()
monitor    |   File "/usr/local/lib/python3.7/site-packages/schedule/__init__.py", line 94, in run_pending
monitor    |     self._run_job(job)
monitor    |   File "/usr/local/lib/python3.7/site-packages/schedule/__init__.py", line 147, in _run_job
monitor    |     ret = job.run()
monitor    |   File "/usr/local/lib/python3.7/site-packages/schedule/__init__.py", line 466, in run
monitor    |     ret = self.job_func()
monitor    |   File "/app/src/analisa_monitor/monitor.py", line 10, in main
monitor    |     analisa = container.get_container('analisa')
monitor    |   File "/app/src/analisa_monitor/utils/container.py", line 6, in get_container
monitor    |     return client.containers.get(id_or_name)
monitor    |   File "/usr/local/lib/python3.7/site-packages/docker/models/containers.py", line 880, in get
monitor    |     resp = self.client.api.inspect_container(container_id)
monitor    |   File "/usr/local/lib/python3.7/site-packages/docker/utils/decorators.py", line 19, in wrapped
monitor    |     return f(self, resource_id, *args, **kwargs)
monitor    |   File "/usr/local/lib/python3.7/site-packages/docker/api/container.py", line 758, in inspect_container
monitor    |     self._get(self._url("/containers/{0}/json", container)), True
monitor    |   File "/usr/local/lib/python3.7/site-packages/docker/utils/decorators.py", line 46, in inner
monitor    |     return f(self, *args, **kwargs)
monitor    |   File "/usr/local/lib/python3.7/site-packages/docker/api/client.py", line 230, in _get
monitor    |     return self.get(url, **self._set_request_timeout(kwargs))
monitor    |   File "/usr/local/lib/python3.7/site-packages/requests/sessions.py", line 543, in get
monitor    |     return self.request('GET', url, **kwargs)
monitor    |   File "/usr/local/lib/python3.7/site-packages/requests/sessions.py", line 530, in request
monitor    |     resp = self.send(prep, **send_kwargs)
monitor    |   File "/usr/local/lib/python3.7/site-packages/requests/sessions.py", line 643, in send
monitor    |     r = adapter.send(request, **kwargs)
monitor    |   File "/usr/local/lib/python3.7/site-packages/requests/adapters.py", line 498, in send
monitor    |     raise ConnectionError(err, request=request)
monitor    | requests.exceptions.ConnectionError: ('Connection aborted.', FileNotFoundError(2, 'No such file or directory'))
monitor exited with code 1

guys please any help???

taken in consideration that i’m new but working with python, i’m now wondering the same thing. if you find an answer please update. anyone else who can help - that would be appreciated.

yeah so this the answer

run the container with -v /var/run/docker.sock:/var/run/docker.sock

eg: docker run -v /var/run/docker.sock:/var/run/docker.sock your_image_name

Parameters:
base_url (str) – URL to the Docker server. For example, unix:///var/run/docker.sock or tcp://127.0.0.1:1234.
version (str) – The version of the API to use. Set to auto to automatically detect the server’s version. Default: 1.35
timeout (int) – Default timeout for API calls, in seconds.
tls (bool or TLSConfig) – Enable TLS. Pass True to enable it with default options, or pass a TLSConfig object to use custom configuration.
user_agent (str) – Set a custom user agent for requests to the server.
credstore_env (dict) – Override environment variables when calling the credential store process.

@lewish95 care to throw more light on your reply

where should these parameters be set

@redneckcarnage you can checkout my repo for more clarification