Access localhost from within a docker image.

huangapple go评论60阅读模式
英文:

Access localhost from within a docker image

问题

以下是翻译好的内容:

我有以下的 .gitlab-ci.yml 文件:

图像: python:3.8

variables:
  PIP_CACHE_DIR: "$CI_PROJECT_DIR/.cache/pip"

cache:
  paths:
    - .cache/pip
    - venv/

before_script:
  - python -V  # 用于调试打印 Python 版本
  - pip install --upgrade pip
  - pip install virtualenv
  - virtualenv venv
  - source venv/bin/activate
  - pip install -r requirements.txt
  - cp properties_ci properties
  - apt-get update
  - apt-get install net-tools

stages:
  - build
  - test
  - security
  - leanix

include:
  - ...

test:unit:
  stage: ...

test:integration:
    stage: test
    script:
        - echo "0"
        - python app.py &
        - curl 127.0.0.1:8126
        - py.test tests/integration/test_integration.py
    services:
        - name: cassandra:3.11

当我使用命令 python app.py 启动我的 Python 应用程序时,我可以看到以下输出:

$ python app.py
警告:cassandra.cluster:Cluster.__init__ 调用时指定了 contact_points,但没有指定 load_balancing_policy。在下一个主要版本中,这将引发错误;请指定一个负载均衡策略。(contact_points = ['cassandra'], lbp = None)
警告:cassandra.connection:未发送身份验证挑战,这很可疑,因为驱动程序预期进行身份验证(配置的 authenticator = PlainTextAuthenticator)
警告:cassandra.connection:未发送身份验证挑战,这很可疑,因为驱动程序预期进行身份验证(配置的 authenticator = PlainTextAuthenticator)
创建 keyspace(如果不存在)...
创建 tables(如果不存在)...
 * 提供 Flask 应用 'src' (延迟加载)
 * 环境: production
   警告: 这是一个开发服务器。请勿在生产部署中使用它。
   请改用生产 WSGI 服务器。
 * 调试模式: 关闭
INFO:werkzeug:警告: 这是一个开发服务器。请勿在生产部署中使用它。
 * 运行在 http://127.0.0.1:8126

因此,连接到 Cassandra 并创建/插入数据工作正常,但集成测试无法访问本地主机上的 Python 应用程序。

在我的集成测试中,我调用 http://127.0.0.1:8126,但显示以下信息:

创建 keyspace(如果不存在)...
=========================== 简短的测试摘要信息 ============================
错误 tests/integration/test_integration.py - requests.exceptions.ConnectionError: HTTPConnectionPool(host='localhost', port=8126): Max retries exceeded with url: /getCSRFToken (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7f316d3df700>: Failed to establish a new connection: [Errno 111] Connection refused'))
!!!!!!!!!!!!!!!!!!!! 中断: 收集期间的 1 个错误 !!!!!!!!!!!!!!!!!!!!
=============================== 3.05 秒内的 1 个错误 ===============================

如何在 Docker 容器内调用本地主机以便访问我的应用程序?我尝试了 127.0.0.1 也不起作用。

编辑 1:

脚本 test_integration.py 包含以下代码的小示例部分:

...
url = "http://localhost:8126/series/"

import zipfile
with zipfile.ZipFile("tests/integration/20230323.zip", mode="r") as archive:
  # 遍历 zip 文件中的文件
  for zipfilename in archive.filelist:
    txtdata = archive.read(zipfilename)
    if (zipfilename.filename == 'OP_lastchange_CRUDE_2023-03-23.json'):
      payload2 = json.dumps(json.loads(txtdata))
      response = requests.request("POST", url, headers=headers, data=payload2)
      assert response.status_code == 204, "圣诞老人今年不来了!"
...

编辑 2:

解决方案由 @KamilCuk 提供 - 在 before_script 部分将 Python 应用程序设置为在后台运行,如 python app.py &,并休眠几秒钟,使我的 Python 应用程序可用于调用,例如 http://localhost:8126

英文:

I have the following .gitlab-ci.yml file :

image: python:3.8

variables:
  PIP_CACHE_DIR: &quot;$CI_PROJECT_DIR/.cache/pip&quot;

cache:
  paths:
    - .cache/pip
    - venv/

before_script:
  - python -V  # Print out python version for debugging
  - pip install --upgrade pip
  - pip install virtualenv
  - virtualenv venv
  - source venv/bin/activate
  - pip install -r requirements.txt
  - cp properties_ci properties
  - apt-get update
  - apt-get install net-tools

stages:
  - build
  - test
  - security
  - leanix

include:
  - ...

test:unit:
  stage: ...

test:integration:
    stage: test
    script:
        - echo &quot;0&quot;
        - python app.py &amp;
        - curl 127.0.0.1:8126
        - py.test tests/integration/test_integration.py
    services:
        - name: cassandra:3.11

When I launch my python application by using the command python app.py I can see the following output :

$ python app.py
WARNING:cassandra.cluster:Cluster.__init__ called with contact_points specified, but no load_balancing_policy. In the next major version, this will raise an error; please specify a load-balancing policy. (contact_points = [&#39;cassandra&#39;], lbp = None)
WARNING:cassandra.connection:An authentication challenge was not sent, this is suspicious because the driver expects authentication (configured authenticator = PlainTextAuthenticator)
WARNING:cassandra.connection:An authentication challenge was not sent, this is suspicious because the driver expects authentication (configured authenticator = PlainTextAuthenticator)
Creating keyspace if not exist...
Creating tables if not exist...
 * Serving Flask app &#39;src&#39; (lazy loading)
 * Environment: production
   WARNING: This is a development server. Do not use it in a production deployment.
   Use a production WSGI server instead.
 * Debug mode: off
INFO:werkzeug:WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.
 * Running on http://127.0.0.1:8126

So connecting to Cassandra and creating/inserting data works well, but the integration tests cannot access the python application on localhost.

In my integration tests I call http://127.0.0.1:8126 but it says the following :

Creating keyspace if not exist...
=========================== short test summary info ============================
ERROR tests/integration/test_integration.py - requests.exceptions.ConnectionError: HTTPConnectionPool(host=&#39;localhost&#39;, port=8126): Max retries exceeded with url: /getCSRFToken (Caused by NewConnectionError(&#39;&lt;urllib3.connection.HTTPConnection object at 0x7f316d3df700&gt;: Failed to establish a new connection: [Errno 111] Connection refused&#39;))
!!!!!!!!!!!!!!!!!!!! Interrupted: 1 error during collection !!!!!!!!!!!!!!!!!!!!
=============================== 1 error in 3.05s =============================== 

Any ideas how to call localhost inside the docker container so that I can access my application ? I tried also 127.0.0.1 and it does not work.

EDIT 1

The script test_integration.py contains as a small example the following portion of code :

...
url = &quot;http://localhost:8126/series/&quot;

import zipfile
with zipfile.ZipFile(&quot;tests/integration/20230323.zip&quot;, mode=&quot;r&quot;) as archive:
  #Iterate through files in zip file
  for zipfilename in archive.filelist:
    txtdata = archive.read(zipfilename)
    if (zipfilename.filename == &#39;OP_lastchange_CRUDE_2023-03-23.json&#39;):
      payload2 = json.dumps(json.loads(txtdata))
      response = requests.request(&quot;POST&quot;, url, headers=headers, data=payload2)
      assert response.status_code == 204, &quot;Santa Claus is not coming this year !&quot;
	  
... 

EDIT 2

The solution is given by @KamilCuk - setting the python app to run in the background in the section before_script like python app.py &amp; and sleeping for several seconds makes my python application available to be called like http://localhost:8126.

答案1

得分: 1

请查看夹具替代方案。将以下内容放入您的 conftest.py 文件中:

@pytest.fixture(scope="session")
def app(request):
    pp = subprocess.Popen("python app.py", shell=True)
    time.sleep(5) # TODO: 替换为等待端口打开
    request.addfinalizer(lambda: (pp.terminate(), pp.wait()))
    return pp

然后在测试中使用:

def test_something(app):
    pass
英文:

Instead consider fixtures. Put the following in your conftest.py:

@pytest.fixture(scope=&quot;session&quot;)
def app(request):
   pp = subprocess.popen(&quot;python app.py&quot;)
   time.sleep(5) # TODO: replace with waiting for open port
   request.addfinalizer(lambda: (pp.terminate(), pp.wait()))
   return pp

Then in tests use:

def test_something(app):
     pass

答案2

得分: 0

以下是代码部分的中文翻译:

image: python:3.8

variables:
  PIP_CACHE_DIR: "$CI_PROJECT_DIR/.cache/pip"

cache:
  paths:
    - .cache/pip
    - venv/

before_script:
  - ...
  - python app.py &
  - sleep 20 # 等待应用程序启动

stages:
  - 构建
  - 运行
  - 测试
  - 安全
  - leanix

include:
  - 项目: ...

services:
  - 名称: cassandra:3.11
    别名: cassandra

test:unit:
  阶段: ...

test:integration:
  阶段: 测试
  脚本:
    - echo "0"
    - py.test tests/integration/test_series.py

请注意,代码中的注释部分没有翻译。

英文:

One of the working solutions, in my case, proposed by KamilCuk, is the following :

image: python:3.8

variables:
  PIP_CACHE_DIR: &quot;$CI_PROJECT_DIR/.cache/pip&quot;

cache:
  paths:
    - .cache/pip
    - venv/

before_script:
  - ...
  - python app.py &amp;
  - sleep 20 # wait for application to start-up

stages:
  - build
  - run
  - test
  - security
  - leanix

include:
  - project: ...

services:
  - name: cassandra:3.11
    alias: cassandra

test:unit:
  stage: ...

test:integration:
  stage: test
  script:
    - echo &quot;0&quot;
    - py.test tests/integration/test_series.py

The solution is given by @KamilCuk - setting the python app to run in the background in the section before_script like python app.py &amp; and sleeping for several seconds sleep 20 makes my python application available to be called like http://localhost:8126.

I need to investigate the other solution proposed by KamilCuk using fixtures.

huangapple
  • 本文由 发表于 2023年4月11日 17:00:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/75984105.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定