我正在Django项目中工作,我想使用Docker SDK for Python创建一些服务。
我的django应用已被docker化,它是此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文件:
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 build
和docker-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
文件引起的,但是我不确定,并且我不知道该如何配置我的容器以正确的方式来识别此文件。
如果有帮助,我将在外壳中添加完整的响应:
激活python shell: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'))
非常感谢您。
答案 0 :(得分:3)
很久以后,我找到了解决方案。只需添加
- /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