我正在尝试安装dockero版本的mastodon,尽管我已经参考了几本指南(包括官方文档),但在连接到postgres数据库时始终遇到错误。
这是我的docker-compose.yml的相关部分:
db:
restart: always
image: postgres:9.6-alpine
shm_size: 256mb
networks:
- internal_network
healthcheck:
test: ["CMD", "pg_isready", "-U", "postgres"]
volumes:
- ./postgres:/var/lib/postgresql/data
...和我的.env.production:
# PostgreSQL
# ----------
DB_HOST=/var/run/postgresql
DB_USER=mastodon
DB_NAME=mastodon_production
DB_PASS=[MY PASSWORD] <--- (I've also tried leaving this blank with no difference in the result)
DB_PORT=5432
但是当我跑步时(例如):
$ docker-compose run --rm web rails db:migrate
我得到以下输出:
rails aborted!
PG::ConnectionBad: could not connect to server: No such file or directory
Is the server running locally and accepting
connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?
和$ docker logs mastodon_db_1
给我:
Error: Database is uninitialized and superuser password is not specified.
You must specify POSTGRES_PASSWORD to a non-empty value for the
superuser. For example, "-e POSTGRES_PASSWORD=password" on "docker run".
You may also use "POSTGRES_HOST_AUTH_METHOD=trust" to allow all
connections without a password. This is *not* recommended.
See PostgreSQL documentation about "trust":
https://www.postgresql.org/docs/current/auth-trust.html
我对Docker相对较新,但是(我认为)我了解基本概念。但是,Googling的 很多 让我认为我是唯一遇到此问题的人,我看不到我不见了。
答案 0 :(得分:1)
第二个错误是由于缺少Postgres容器所需的ENV。
Error: Database is uninitialized and superuser password is not specified.
You must specify POSTGRES_PASSWORD to a non-empty value for the
superuser
POSTGRES_PASSWORD
此环境变量是使用PostgreSQL映像所必需的。不能为空或未定义。此环境变量设置PostgreSQL的超级用户密码。默认的超级用户是由
POSTGRES_USER
环境变量定义的
您需要指定POSTGRES_PASSWORD
ENV
db:
image: postgres
restart: always
environment:
POSTGRES_PASSWORD: example
POSTGRES_DB: mastodon_production
POSTGRES_USER: mastodon
postgres-docker-Environment Variables
第二个错误似乎是主机无效或您没有从Environment变量正确使用
。PG::ConnectionBad: could not connect to server: No such file or directory
将dot env文件更改为
DB_HOST=db
DB_USER=mastodon
DB_NAME=mastodon_production
DB_PASS=example <--- (the one which is set in DB)
DB_PORT=5432
DB_HOST应该是从另一个容器连接到DB的服务名称。