首页 > 解决方案 > Docker SDK for Python 未在我的 dockerized Django 应用程序中运行

问题描述

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

我的 django 应用程序是 dockerized,它是这个存储库的克隆。

这是 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 文件:

version: '2'
services:
  web:
    build: .
    image: djangobase
    command: python manage.py runserver 0.0.0.0:8000
    ports:
      - "3000:3000"
      - "8000:8000"
    volumes:
      - .:/code
    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

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

运行命令后docker-compose builddocker-compose up我的 django 项目运行良好。

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

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 中添加完整的响应:

激活 python 外壳: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'))

太感谢了。

标签: pythondjangodockerdocker-compose

解决方案


我找到了一个解决方案,很多时间之后。只需要包括

- /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

推荐阅读