Docker编写环境变量 [英] Docker-compose environment variables

查看:121
本文介绍了Docker编写环境变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试设置一个postgres容器,并且想要设置postgres登录名:

  POSTGRES_USER:docker 
POSTGRES_PASSWORD:docker

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

  web:
build:。
ports:
- 62576:62576
links:
- redis
- db
db:
image:postgres
环境:
POSTGRES_PASSWORD:docker
POSTGRES_USER:docker

redis:
image:redis

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

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

但是,由于无论何种原因,当我尝试使用各种连接字符串连接到postgres数据库时,这两个选项似乎都不起作用:

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

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

解决方案

您所得到的身份验证错误将会有所帮助!



我用你的论点解雇了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:数据库user不存在
/ pre>

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

 code> docker exec -it db cat /var/lib/postgresql/data/pg_hba.conf | grep -v'^#'

本地全部信任
主机全部127.0.0.1/32信任
主机全部:: 1/128信任
主机all all 0.0.0.0/0 md5

要模拟您的Web容器,我将运行另一个postgres容器并链接db容器,然后连接到db容器:

  core @ ku1 / tmp / i $ docker运行--rm --name web --link db:db -it postgres psql -h db -Udocker user 
用户docker的密码:
psql:FATAL:用户docker的密码验证失败

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

  core @ ku1 / tmp / i $ docker run --rm  - --link db:db -it postgres psql -h db -Udocker user 
用户docker的密码:
psql:FATAL:数据库user不存在

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

  web:
image:postgres
命令:sleep 999
ports:
- 62576:62576
links:
- db
db:
image:postgres
环境:
POSTGRES_PASSWORD:docker
POSTGRES_USER:docker

然后触发它使用docker-compose:

  core @ ku1 / tmp / i $ docker-compose -f dc.yaml up 
创建i_db_1 ...
创建i_web_1 ...
附加到i_db_1,i_web_1
db_1 | ok
db_1 |在/ var / lib / postgresql / data / base / 1中创建template1数据库... ok
db_1 |初始化pg_authid ... ok
db_1 |初始化依赖关系... ok
db_1 |创建系统视图... ok
db_1 |加载系统对象的描述... ok
db_1 |创建归类... ok
db_1 |创建转换... ok
db_1 |创建字典... ok
db_1 |对内置对象设置权限... ok
db_1 |创建信息模式... ok
db_1 |加载PL / pgSQL服务器端语言... ok
db_1 |抽真空数据库template1 ... ok
db_1 |将template1复制到template0 ... ok
db_1 |将template1复制到postgres ... ok
db_1 |将数据同步到磁盘... ok
db_1 |
db_1 |警告:为本地连接启用信任身份验证
db_1 |您可以通过编辑pg_hba.conf或使用选项-A或
db_1 |来更改此选项--auth-local和--auth-host,下次运行initdb时。
db_1 |
db_1 |成功。现在可以使用以下命令启动数据库服务器:
db_1 |
db_1 | postgres -D / var / lib / postgresql / data
db_1 |或
db_1 | pg_ctl -D / var / lib / postgresql / data -l logfile start
db_1 |
db_1 |
db_1 | PostgreSQL独立后端9.4.1
db_1 |后端>声明:CREATE DATABASEdocker;
db_1 |
db_1 |后端>
db_1 |
db_1 | PostgreSQL独立后端9.4.1
db_1 |后端>声明:CREATE USERdockerWITH SUPERUSER PASSWORD'docker';
db_1 |
db_1 |后端>
db_1 |日志:数据库系统在2015-04-12 22:01:12 UTC
db_1 |关闭LOG:数据库系统已准备好接受连接
db_1 |日志:autovacuum启动器启动
^ Z
[1] +停止的docker-compose -f dc.yaml up
core @ ku1 / tmp / i $ bg

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

  core @ ku1 / tmp / i $ docker exec -it i_web_1 psql -Udocker -h db user 
用户docker的密码:
psql:FATAL:用户docker的密码认证失败
core @ ku1 / tmp / i $
db_1 | FATAL:用户docker的密码认证失败
db_1 |详细信息:连接匹配pg_hba.conf行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
用户docker的密码:
psql:FATAL:数据库user不存在
db_1 | FATAL:数据库user不存在

所以唯一可以想到的是你是尝试从主机连接到数据库,而不是Web容器?或者您的网络容器不使用'db'作为连接主机?您对Web容器的定义不包含任何可以看到的错误。



-g


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

POSTGRES_USER: docker
POSTGRES_PASSWORD: docker

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

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:
  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@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.

解决方案

the authentication error you got would help a lot!

i fired up the postgres image with your arguments:

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:

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:

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:

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.

-g

这篇关于Docker编写环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆