python - Gitlab runner Docker 执行器不连接到 postgres 服务

标签 python django postgresql gitlab-ci

我成功地让 runner 能够执行 git clone 以安装 Django 依赖项。现在我正在解决下一个问题。它是 Postgres

我的最终目标是 pytest,但现在我将使用 python manager.py test 测试 gitlab-ci 脚本。

Successfully installed appnope-0.1.0 boto3-1.4.7 botocore-1.7.47 certifi-2017.11.5 chardet-3.0.4 collectfast-0.5.2 decorator-4.1.2 django-1.11.7 django-choices-1.6.0 django-cors-headers-2.1.0 django-countries-5.0 django-debug-toolbar-1.9.1 django-dirtyfields-1.3 django-environ-0.4.4 django-extensions-1.9.7 django-filter-1.1.0 django-geoposition django-guardian-1.4.9 django-money django-reversion-2.0.10 django-s3-folder-storage-0.5 django-storages-1.6.5 djangorestframework-3.7.3 djangorestframework-jwt-1.11.0 docutils-0.14 freezegun-0.3.9 gevent-1.2.2 greenlet-0.4.12 gunicorn-19.7.1 idna-2.6 ipython-6.2.1 ipython-genutils-0.2.0 jedi-0.11.0 jmespath-0.9.3 model-mommy-1.4.0 olefile-0.44 parso-0.1.0 pexpect-4.3.0 pickleshare-0.7.4 pillow-4.3.0 prompt-toolkit-1.0.15 psycopg2-2.7.3.2 ptyprocess-0.5.2 py-1.5.2 py-moneyed-0.7.0 pygments-2.2.0 pyjwt-1.5.3 pytest-3.2.5 pytest-django-3.1.2 python-dateutil-2.6.1 pytz-2017.3 requests-2.18.4 rest-framework-generic-relations-1.1.0 s3transfer-0.1.11 simplegeneric-0.8.1 six-1.11.0 sqlparse-0.2.4 traitlets-4.3.2 typing-3.6.2 urllib3-1.22 wcwidth-0.1.7 werkzeug-0.12.2
$ python manage.py test
/usr/local/lib/python3.6/site-packages/django/db/backends/postgresql/base.py:267: RuntimeWarning: Normally Django will use a connection to the 'postgres' database to avoid running initialization queries against the production database when it's not needed (for example, when running tests). Django was unable to create a connection to the 'postgres' database and will use the default database instead.
  RuntimeWarning
Creating test database for alias 'default'...
Traceback (most recent call last):
  File "/usr/local/lib/python3.6/site-packages/django/db/backends/base/base.py", line 213, in ensure_connection
    self.connect()
  File "/usr/local/lib/python3.6/site-packages/django/db/backends/base/base.py", line 189, in connect
    self.connection = self.get_new_connection(conn_params)
  File "/usr/local/lib/python3.6/site-packages/django/db/backends/postgresql/base.py", line 176, in get_new_connection
    connection = Database.connect(**conn_params)
  File "/usr/local/lib/python3.6/site-packages/psycopg2/__init__.py", line 130, in connect
    conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
psycopg2.OperationalError: could not connect to server: Connection refused
    Is the server running on host "localhost" (127.0.0.1) and accepting
    TCP/IP connections on port 5432?
could not connect to server: Cannot assign requested address
    Is the server running on host "localhost" (::1) and accepting
    TCP/IP connections on port 5432?

.gitlab-ci.yml

image: python:3.6
services:
  - postgres:latest

variables:
  POSTGRES_DB: poinkdb
  POSTGRES_USER: postgres
  POSTGRES_PASSWORD: postgres

before_script:
  - 'which ssh-agent || ( apt-get update -y && apt-get install openssh-client -y )'
  - eval $(ssh-agent -s)
  - echo "$SSH_PRIVATE_KEY" | tr -d '\r' | ssh-add - > /dev/null
  - mkdir -p ~/.ssh
  - chmod 700 ~/.ssh
  - ssh-keyscan github.com >> ~/.ssh/known_hosts
  - chmod 644 ~/.ssh/known_hosts
  - git config --global user.email "sarit@elcolie.com"
  - git config --global user.name "sarit"
  - python -V
  - pip install -r requirements.txt

test:
  tags:
    - poink
    - Elcolie
  script:
  - sleep 10
  - python manage.py test

仅供引用:
我在 docker 和 postgres gitlab-ci 上搜索过。却发现无关紧要的话题。它们是 Django 和 Postgres 之间的 docker 网络,并将配置放在单个 docker-compose 中。我知道 docker-compose。我已经使用它一年了。

但他们不是我的情况。我在 runner 中询问。

引用资料:

https://medium.com/pyslackers/setting-up-tests-in-gitlab-ci-for-django-project-with-docker-engine-44f01940424d
https://github.com/gitlabhq/gitlabhq/blob/master/vendor/gitlab-ci-yml/Django.gitlab-ci.yml

解决方案:
@dvnguyen 下次提醒我自己。

test:
  tags:
    - poink
    - Elcolie
  variables:
    DATABASE_URL : "postgres://postgres:postgres@postgres:5432/poinkdb"
  script:
  - python manage.py test

最佳答案

Is the server running on host "localhost" (127.0.0.1) and accepting TCP/IP connections on port 5432?

Postgres 不在 gitlab-ci 运行程序的“本地主机”中。 gitlab-ci 运行在一个容器上,而 postgresql 运行在另一个容器上。

正如您在 .gitlab-ci.yml 中指定的那样,只需使用名称“postgres”即可发现 Postgres 容器: 服务: - postgres:最新

因此在您的代码中用“postgres”替换“localhost”将解决您的问题。

关于python - Gitlab runner Docker 执行器不连接到 postgres 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47966215/

相关文章:

python - 如何使用 django 中的中间件同时处理基于 session 和基于 token 的身份验证?

Django,post_delete,使用反向关系安全吗?

qt - 无法编译源代码-没有这样的文件或目录<libpq-fe.h>

sql - Postgres : Statistical functions on date time intervals

python - Airflow 调度程序异常停止

python - 如何使 'while' 循环将循环条件的结果打印到单行(串联)中?

python - 使用 Python 将字典与列表值组合

python - Flask - 基本身份验证的装饰器

python - 如何使用 python 的 paramiko 获取每日 SSH 消息 (MOTD)?

Django:为 request.user 使用自定义类?