简体   繁体   English

Docker-compose 环境变量

[英]Docker-compose environment variables

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.他们都给我验证失败,而不是抱怨没有用户数据库。

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 volume as well:然后是音量

docker volume rm myapp_postgres

Then when I did a fresh docker-compose up I saw CREATE ROLE fly by, which I'm assuming is 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 存储库中。

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?所以我唯一能想到的是你试图从你的主机连接到数据库,而不是 web 容器? Or your web container is not using the 'db' as the host to connect to?或者您的 Web 容器没有使用“db”作为要连接的主机? Your definition for the web container does not contain any errors that I can see.您对 Web 容器的定义不包含我可以看到的任何错误。

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 change port number or uninstall it in order to avoid the conflict.尝试检查您的本地数据库是否处于活动状态,因为它主要与 Docker 冲突,如果是,您可以停用它或更改端口号或卸载它以避免冲突。

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

docker-compose up --force-recreate

这帮助了我

docker stop $(docker ps -qa) && docker system prune -af --volumes && docker compose up

Thanks to Bryan with the docker-compose exec containername env I have discovered that the need is also to delete volumes.感谢 Bryan 使用docker-compose exec containername env我发现还需要删除卷。 Since for the docker-compose volume rm volumename you need to know the exact name it is easier just to delete all with:由于对于docker-compose volume rm volumename您需要知道确切的名称,因此删除所有内容更容易:

docker-compose down --volumes

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 run and it wasn't picking up the environment variable as proven by running docker-compose exec task env .在此之前,我一直在使用docker-compose run并且它没有选择环境变量,正如运行docker-compose exec task env所证明的那样。 Strangely, docker-compose run task env showed the environment variable I was expecting.奇怪的是, docker-compose run task env显示了我期望的环境变量。

In my case, running postgres:13-alpine in a windows 10 WSL2 , none of the above solutions did the trick.在我的情况下,在 windows 10 WSL2运行postgres:13-alpine ,上述解决方案都没有成功。

My mistake was that the docker network name I was using was shared with another project.我的错误是我使用的 docker network名称与另一个项目共享。 Let's say I have projects A and B, both with the following structure:假设我有项目 A 和 B,都具有以下结构:

myappfolder
  - docker-compose.yml
    - services
        - app
            - depends on db
        - db

It happens that, by default, docker-compose takes the network name from the parent directory name of the docker-compose.yml file.碰巧,默认情况下, docker-composedocker-compose.yml文件的父目录名称中获取network名称。 Therefore both projects, A and B were trying to connect to the same network: myappfolder_default .因此,A 和 B 两个项目都试图连接到同一个网络: myappfolder_default

To solve this:要解决这个问题:

  1. ensure network names are unique among projects:确保网络名称在项目中是唯一的:

    a.一种。 either change the name of the root folder to be unique要么将根文件夹的名称更改为唯一

    b.or edit you docker-compose.yml to set an explicit network name或编辑您docker-compose.yml以设置显式网络名称

  2. do docker-compose down -v this will reset all the possible dbs you had defined in your network > make sure you make a psql dump before proceeding do docker-compose down -v这将重置您在网络中定义的所有可能的数据库 > 确保在继续之前进行 psql 转储

  3. do docker-compose updocker-compose up

More networking docs here: https://docs.docker.com/compose/networking/更多网络文档在这里: https : //docs.docker.com/compose/networking/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM