我在AWS弹性beanstalk上运行一个多容器应用程序,该应用程序工作并连接到我的本地postgresql数据库,我docker-compose up
。但是当我将它部署在弹性beanstalk上时,会出现错误
File "/usr/local/lib/python3.6/site-packages/psycopg2/__init__.py", line 130, in connect
conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
django.db.utils.OperationalError: could not translate host name "db" to
address: Name or service not known
我读了一些帖子,说它是我的app.x目录中的settings.py,我需要将'HOST': 'localhost',
设置为'HOST': 'db',
,因为这是{{1}的服务} 在寻找?这是我的docker-compose.yml
和我的settings.py
文件。如果这是正确的或者似乎还有其他任何问题,请告诉我。
settings.py:
docker-compose.yml
搬运工-compose.yml:
if 'RDS_DB_NAME' in os.environ:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': os.environ['RDS_DB_NAME'],
'USER': os.environ['RDS_USERNAME'],
'PASSWORD': os.environ['RDS_PASSWORD'],
'HOST': os.environ['RDS_HOSTNAME'],
'PORT': os.environ['RDS_PORT'],
}
}
else:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'actualnameofdb',
'USER': 'xxx',
'PASSWORD': '*****',
'HOST': 'db',
'PORT': '5432',
}
}
dockerfile:
version: '3'
services:
db:
image: postgres:10.1
environment:
- POSTGRES_USER=rdeng
- POSTGRES_PASSWORD=walterfedy
- POSTGRES_DB=db1
ports:
- "5432:5432"
volumes:
- postgres_data:/var/lib/postgresql/data/
web:
#image: rdeng/test:latest
build: $PWD
environment:
- RDS_DB_NAME=db1
- RDS_USERNAME=rdeng
- RDS_HOSTNAME=db
- RDS_PASSWORD=walterfedy
- RDS_PORT=5432
command: bash -c "python /code/manage.py makemigrations && python /code/manage.py migrate && python /code/manage.py runserver 0.0$
ports:
- "8000:8000"
depends_on:
- db
volumes:
postgres_data: