ara*_*lar 11 postgresql docker docker-compose
我正在尝试使用PostgreSQL的shell(psql
)docker-compose
,但是我遇到了一些困难...这是我的docker-compose
文件:
main:
build: .
volumes:
- .:/code
links:
- postgresdb
environment:
- DEBUG=true
postgresdb:
build: utils/sql/
ports:
- "5432"
environment:
- DEBUG=true
Run Code Online (Sandbox Code Playgroud)
我试图访问psql
通过该会main
还有postgresdb
服务,通过运行
docker-compose run postgresdb psql -h postgresdb -U docker mydatabase
但我得到的是psql: could not translate host name "postgresdb" to address: Name or service not known
......我不明白,因为我postgresdb
在数据库配置文件中用作主机,例如:
DB_ACCESS = {
'drivername': 'postgres',
# Name of docker-compose service
'host': 'postgresdb',
'port': '5432',
'username': 'docker',
'password': '',
'database': 'mydatabase'
}
Run Code Online (Sandbox Code Playgroud)
l3x*_*l3x 15
自从最初提出问题以来,情况发生了一些变化.
以下是使用docker-compose今天访问PostgreSQL的shell(psql)的方法:
泊坞窗,compoose.yml
version: '2'
services:
postgresdb:
build: utils/sql/
ports:
- "5432"
environment:
- DEBUG=true
main:
build: .
volumes:
- .:/code
depends_on:
- postgresdb
environment:
- DEBUG=true
Run Code Online (Sandbox Code Playgroud)
在您的docker引擎shell中......
docker ps
命令中查找容器ID(或容器名称)见下面的例子:
## .
## ## ## ==
## ## ## ## ## ===
/"""""""""""""""""\___/ ===
~~~ {~~ ~~~~ ~~~ ~~~~ ~~~ ~ / ===- ~~~
\______ o __/
\ \ __/
\____\_______/
_ _ ____ _ _
| |__ ___ ___ | |_|___ \ __| | ___ ___| | _____ _ __
| '_ \ / _ \ / _ \| __| __) / _` |/ _ \ / __| |/ / _ \ '__|
| |_) | (_) | (_) | |_ / __/ (_| | (_) | (__| < __/ |
|_.__/ \___/ \___/ \__|_____\__,_|\___/ \___|_|\_\___|_|
Boot2Docker version 1.11.2, build HEAD : a6645c3 - Wed Jun 1 22:59:51 UTC 2016
Docker version 1.11.2, build b9f10c9
docker@default:~$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b563764171e2 poc_web "bundle exec rails s " 43 minutes ago Up 43 minutes 0.0.0.0:3000->3000/tcp poc_web_1
c6e480b0f26a postgres "/docker-entrypoint.s" 49 minutes ago Up 49 minutes 0.0.0.0:5432->5432/tcp poc_db_1
docker@default:~$ docker exec -it c6e480b0f26a sh
# su - postgres
No directory, logging in with HOME=/
$ psql
psql (9.5.3)
Type "help" for help.
postgres=#
Run Code Online (Sandbox Code Playgroud)
笔记:
version: '2'
services:
db:
image: postgres
ports:
- "5432:5432"
. . .
参考
https://docs.docker.com/compose/compose-file/
让我们从一个最小的docker-compose.yml
文件开始,以供参考:
version: "3"
services:
postgres:
image: postgres:9.5
Run Code Online (Sandbox Code Playgroud)
您提供服务docker-compose up
.
假设您有一个要连接到的数据库test
.
回答: docker-compose run postgres psql -h YOUR_SERVICE -U YOUR_USER -d YOUR_DATABASE
在我们的例子中,这将是 docker-compose run postgres psql -h postgres -U postgres -d test
或者,您可以使用psql的连接字符串: docker compose run postgres psql -d postgres://YOUR_USER@YOUR_SERVICE/YOUR_DATABASE
您正在尝试从自身连接 Postgresdb 容器,但容器不知道postgresdb
您在main
容器内设置的别名。
考虑以下示例:
$ docker run -it --rm ubuntu hostname --fqdn
817450b29b34
Run Code Online (Sandbox Code Playgroud)
现在您看到 Postgresdb 不知道如何解析postgresdb
. 但无论如何postgresdb
应该可以解决main
。
您有几个机会来解决这个问题:
docker-compose.yml
);main
以这种方式从容器访问 Postgresdb :
$ docker exec -it main_container_full_name psql -h postgresdb -U docker mydatabase
Run Code Online (Sandbox Code Playgroud)以下对我有用:
在docker-compose.yaml中
services:
db:
container_name: db
image: postgres
Run Code Online (Sandbox Code Playgroud)
然后,运行后docker-compose up
我可以跑
sudo docker exec -it -u postgres db psql
postgres=#
Run Code Online (Sandbox Code Playgroud)
得到外壳
笔记:
docker ps
来获取container_id-u postgres
身份访问容器 归档时间: |
|
查看次数: |
9564 次 |
最近记录: |