gpt4 book ai didi

postgresql - Docker Compose 和 Postgres : Name does not resolve

转载 作者:IT老高 更新时间:2023-10-28 21:22:32 24 4
gpt4 key购买 nike

在 Docker 中,我正在尝试配置 Postgres,使其在另一个容器中启动并运行,并将其链接到我的 users 服务,使用以下结构进行设置:

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"]

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

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 实例的启动和健康,因此我添加了一个 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

ma​​nage.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

我在上面的代码中遗漏了什么?

最佳答案

Dockerfile-dev中需要安装bind-tools解决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

关于postgresql - Docker Compose 和 Postgres : Name does not resolve,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54433522/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com