简体   繁体   English

docker-compose有两个容器:Web无法连接到数据库

[英]docker-compose with two containers: web can not connect to db

docker-compose fails to build web component because it can not connect to the previously created db component docker-compose无法构建Web组件,因为它无法连接到先前创建的数据库组件

Mac OSX 10.13.6, conda 4.5.11, Python 3.6.8, Docker version 18.09.1, docker-compose version 1.23.2 Mac OSX 10.13.6,conda 4.5.11,Python 3.6.8,Docker版本18.09.1,docker-compose版本1.23.2

django 1.8.3 gets installed with requirements.txt from Dockerfile. django 1.8.3从Dockerfile中随Requirements.txt一起安装。 Not at liberty to upgrade. 不能随意升级。

Several very similar discussions on SO did not help (like this one: Docker-compose with django could not translate host name "db" to address: Name or service not known ). 关于SO的一些非常相似的讨论无济于事(例如: django的Docker-compose无法将主机名“ db”转换为地址:名称或服务未知 )。

I have a docker-compose.yml with a network and two components: 我有一个带有网络和两个组件docker-compose.yml

version: '3'
networks:
  bridge:
   driver: bridge
services:
  db:
    image: postgres:10
    container_name: myapp-db
    volumes:
      - ./postgres_data:/var/lib/postgresql/data/
    ports:
      - "5432:5432"
    environment:
     POSTGRES_DB: actionability-master
     POSTGRES_PASSWORD: postgres
     POSTGRES_USER: postgres
    networks:
      - bridge


  web:
    restart: unless-stopped
    container_name: myapp-web
    build: .
    command: /start_gunicorn.sh
    ports:
      - "8080:8080"
    environment:
      PRODUCTION: 'true'
    networks:
      - bridge

In my settings.py I have DATABASES section: 在我的settings.py我有DATABASES部分:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': INSTANCE_NAME,
        'USER': 'postgres',
        'PASSWORD': 'postgres',
        'HOST': 'db',
        'PORT': '5432'
    },

}

When I run $ docker-compose up -d , the first image (db) gets created and its container gets started. 当我运行$ docker-compose up -d ,将创建第一个映像(db)并启动其容器。 Can see it's running and listening on port 5432 with docker ps and lsof . 可以看到它正在运行,并且正在使用docker pslsof在端口5432上侦听。 The same thing happens if I remove web: component from docker-compose.yml file 如果我从docker-compose.yml文件中删除web:组件, docker-compose.yml发生相同的事情

Now, the second component (web) has Dockerfile that contains these two lines (among many others): 现在,第二个组件(Web)具有包含以下两行(以及其他许多行)的Dockerfile:

RUN python manage.py makemigrations myapp
RUN python manage.py migrate

The "migrate" like dies with this error: 像“迁移”一样死于此错误:

Traceback (most recent call last):
File "/usr/local/lib/python3.6/site-packages/django/db/backends/base/base.py", line 130, in ensure_connection
    self.connect()
File "/usr/local/lib/python3.6/site-packages/django/db/backends/base/base.py", line 119, in connect
    self.connection = self.get_new_connection(conn_params)
File "/usr/local/lib/python3.6/site-packages/django/db/backends/postgresql_psycopg2/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 "db" (37.34.32.51) and accepting TCP/IP connections on port 5432?

I tired to several tweaks. 我累了几次调整。 * changed version '3.7' * added to db section *更改了version '3.7' *添加到数据库部分

expose:
    - "5432"
  • added to web component: 添加到Web组件:

depends_on: - "db"

  • added to web component: 添加到Web组件:

links: - "db"

  • set PRODUCTION to false 将PRODUCTION设置为false

environment: PRODUCTION: 'false'

  • changed HOST in settings.py to container name, to image name, to tags, to container id, to 'localhost', to '127.0.0.1', etc.. the error is the same, just mentioning the new HOST name instead of 'db' settings.py HOST更改为容器名称,图像名称,标签,容器ID,“ localhost”,“ 127.0.0.1”等。错误是相同的,只是提及新的HOST名称而不是'D b'
  • ran outside of conda env 在conda env外面跑了
  • ran with --build switch ( docker-compose up -d --build ) 使用--build开关运行( docker-compose up -d --build
  • did docker system prune and ran again docker system prune并再次运行

All the same error. 所有相同的错误。


UPDATE: After a sugestion that it docker-compose does not work this way I tried to split it into two separate tasks. 更新:建议它docker-compose无法以这种方式工作后,我尝试将其拆分为两个单独的任务。 First I build myapp-db container and make sure it's running on the correct port : 首先,我建立myapp-db容器并确保它在正确的端口上运行:

$ docker container ps


CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                      NAMES
c110e8361cda        postgres:10         "docker-entrypoint.s…"   4 hours ago         Up 4 hours          0.0.0.0:5432->5432/tcp     myapp-db

Then I build myapp-web: 然后我建立myapp-web:

docker build -t myapp-web .

The same error still happens. 仍然发生相同的错误。 So, why is it not finding the db container NOW? 那么,为什么现在不找到数据库容器呢?

I use the depends_on list to start the db container before the web container and the links to ensure that the host names can be resolved. 我使用depends_on列表在Web容器和links之前启动数据库容器,以确保可以解析主机名。

I add the following to the web: 我将以下内容添加到网络:

services:
  db:
    # ...
  web:
    links:
    - "db:db" # resolve the hostname "db" with the ip of the db container
    depends_on:
    - db # start db before web

Example

I know this may seem really basic, but why not consider a modestly more sophisticated naming convention, and then simply add those names to your DNS service on the host, or at least the A records? 我知道这看起来似乎很基础,但是为什么不考虑一个稍微复杂一些的命名约定,然后将那些名称简单地添加到主机上的DNS服务或至少添加A记录呢?

Then the system can reach the database by name or by IP. 然后,系统可以通过名称或IP到达数据库。 Just a thought. 只是一个想法。

Great choices and combination of platform and languages btw, Django + PostgreSQL are my favorite. 很棒的选择以及平台和语言的结合,顺便说一句,Django + PostgreSQL是我的最爱。 I would also recommend Bootstrap, especially if you are looking for rapid tool deployment capabilities. 我也建议使用Bootstrap,尤其是在您正在寻找快速工具部署功能的情况下。

You can try this configuration with network aliases. 您可以尝试使用网络别名进行此配置。

version: '3.5'
services:

  db:
    image: postgres:10
    container_name: myapp-db
    volumes:
      - ./postgres_data:/var/lib/postgresql/data/
    expose:
      - "5432"
    environment:
       POSTGRES_DB: actionability-master
       POSTGRES_PASSWORD: postgres
       POSTGRES_USER: postgres
    networks:
      services-network:
        aliases:
         - db

  web:
    restart: unless-stopped
    container_name: myapp-web
    build: .
    command: /start_gunicorn.sh
    ports:
      - "8080:8080"
    environment:
      PRODUCTION: 'true'
    depends_on:
      - db
    networks:
      services-network:
        aliases:
         - web

networks:
   services-network:
     name: services-network
     driver: bridge

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM