提问人:BooRuleDie 提问时间:11/22/2022 最后编辑:BooRuleDie 更新时间:11/22/2022 访问量:369
Docker:服务器是否在主机“localhost”(127.0.0.1)上运行 [重复]
Docker: Is the server running on host "localhost" (127.0.0.1) [duplicate]
问:
我正在尝试为我的 fastAPI 应用程序创建一个 docker 容器。但是,每当我尝试运行以下复合文件时,我都会收到此错误:
错误
docker logs api-api-1
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?
当我第一次看到错误时,我以为我在容器的本地主机上运行数据库,但我检查了它,数据库在 0.0.0.0:5432 上运行。
docker logs api-postgres-1
2022-11-22 05:08:23.971 UTC [1] LOG: listening on IPv4 address "0.0.0.0", port 5432
Docker文件
FROM python:3.10
WORKDIR /usr/src/app
COPY requirements.txt ./
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8000"]
Compose.YAML 格式
version: '3'
services:
api:
image: fastapilearning
depends_on:
- postgres
ports:
- 80:8000
environment:
- ACCESSTOKENEXPIREMINUTE=${ACCESSTOKENEXPIREMINUTE}
- ALGORITHM=${ALGORITHM}
- DATABASEHOSTNAME=${DATABASEHOSTNAME}
- DATABASENAME=${DATABASENAME}
- DATABASEPASSWORD=${DATABASEPASSWORD}
- DATABASEPORT=${DATABASEPORT}
- DATABASEUSERNAME=${DATABASEUSERNAME}
- SECRETKEY=${SECRETKEY}
command: /bin/bash -c "alembic upgrade head && uvicorn app.main:app --host 0.0.0.0 --port 8000"
postgres:
image: postgres
environment:
- POSTGRES_PASSWORD=${DATABASEPASSWORD}
- POSTGRES_DB=${DATABASENAME}
volumes:
- C:\Users\booruledie\Documents\Git\freecodecamp\dockerPostgresData\:/var/lib/postgresql/data
这里有什么问题?
我确定源代码,因为当我尝试在本地计算机上运行它时,我没有收到任何错误。
答:
0赞
user7833368
11/22/2022
#1
最有可能的是 PostgreSQL 服务器没有正确关闭。
尝试:
postgres -D /usr/local/var/postgres
你看:
FATAL: lock file "postmaster.pid" already exists
HINT: Is another postmaster (PID 449) running in data directory "/usr/local/var/postgres"?
然后终止端口:
kill -9 PID
评论
0赞
BooRuleDie
11/22/2022
感谢您的回复,问题完全与环境变量有关。我忘了将 DATABASEHOSTNAME 的值从 localhost 更改为 postgres。
评论