简体   繁体   English

Docker build pytest:无法建立新连接:[Errno 111]连接被拒绝

[英]Docker build pytest :Failed to establish a new connection: [Errno 111] Connection refused

I am facing a serious issue while docker build. docker构建时,我面临一个严重的问题。 The pytest is not working but after build(remove the pytest part) the same pytest work from inside container pytest不起作用,但是在构建(删除pytest部分)之后,从容器内部进行相同的pytest工作

Here is my docker file: 这是我的docker文件:

    FROM phusion/baseimage:0.9.22

    RUN apt-get update && apt-get install -y build-essential git libaio1 libffi-dev libjpeg-dev libssl-dev libxml2-dev python-wand libmagickwand-dev\
        libxslt-dev libz-dev  python3-pip python3-setuptools unzip


    ADD requirements.pip /
    RUN pip3 install -r /requirements.pip

    VOLUME ["/app"]
    WORKDIR /app
    ADD . .


    EXPOSE 8000
    RUN python3  manage.py runserver 0.0.0.0:8000 &
    RUN python3  -m pytest tests/test_demo.py

Here is my test file: 这是我的测试文件:

    import requests

    def test_demo():
        response = requests.get('http://0.0.0.0:8000/' + "demo")
        assert (response.status_code == 200)

Please help. 请帮忙。

You can't start a background process in a Dockerfile, or anything that looks like it. 您无法在Dockerfile或类似的文件中启动后台进程。 In particular, the command 特别是命令

RUN python3 manage.py ... &

exits immediately, and when it does, the intermediate container with the background process is deleted. 立即退出,然后退出,具有后台进程的中间容器将被删除。

I wouldn't try to run this sample code from inside the Dockerfile. 我不会尝试从Dockerfile内部运行此示例代码。 I'd end with 我会以

CMD ["python3", "manage.py", "runserver", "0.0.0.0:8000"]

Build and launch the image 生成并启动映像

docker build -t myimage .
docker run -p 8000:8000 myimage

Change the script to take the server location as an environment variable (0.0.0.0 is never a valid IP address to which to connect) 更改脚本以将服务器位置作为环境变量(0.0.0.0永远不是要连接的有效IP地址)

import requests
import os
def test_demo():
   response = requests.get(os.environ['SERVER_URL'] + '/demo')
   assert(response.status_code == 200)

and then run (assuming you're running the client from the same host) 然后运行(假设您从同一主机运行客户端)

SERVER_URL=http://localhost:8000 pytest

Here is a minimal working example of tests ran with pytest-django in container build stage. 这是在容器构建阶段使用pytest-django运行的测试的最小工作示例。

the test script 测试脚本

# test_server.py
import requests


def test_about_page(live_server):
    response = requests.get(live_server.url + '/about')
    response.raise_for_status()

Notice I used the live_server fixture that starts a separate server instance in another thread. 注意,我使用live_server固定装置在另一个线程中启动一个单独的服务器实例。 I can then access the address via live_server.url in the test. 然后,我可以在测试中通过live_server.url访问该地址。

Similarly, the changes needed to be done in your test would then be: 同样,在测试中需要进行的更改将是:

import requests

def test_demo(live_server):
    response = requests.get(live_server.url + "/demo")
    assert (response.status_code == 200)

the sample Dockerfile 样本Dockerfile

FROM ubuntu:latest

RUN apt update && apt install git python3-pip -y \
    && pip3 install pytest-django django requests
RUN git clone https://github.com/Microsoft/project-python-django-webapp
ADD test_server.py project-python-django-webapp
WORKDIR project-python-django-webapp

RUN pytest -v --ds=python_webapp_django.settings

No magic happens here - I used an example Django project found on Github to run the test for. 这里没有发生任何魔术-我使用在Github找到的Django项目示例来运行测试。

building the container 建造容器

The container build starts the django server and executes the test, completing on test success: 容器构建将启动django服务器并执行测试,并在测试成功时完成:

$ docker build -t so/example .
Sending build context to Docker daemon  3.072kB
Step 1/6 : FROM ubuntu:latest
 ---> 113a43faa138
Step 2/6 : RUN apt update && apt install git python3-pip -y && pip3 install pytest-django django requests
 ---> Using cache
 ---> fd7adbe53cc8
Step 3/6 : RUN git clone https://github.com/Microsoft/project-python-django-webapp
 ---> Using cache
 ---> df514c1343c9
Step 4/6 : ADD test_server.py project-python-django-webapp
 ---> 118f74e43370
Step 5/6 : WORKDIR project-python-django-webapp
Removing intermediate container f81db838a81d
 ---> c3d35262f37c
Step 6/6 : RUN pytest -v --ds=python_webapp_django.settings
 ---> Running in e6292f2ac3e8
============================= test session starts ==============================
platform linux -- Python 3.6.5, pytest-3.6.3, py-1.5.4, pluggy-0.6.0 -- /usr/bin/python3
cachedir: .pytest_cache
Django settings: python_webapp_django.settings (from command line option)
rootdir: /project-python-django-webapp, inifile:
plugins: django-3.3.2
collecting ... collected 1 item

test_server.py::test_index PASSED                                        [100%]

=========================== 1 passed in 1.13 seconds ===========================
Removing intermediate container e6292f2ac3e8
 ---> c78c589b6d74
Successfully built c78c589b6d74
Successfully tagged so/example:latest

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Docker - Elasticsearch - 无法建立新连接:[Errno 111] Connection refused',)) - Docker - Elasticsearch - Failed to establish a new connection: [Errno 111] Connection refused',)) 请求 - Python - 无法建立新连接:[Errno 111] 连接被拒绝 - Requests - Python - Failed to establish a new connection: [Errno 111] Connection refused 连接错误(&#39;<urllib3.connection.HTTPSConnection object at 0x7f3a5d760390> : 建立新连接失败: [Errno 111] 连接被拒绝&#39;) - ConnectionError('<urllib3.connection.HTTPSConnection object at 0x7f3a5d760390>: Failed to establish a new connection: [Errno 111] Connection refused') urllib3 - 无法建立新连接:[Errno 111] - urllib3 - Failed to establish a new connection: [Errno 111] 新连接错误(<urllib3.connection.HTTPConnection object at 0x7f09b9f0a7c0> : 建立新连接失败: [Errno 111] 连接被拒绝) - NewConnectionError(<urllib3.connection.HTTPConnection object at 0x7f09b9f0a7c0>: Failed to establish a new connection: [Errno 111] Connection refused) Docker 安装库时映像构建失败:无法建立新连接:[Errno -5] 没有与主机名关联的地址 - Docker Image build fails when installing libraries: Failed to establish a new connection: [Errno -5] No address associated with hostname Docker 构建问题 pip install -r 要求: 无法建立新连接: [Errno -2] 名称或服务未知 - Docker build problems with pip install -r requirements: Failed to establish a new connection: [Errno -2] Name or service not known ConnectionRefusedError:[Errno 111]连接被拒绝 - ConnectionRefusedError: [Errno 111] Connection refused Mysql([Errno 111] 连接被拒绝)”) - Mysql ([Errno 111] Connection refused)") GraphQL:[Errno 111] 连接被拒绝 - GraphQL: [Errno 111] Connection refused
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM