1
私のdjangoアプリはpostgresqlに接続できません。私はdockerfileのdjangoを使っていますし、postgresの公式画像とdocker-composeを使ってビルドしています。docker composeはpostgresqlデータベースとdjangoを接続できません
ドッカー-compose.yml
version: '2'
services:
db:
image: eg_postgresql
expose:
- 5432
environment:
- POSTGRES_PASSWORD=docker
- POSTGRES_USER=docker
- POSTGRES_DB=postgres
web:
build: .
command: python3 manage.py runserver 0.0.0.0:8000
volumes:
- .:/test_application
ports:
- "8000:8000"
links:
- "db:db"
environment:
- DATABASE_URL=postgres://docker:[email protected]:5432/postgres
- DJANGO_SECRET_KEY=x7-g-xu^h5k%[email protected])7q9frn9_l6tmefvf)y=0)d!uh
出力:
conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
django.db.utils.OperationalError: could not connect to server: Connection refused
Is the server running on host "localhost" (127.0.0.1) and accepting
TCP/IP connections on port 5432?
could not connect to server: Cannot assign requested address
Is the server running on host "localhost" (::1) and accepting
TCP/IP connections on port 5432?
ドッカー-コンPS
[email protected]:~/dj1/helloworld$ sudo docker-compose ps
Name Command State Ports
----------------------------------------------------------------------------------
helloworld_db_1 /usr/lib/postgresql/9.3/bi ... Up 5432/tcp
helloworld_web_1 python3 manage.py runserve ... Up 0.0.0.0:8000->8000/tcp
アプリsettings.pyで
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'postgres',
'USER': 'docker',
'PASSWORD': 'docker',
'HOST': 'db',
'PORT': '5432',
}
}
私は接続する多くの方法で試みたが、問題は同じです...
が、問題があります同じ –
あなたはウェブコンテナのping dbの中にいらっしゃいますか? @RatulBasak – Sergiu
はい。 Webはdb –