Docker exec

编程入门 行业动态 更新时间:2024-10-27 10:19:32
本文介绍了Docker exec-无法使用环境变量调用postgres的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我在Postgres容器上定义了多个环境变量,例如POSTGRES_USER.容器正在运行,我想使用exec从命令行连接到Postgres.

I have multiple Environment Variables defined on my Postgres container, such as POSTGRES_USER. The container is running and I want to connect to Postgres from the command line using exec.

我无法连接以下内容:

docker exec -it <CONTAINER-ID> psql -U $POSTGRES_USER -d <DB NAME>

我了解该变量是在容器上定义的,并且确实可以执行以下操作:

I understand that the variable is defined on the container and the following does work:

docker exec -it <CONTAINER-ID> bash -c 'psql -U $POSTGRES_USER -d <DB NAME>'

我是否可以直接从docker exec执行psql命令并在容器上调用环境变量?

Is there a way for me to execute the psql command directly from docker exec and call the environment variable on the container?

docker exec -it <CONTAINER-ID> psql -U ????? -d <DB NAME>

推荐答案

根据您的用例,您可以做什么,而不是将用户传递给 psql 命令,而是定义环境变量 PGUSER 到容器

Depending on your use case, what you could do, instead of passing a user to the psql command is to define the environment variable PGUSER to the container at boot time.

这样,如果您不指定任何用户,它将成为PostgreSQL的默认用户,因此您甚至不必指定它即可进行连接:

This way, it will be the default user for PostgreSQL, if you do not specify any, so you won't even have to specify it in order to connect:

$ docker run --name postgres -e POSTGRES_PASSWORD=bar -e POSTGRES_USER=foo -e PGUSER=foo -d postgres e250f0821613a5e2021e94772a732f299874fc7a16b340ada4233afe73744423 $ docker exec -ti postgres psql -d postgres psql (12.4 (Debian 12.4-1.pgdg100+1)) Type "help" for help. postgres=#

更多推荐

Docker exec

本文发布于:2023-10-22 23:20:41,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1519027.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Docker   exec

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!