Docker-compose和Postgres:名称无法解析

时间:2019-01-30 04:49:10

标签: postgresql docker docker-compose

docker中,我试图配置Postgres,使其在另一个容器中运行,并将其链接到我的users服务,并通过以下结构进行设置:< / p>

docker-compose-dev.yml
services/
        users/
             manage.py
             Dockerfile-dev
             entrypoint.sh
             project/
                    __init__.py
                    config.py
                    db/
                      create.sql
                      Dockerfile

docker-compose-dev.yml

version: '3.7'

services:

  users:
    build:
      context: ./services/users
      dockerfile: Dockerfile-dev
    volumes:
      - './services/users:/usr/src/app'
    ports:
      - 5001:5000
    environment:
      - FLASK_APP=project/__init__.py
      - FLASK_ENV=development
      - APP_SETTINGS=project.config.DevelopmentConfig
      - DATABASE_URL=postgres://postgres:postgres@users-db:5432/users_dev 
      - DATABASE_TEST_URL=postgres://postgres:postgres@users-db:5432/users_test  
    depends_on:  
      - users-db

  users-db:  
    build:
      context: ./services/users/project/db
      dockerfile: Dockerfile
    ports:
      - 5435:5432
    environment:
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=postgres

Dockerfile-dev

# base image
FROM python:3.7.2-alpine

# install dependencies
RUN apk update && \
    apk add --virtual build-deps gcc python-dev musl-dev && \
    apk add postgresql-dev && \
    apk add netcat-openbsd

# set working directory
WORKDIR /usr/src/app

# add and install requirements
COPY ./requirements.txt /usr/src/app/requirements.txt
RUN pip install -r requirements.txt

# add entrypoint.sh
COPY ./entrypoint.sh /usr/src/app/entrypoint.sh
RUN chmod +x /usr/src/app/entrypoint.sh

# add app
COPY . /usr/src/app

# run server
CMD ["/usr/src/app/entrypoint.sh"]

在这里,我尝试通过将Postgres文件添加到容器中的SQL目录中来扩展官方"docker-entrypoint-initdb.d"图像。

Dockerfile

# base image
FROM postgres:11.1-alpine

# run create.sql on init
ADD create.sql /docker-entrypoint-initdb.d

create.sql

CREATE DATABASE users_prod;
CREATE DATABASE users_dev;
CREATE DATABASE users_test;

由于“ users” 服务不仅取决于容器的正常运行,而且还取决于实际的Postgres实例是否正常运行,因此我向{em添加了entrypoint.sh文件>“用户” :

entrypoint.sh

#!/bin/sh

echo "Waiting for postgres..."

while ! nc -z users-db 5432; do
  sleep 0.1
done

echo "PostgreSQL started"

python manage.py run -h 0.0.0.0

manage.py

from flask.cli import FlaskGroup
from project import app, db

cli = FlaskGroup(app)
@cli.command('recreate_db')
def recreate_db():
    db.drop_all()
    db.create_all()
    db.session.commit()
if __name__ == '__main__':
    cli()

config.py

import os  

class BaseConfig:
    """Base configuration"""
    TESTING = False
    SQLALCHEMY_TRACK_MODIFICATIONS = False  

class DevelopmentConfig(BaseConfig):
    """Development configuration"""
    SQLALCHEMY_DATABASE_URI = os.environ.get('DATABASE_URL') 

class TestingConfig(BaseConfig):
    """Testing configuration"""
    TESTING = True
    SQLALCHEMY_DATABASE_URI = os.environ.get('DATABASE_TEST_URL')  

class ProductionConfig(BaseConfig):
    """Production configuration"""
    SQLALCHEMY_DATABASE_URI = os.environ.get('DATABASE_URL')  

我运行并成功使用:

docker-compose -f docker-compose-dev.yml up -d --build

但是我有一些网络问题。如果我运行:

docker-compose -f docker-compose-dev.yml logs

我得到:

Attaching to dev2_users_1
users_1  | Waiting for postgres...
users_1  | nc: getaddrinfo: Name does not resolve
users_1  | nc: getaddrinfo: Name does not resolve
users_1  | nc: getaddrinfo: Name does not resolve
users_1  | nc: getaddrinfo: Name does not resolve
users_1  | nc: getaddrinfo: Name does not resolve
(...)

CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
6ca5718c9867        dev2_users          "/usr/src/app/entryp…"   2 minutes ago       Up 2 minutes        0.0.0.0:5001->5000/tcp   dev2_users_1

有人可以在这里向我指出正确的方向吗?上面的代码中我缺少什么?

1 个答案:

答案 0 :(得分:7)

您需要为bind-tools问题在Dockerfile-dev中安装Name does not resolve

apk add bind-tools 

测试:

/usr/src/app # nslookup users-db
nslookup: can't resolve '(null)': Name does not resolve

Name:      users-db
Address 1: 172.20.0.2 stc_users-db_1_f435854a8c88.stc_default
/usr/src/app # apk add bind-tools
(1/4) Installing json-c (0.13.1-r0)
(2/4) Installing libxml2 (2.9.8-r1)
(3/4) Installing bind-libs (9.12.3-r0)
(4/4) Installing bind-tools (9.12.3-r0)
Executing busybox-1.28.4-r3.trigger
OK: 188 MiB in 62 packages
/usr/src/app # nslookup users-db
Server:         127.0.0.11
Address:        127.0.0.11#53

Non-authoritative answer:
Name:   users-db
Address: 172.20.0.2