postgresql Docker-compose 环境变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29580798/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 05:57:02  来源:igfitidea点击:

Docker-compose environment variables

postgresqldockerdocker-compose

提问by royalaid

I am trying to setup a postgres container and want to setup the postgres login with:

我正在尝试设置一个 postgres 容器并希望设置 postgres 登录:

POSTGRES_USER: docker
POSTGRES_PASSWORD: docker

So I have created the docker-compose.yml like so

所以我像这样创建了 docker-compose.yml

web:
  build: .
  ports:
    - "62576:62576"
  links:
   - redis
   - db
db:
  image: postgres
  environment:
    POSTGRES_PASSWORD: docker
    POSTGRES_USER: docker

redis:
   image: redis

I have also tried the other syntax for environment variable declaring the db section as:

我还尝试了环境变量的其他语法,将 db 部分声明为:

db:
  image: postgres
  environment:
   - POSTGRES_PASSWORD=docker
   - POSTGRES_USER=docker

However neither of these options seem to work because for whatever reason whenever I try to connect to the postgres database using the various connection strings:

然而,这些选项似乎都不起作用,因为无论出于何种原因,每当我尝试使用各种连接字符串连接到 postgres 数据库时:

postgres://postgres:postgres@db:5432/users
postgres://postgres:docker@db:5432/users
postgres://docker:docker@db:5432/users

They all give me auth failures as opposed to complaining there is no users database.

他们都给我验证失败,而不是抱怨没有用户数据库。

采纳答案by Greg

The authentication error you got would help a lot!

您遇到的身份验证错误会很有帮助!

I fired up the postgres image with your arguments:

我用你的论点启动了 postgres 图像:

docker run --name db -d -e POSTGRES_PASSWORD=docker -e POSTGRES_USER=docker postgres

Then I exec'ed in :

然后我执行了:

docker exec -it db psql -U docker user
psql: FATAL:  database "user" does not exist

I get the error message you are expecting because I have trust authentication :

我收到您期望的错误消息,因为我有信任身份验证:

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

To simulate your web container, I'll run another instance of the postgres container and link the db container and then connect back to the db container:

为了模拟您的 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"

I get an authentication error if I enter the incorrect password. But, if I enter the correct password:

如果我输入了错误的密码,我会收到身份验证错误。但是,如果我输入正确的密码:

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

It all seems to be working correctly. I put it all in a yaml file and tested it that way as well:

这一切似乎都在正常工作。我把它全部放在一个 yaml 文件中,并以这种方式进行了测试:

web:
  image: postgres
  command: sleep 999
  ports:
    - "62576:62576"
  links:
   - db
db:
  image: postgres
  environment:
    POSTGRES_PASSWORD: docker
    POSTGRES_USER: docker

then fired it up with docker-compose:

然后用 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

you can see that the user and password were created. I exec in:

您可以看到用户和密码已创建。我执行:

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

So the only thing I can think of is that you are trying to connect to the database from your host, not the web container? Or your web container is not using the 'db' as the host to connect to? Your definition for the web container does not contain any errors that I can see.

所以我唯一能想到的是你试图从你的主机连接到数据库,而不是 web 容器?或者您的 Web 容器没有使用“db”作为要连接的主机?您对 Web 容器的定义不包含我可以看到的任何错误。

回答by davetapley

I struggled with this for a while and wasn't having luck with the accepted answer, I finally got it to work by removing the container:

我为此苦苦挣扎了一段时间,但对接受的答案并不走运,我终于通过移除容器使其工作:

docker-compose rm postgres

And then the volumeas well:

然后是音量

docker volume rm myapp_postgres

Then when I did a fresh docker-compose upI saw CREATE ROLEfly by, which I'm assumingis what was missed on the initial up.

然后当我做一个新鲜的docker-compose up我看到CREATE ROLE飞过,我假设这是最初的up.



The reasons for this are elaborated on here, on the Git repo for the Docker official image for postgres.

其原因在此处详细说明,在 postgres 的 Docker 官方映像的 Git 存储库中。

回答by Alexey

I had the same problem, and in my case problem was fixed with a single command:

我遇到了同样的问题,在我的情况下,问题是用一个命令解决的:

docker-compose up --force-recreate

回答by Abdelsalam Megahed

If you're using Docker.

如果您使用的是Docker

Try to check if your local DB is active because mostly it's conflicting with Docker, if so, you can deactivate it or uninstall it in order to avoid the conflict.

尝试检查您的本地数据库是否处于活动状态,因为它主要与 Docker 冲突,如果是,您可以将其停用或卸载以避免冲突。

回答by Bryan

I had a similar situation. Following the answer from @Greg, I did a docker-compose up, and it picked up the environment variable.

我也有类似的情况。按照@Greg 的回答,我做了一个 docker -compose up,它选择了环境变量。

Prior to that, I had just been using docker-compose runand it wasn't picking up the environment variable as proven by running docker-compose exec task env. Strangely, docker-compose run task envshowed the environment variable I was expecting.

在此之前,我一直在使用docker-compose run并且它没有选择环境变量,正如运行docker-compose exec task env所证明的那样。奇怪的是,docker-compose run task env显示了我期望的环境变量。