Docker容器中的Django应用无法找到Postgres [英] Django app in Docker container unable to find postgres

查看:101
本文介绍了Docker容器中的Django应用无法找到Postgres的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在gitlab上有一个Django应用程序,我正在尝试使用gitlab-ci添加CI / CD。我在EC2上启动了一台服务器,并在其上安装了gitlab-runner。

I have a Django application on gitlab and I am trying to add CI/CD using gitlab-ci. I started a server on EC2 and installed gitlab-runner on it.

我添加了 .gitlab-ci.yml 文件添加到我的项目中,并推送了最新更改。我可以看到管道正在运行,并且正在安装依赖项。但是,当脚本尝试运行测试时,出现如下错误:

I added .gitlab-ci.yml file to my project and pushed the latest changes. I can see the pipeline running and dependencies being installed. But When the script tries to run tests, I get an error like this:


django.db.utils.OperationalError:无法连接至服务器:连接被拒绝

django.db.utils.OperationalError: could not connect to server: Connection refused

服务器是否在主机 localhost(:: 1)上运行并在端口5432上接受
TCP / IP连接?

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

无法连接到服务器:连接被拒绝

could not connect to server: Connection refused

服务器是否在主机 localhost(127.0.0.1)上运行,并且接受端口5432上的
TCP / IP连接吗?

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服务器?

Why is my application unable to connect to postgres server?

推荐答案

当您的容器尝试在自己的本地主机上连接postgres时,这就是为什么连接被拒绝的原因。 127.0.0.1 该本地主机是 Django应用程序容器的本地主机。要使用localhost连接到postgress,您需要 link 您的docker容器。

As your container try to connect postgres on their own localhost that's why you getting connection refused. 127.0.0.1 this localhost is the localhost of Django application container. To connect with postgress using localhost you need to link your docker container.


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.

服务如何链接到容器作业


总而言之,如果将mysql作为服务添加到应用程序中,则该映像将用于创建容器

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.

MySQL的服务容器将以
mysql的主机名进行访问。因此,为了访问数据库服务,您必须
连接到名为mysql的主机,而不是套接字或本地主机。

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.

在我为您也要检查的同类问题发布详细答案的前一天。

A day before I post a detailed answer for the same sort of question you check this as well.

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服务

这篇关于Docker容器中的Django应用无法找到Postgres的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆