我的docker-compose文件如下
version: "3"
volumes:
postgres:
driver: local
services:
postgres:
image: postgres:11.1
environment:
- POSTGRES_NAME=postgres
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=mysecretpassword
- PGDATA=pgdata
volumes:
- postgres:/var/lib/postgresql/data/pgdata
backend:
build:
context: backend/
ports:
- "8000:8000"
environment:
- PRODUCTION=false
- config_file=/srv/backend/config/config.json
volumes:
- ./backend:/srv/backend
depends_on:
- postgres
frontend:
build:
context: frontend/
ports:
- "8080:8080"
volumes:
- ./frontend:/srv/frontend
scraper:
build:
context: scraper/
volumes:
- ./scraper:/srv/scraper
我在运行docker时遇到错误,
backend_1 | self.ensure_connection()
backend_1 | File "/usr/local/lib/python3.7/site-packages/django/utils/asyncio.py", line 26, in inner
backend_1 | return func(*args, **kwargs)
backend_1 | File "/usr/local/lib/python3.7/site-packages/django/db/backends/base/base.py", line 220, in ensure_connection
backend_1 | self.connect()
backend_1 | File "/usr/local/lib/python3.7/site-packages/django/db/utils.py", line 90, in __exit__
backend_1 | raise dj_exc_value.with_traceback(traceback) from exc_value
backend_1 | File "/usr/local/lib/python3.7/site-packages/django/db/backends/base/base.py", line 220, in ensure_connection
backend_1 | self.connect()
backend_1 | File "/usr/local/lib/python3.7/site-packages/django/utils/asyncio.py", line 26, in inner
backend_1 | return func(*args, **kwargs)
backend_1 | File "/usr/local/lib/python3.7/site-packages/django/db/backends/base/base.py", line 197, in connect
backend_1 | self.connection = self.get_new_connection(conn_params)
backend_1 | File "/usr/local/lib/python3.7/site-packages/django/utils/asyncio.py", line 26, in inner
backend_1 | return func(*args, **kwargs)
backend_1 | File "/usr/local/lib/python3.7/site-packages/django/db/backends/postgresql/base.py", line 185, in get_new_connection
backend_1 | connection = Database.connect(**conn_params)
backend_1 | File "/usr/local/lib/python3.7/site-packages/psycopg2/__init__.py", line 130, in connect
backend_1 | conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
backend_1 | django.db.utils.OperationalError: could not connect to server: No route to host
backend_1 | Is the server running on host "postgres" (172.18.0.2) and accepting
backend_1 | TCP/IP connections on port 5432?
backend_1 |
backend_1 | ==> Django setup, executing: collectstatic
backend_1 |
backend_1 | 0 static files copied to '/srv/backend/django/static', 184 unmodified.
backend_1 | ==> Starting ...
config.json文件如下
{
"production": false,
"django": {
"domain_frontend": "http://localhost:8080",
"default_from_email": “admin@mysite.com",
"secret_key": "mysecretkey",
"allowed_hosts": [
"localhost"
],
"cors_whitelist": [
"http://localhost:8080"
]
},
"postgres": {
"db": "postgres",
"user": "postgres",
"password": "mysecretpassword",
"host": "postgres",
"port": "5432"
}
}
我杀死了在端口5432上运行的现有处理,并尝试在docker-compose文件中添加端口,但这无济于事。
我发现此主题有可用的帮助,我尝试了其中的大多数。但是还没有运气。
请帮助。
答案 0 :(得分:0)
从日志中显示:
django.db.utils.OperationalError: could not connect to server: No route to host
Is the server running on host "postgres" (172.18.0.2) and
accepting TCP/IP connections on port 5432?
我想分配给postgres
的IP和config.json
中指定的IP可能会不同。
确保/srv/backend/config/config.json
具有与分配给postgres
相同的ip
并根据需要公开端口5432
。
version: "3"
volumes:
postgres:
driver: local
services:
postgres:
image: postgres:11.1
environment:
- POSTGRES_NAME=postgres
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=mysecretpassword
- PGDATA=pgdata
ports:
- "5432:5432"
volumes:
- postgres:/var/lib/postgresql/data/pgdata
backend:
build:
context: backend/
ports:
- "8000:8000"
environment:
- PRODUCTION=false
- config_file=/srv/backend/config/config.json
volumes:
- ./backend:/srv/backend
depends_on:
- postgres
frontend:
build:
context: frontend/
ports:
- "8080:8080"
volumes:
- ./frontend:/srv/frontend
scraper:
build:
context: scraper/
volumes:
- ./scraper:/srv/scraper
在docker-compose.yml上面尝试,并在config.json中将Postgres主机更新为localhost
和端口5432