Docker-compose环境variables
我想设置一个postgres容器,并想要设置postgreslogin:
POSTGRES_USER: docker POSTGRES_PASSWORD: docker
所以我创build了docker-compose.yml
web: build: . ports: - "62576:62576" links: - redis - db db: image: postgres environment: POSTGRES_PASSWORD: docker POSTGRES_USER: docker redis: image: redis
我也尝试了其他的声明db部分的环境variables的语法:
db: image: postgres environment: - POSTGRES_PASSWORD=docker - POSTGRES_USER=docker
但是,这些选项似乎都不起作用,因为无论什么原因,当我尝试连接到使用各种连接stringpostgres数据库:
postgres://postgres:postgres@db:5432/users postgres://postgres:docker@db:5432/users postgres://docker:docker@db:5432/users
他们都给我auth失败,而不是抱怨没有用户数据库。
authentication错误你会得到很多帮助!
我用你的论据激发了postgres的形象:
docker run --name db -d -e POSTGRES_PASSWORD=docker -e POSTGRES_USER=docker postgres
然后我执行:
docker exec -it db psql -U docker user psql: FATAL: database "user" does not exist
我收到您所期望的错误消息,因为我有信任validation:
docker exec -it db cat /var/lib/postgresql/data/pg_hba.conf | grep -v '^#' local all all trust host all all 127.0.0.1/32 trust host all all ::1/128 trust host all all 0.0.0.0/0 md5
为了模拟你的Web容器,我将运行postgres容器的另一个实例并链接db容器,然后连接回db容器:
core@ku1 /tmp/i $ docker run --rm --name web --link db:db -it postgres psql -h db -Udocker user Password for user docker: psql: FATAL: password authentication failed for user "docker"
如果input不正确的密码,我会收到validation错误。 但是,如果我input正确的密码:
core@ku1 /tmp/i $ docker run --rm --name web --link db:db -it postgres psql -h db -Udocker user Password for user docker: psql: FATAL: database "user" does not exist
这一切似乎工作正常。 我把它全部放在一个yaml文件中,并用这种方式进行testing:
web: image: postgres command: sleep 999 ports: - "62576:62576" links: - db db: image: postgres environment: POSTGRES_PASSWORD: docker POSTGRES_USER: docker
然后用docker-compose将它解雇:
core@ku1 /tmp/i $ docker-compose -f dc.yaml up Creating i_db_1... Creating i_web_1... Attaching to i_db_1, i_web_1 db_1 | ok db_1 | creating template1 database in /var/lib/postgresql/data/base/1 ... ok db_1 | initializing pg_authid ... ok db_1 | initializing dependencies ... ok db_1 | creating system views ... ok db_1 | loading system objects' descriptions ... ok db_1 | creating collations ... ok db_1 | creating conversions ... ok db_1 | creating dictionaries ... ok db_1 | setting privileges on built-in objects ... ok db_1 | creating information schema ... ok db_1 | loading PL/pgSQL server-side language ... ok db_1 | vacuuming database template1 ... ok db_1 | copying template1 to template0 ... ok db_1 | copying template1 to postgres ... ok db_1 | syncing data to disk ... ok db_1 | db_1 | WARNING: enabling "trust" authentication for local connections db_1 | You can change this by editing pg_hba.conf or using the option -A, or db_1 | --auth-local and --auth-host, the next time you run initdb. db_1 | db_1 | Success. You can now start the database server using: db_1 | db_1 | postgres -D /var/lib/postgresql/data db_1 | or db_1 | pg_ctl -D /var/lib/postgresql/data -l logfile start db_1 | db_1 | db_1 | PostgreSQL stand-alone backend 9.4.1 db_1 | backend> statement: CREATE DATABASE "docker" ; db_1 | db_1 | backend> db_1 | db_1 | PostgreSQL stand-alone backend 9.4.1 db_1 | backend> statement: CREATE USER "docker" WITH SUPERUSER PASSWORD 'docker' ; db_1 | db_1 | backend> db_1 | LOG: database system was shut down at 2015-04-12 22:01:12 UTC db_1 | LOG: database system is ready to accept connections db_1 | LOG: autovacuum launcher started ^Z [1]+ Stopped docker-compose -f dc.yaml up core@ku1 /tmp/i $ bg
您可以看到用户和密码已创build。 我执行:
core@ku1 /tmp/i $ docker exec -it i_web_1 psql -Udocker -h db user Password for user docker: psql: FATAL: password authentication failed for user "docker" core@ku1 /tmp/i $ db_1 | FATAL: password authentication failed for user "docker" db_1 | DETAIL: Connection matched pg_hba.conf line 95: "host all all 0.0.0.0/0 md5" core@ku1 /tmp/i $ docker exec -it i_web_1 psql -Udocker -h db user Password for user docker: psql: FATAL: database "user" does not exist db_1 | FATAL: database "user" does not exist
所以我唯一能想到的是,你正试图从你的主机连接到数据库,而不是Web容器? 或者您的Web容器不使用'db'作为主机连接? 您对Web容器的定义不包含任何可以看到的错误。
我为此挣扎了一段时间,并没有接受答案的运气,我终于通过删除容器来工作:
docker-compose rm postgres
然后是音量 :
docker volume rm myapp_postgres
然后,当我做了一个新的docker-compose up
我看到CREATE ROLE
飞过,我假设是最初错过了什么。
我也有类似的情况。 在@Greg的回答之后,我做了一个docker -up ,并且find了环境variables。
在此之前,我刚刚在使用docker-compose运行 ,而且没有运行docker-compose exec task envcertificate的环境variables。 奇怪的是, docker-compose运行任务env显示了我期待的环境variables。
我有同样的问题,在我的情况下,问题是用一个命令修复的:
docker-compose up --force-recreate