我在 gitlab 上有一个 Django 应用程序,我正在尝试使用 gitlab-ci 添加 CI/CD。我在 EC2 上启动了一个服务器并在上面安装了 gitlab-runner。
我将 .gitlab-ci.yml
文件添加到我的项目中并推送了最新的更改。我可以看到正在运行的管道和正在安装的依赖项。但是当脚本尝试运行测试时,我收到如下错误:
django.db.utils.OperationalError: could not connect to server: Connection refused
Is the server running on host "localhost" (::1) and accepting TCP/IP connections on port 5432?
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?
下面是我的.gitlab-ci.yaml
脚本:
image: python:3-jessie
services:
- postgres:latest
before_script:
- python -V
- apt-get -y update
- apt-get install -y python-dev python-pip
- pip install --upgrade pip
stages:
- build
build:
type: build
variables:
DATABASE_URL: postgres://postgres:@postgres:5432/opensys_erp
script:
- pip install -r requirements.txt
- python manage.py test
only:
- branches
为什么我的应用程序无法连接到 postgres 服务器?
最佳答案
当您的容器尝试在他们自己的本地主机上连接 postgres 时,这就是您拒绝连接的原因。 127.0.0.1
这个localhost是Django application
容器的localhost。要使用本地主机连接 postgress,您需要链接您的 docker 容器。
Docker also has a linking system that allows you to link multiple containers together and send connection information from one to another. When containers are linked, information about a source container can be sent to a recipient container.
服务如何与工作联系起来
To summarize, if you add mysql as service to your application, the image will then be used to create a container that is linked to the job container.
The service container for MySQL will be accessible under the hostname mysql. So, in order to access your database service you have to connect to the host named mysql instead of a socket or localhost.
在我针对同一类问题发布详细答案的前一天,您也可以查看此问题。
https://stackoverflow.com/a/49342027/3288890
你可以查看一些链接
https://docs.docker.com/network/links/
https://docs.gitlab.com/ce/ci/docker/using_docker_images.html
https://docs.gitlab.com/ce/ci/docker/using_docker_images.html#accessing-the-services
关于postgresql - Docker 容器中的 Django 应用程序无法找到 postgres,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49376961/