Docker SDK for Python没有在我的DockerDjango应用程序中运行

2024-04-25 11:41:06 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在Django项目中工作,我想使用Docker SDK for Python来创建一些服务。在

我的django应用程序是dockerized的,它是这个repository的克隆。在

这是Dockerfile:

FROM python:3.6.0

RUN wget http://rubies.travis-ci.org/ubuntu/14.04/x86_64/ruby-2.3.1.tar.bz2 \
    && tar xvjf ruby-2.3.1.tar.bz2 \
    && cp -rp ruby-2.3.1/* /usr/local/ \
    && rm -rf ruby-2.3.1.tar.bz2 ruby-2.3.1/

RUN mkdir /code
WORKDIR /code

RUN easy_install -U pip
RUN pip install -U pip setuptools

ADD requirements.txt /code/requirements.txt
RUN pip install -r requirements.txt

这是docker compose文件:

^{pr2}$

我已经将docker==3.5.0添加到需求文件中。在

在运行docker-compose builddocker-compose up命令之后,django项目运行得非常好。在

在安装docker==3.5.0库之后,当我尝试按照here描述的步骤操作时,我的问题就开始了。在

import docker
client = docker.from_env()
client.containers.run("ubuntu", "echo hello world")

当我尝试运行第三行时,遇到以下错误:

raise ConnectionError(err, request=request)
requests.exceptions.ConnectionError: ('Connection aborted.', FileNotFoundError(2, 'No such file or directory'))

我认为这是因为,在活动容器中运行该命令时,它找不到var/run/docker.sock文件,但我对此不确定,而且我不知道如何以正确的方式配置容器来识别该文件。在

我将在shell plus中添加完整的响应,如果它有帮助:

激活pythonshell:docker-compose exec app python manage.py shell_plus

>>> import docker
>>> client = docker.from_env()
>>> client.containers.run("ubuntu", "echo hello world")
Traceback (most recent call last):
  File "/usr/local/lib/python3.6/site-packages/urllib3/connectionpool.py", line 600, in urlopen
    chunked=chunked)
  File "/usr/local/lib/python3.6/site-packages/urllib3/connectionpool.py", line 354, in _make_request
    conn.request(method, url, **httplib_request_kw)
  File "/usr/local/lib/python3.6/http/client.py", line 1239, in request
    self._send_request(method, url, body, headers, encode_chunked)
  File "/usr/local/lib/python3.6/http/client.py", line 1285, in _send_request
    self.endheaders(body, encode_chunked=encode_chunked)
  File "/usr/local/lib/python3.6/http/client.py", line 1234, in endheaders
    self._send_output(message_body, encode_chunked=encode_chunked)
  File "/usr/local/lib/python3.6/http/client.py", line 1026, in _send_output
    self.send(msg)
  File "/usr/local/lib/python3.6/http/client.py", line 964, in send
    self.connect()
  File "/usr/local/lib/python3.6/site-packages/docker/transport/unixconn.py", line 42, 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 "/usr/local/lib/python3.6/site-packages/requests/adapters.py", line 445, in send
    timeout=timeout
  File "/usr/local/lib/python3.6/site-packages/urllib3/connectionpool.py", line 638, in urlopen
    _stacktrace=sys.exc_info()[2])
  File "/usr/local/lib/python3.6/site-packages/urllib3/util/retry.py", line 367, in increment
    raise six.reraise(type(error), error, _stacktrace)
  File "/usr/local/lib/python3.6/site-packages/urllib3/packages/six.py", line 685, in reraise
    raise value.with_traceback(tb)
  File "/usr/local/lib/python3.6/site-packages/urllib3/connectionpool.py", line 600, in urlopen
    chunked=chunked)
  File "/usr/local/lib/python3.6/site-packages/urllib3/connectionpool.py", line 354, in _make_request
    conn.request(method, url, **httplib_request_kw)
  File "/usr/local/lib/python3.6/http/client.py", line 1239, in request
    self._send_request(method, url, body, headers, encode_chunked)
  File "/usr/local/lib/python3.6/http/client.py", line 1285, in _send_request
    self.endheaders(body, encode_chunked=encode_chunked)
  File "/usr/local/lib/python3.6/http/client.py", line 1234, in endheaders
    self._send_output(message_body, encode_chunked=encode_chunked)
  File "/usr/local/lib/python3.6/http/client.py", line 1026, in _send_output
    self.send(msg)
  File "/usr/local/lib/python3.6/http/client.py", line 964, in send
    self.connect()
  File "/usr/local/lib/python3.6/site-packages/docker/transport/unixconn.py", line 42, 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 "<console>", line 1, in <module>
  File "/usr/local/lib/python3.6/site-packages/docker/models/containers.py", line 766, in run
    detach=detach, **kwargs)
  File "/usr/local/lib/python3.6/site-packages/docker/models/containers.py", line 824, in create
    resp = self.client.api.create_container(**create_kwargs)
  File "/usr/local/lib/python3.6/site-packages/docker/api/container.py", line 411, in create_container
    return self.create_container_from_config(config, name)
  File "/usr/local/lib/python3.6/site-packages/docker/api/container.py", line 421, in create_container_from_config
    res = self._post_json(u, data=config, params=params)
  File "/usr/local/lib/python3.6/site-packages/docker/api/client.py", line 257, in _post_json
    return self._post(url, data=json.dumps(data2), **kwargs)
  File "/usr/local/lib/python3.6/site-packages/docker/utils/decorators.py", line 46, in inner
    return f(self, *args, **kwargs)
  File "/usr/local/lib/python3.6/site-packages/docker/api/client.py", line 194, in _post
    return self.post(url, **self._set_request_timeout(kwargs))
  File "/usr/local/lib/python3.6/site-packages/requests/sessions.py", line 559, in post
    return self.request('POST', url, data=data, json=json, **kwargs)
  File "/usr/local/lib/python3.6/site-packages/requests/sessions.py", line 512, in request
    resp = self.send(prep, **send_kwargs)
  File "/usr/local/lib/python3.6/site-packages/requests/sessions.py", line 622, in send
    r = adapter.send(request, **kwargs)
  File "/usr/local/lib/python3.6/site-packages/requests/adapters.py", line 495, in send
    raise ConnectionError(err, request=request)
requests.exceptions.ConnectionError: ('Connection aborted.', FileNotFoundError(2, 'No such file or directory'))

非常感谢你。在


Tags: dockerinpyselfclientsendrequestlib
1条回答
网友
1楼 · 发布于 2024-04-25 11:41:06

我找到了一个解决办法,很久以后。只是有必要包括

- /var/run/docker.sock:/var/run/docker.sock

在web服务的卷列表中。在

version: '2' 
services:
  web:
    build: .
    image: djangobase
    command: python manage.py runserver 0.0.0.0:8000
    ports:
      - "3000:3000"
      - "8000:8000"
    volumes:
      - .:/code
      - /var/run/docker.sock:/var/run/docker.sock
    depends_on:
      - migration
      - db
  db:
    image: postgres:10.1
    volumes:
      - .:/tmp/data/
  migration:
    image: djangobase
    command: python manage.py migrate  noinput
    volumes:
      - .:/code
    depends_on:
      - db

相关问题 更多 >