docker-compose:访问postgres的shell(psql)

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)的方法:

  1. 使用docker-compose.yml版本'2'配置文件
  2. 使用'depends_on'而不是'links'(版本'2'的新用户)
  3. 使用"服务"分解您的容器

泊坞窗,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中......

  1. docker ps命令中查找容器ID(或容器名称)
  2. 使用该id打开shell提示符
  3. 将用户切换到postgres用户帐户
  4. 运行psql

见下面的例子:

                        ##         .
                  ## ## ##        ==
               ## ## ## ## ##    ===
           /"""""""""""""""""\___/ ===
      ~~~ {~~ ~~~~ ~~~ ~~~~ ~~~ ~ /  ===- ~~~
           \______ 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)

笔记:

  1. docker-compose up将按依赖顺序启动服务
  2. 如果你想使用官方图像站立一个postgres容器,你的docker-compose.yml看起来可能更像如下:

version: '2' services:
db: image: postgres ports: - "5432:5432" . . .

参考

https://docs.docker.com/compose/compose-file/

  • 一年过去了,情况甚至更好:$ docker-compose exec -u postgres postgres psql (3认同)

Tat*_*ton 8

让我们从一个最小的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


Vit*_*aev 5

您正在尝试从自身连接 Postgresdb 容器,但容器不知道postgresdb您在main容器内设置的别名。

考虑以下示例:

$ docker run -it --rm ubuntu hostname --fqdn
817450b29b34
Run Code Online (Sandbox Code Playgroud)

现在您看到 Postgresdb 不知道如何解析postgresdb. 但无论如何postgresdb应该可以解决main

您有几个机会来解决这个问题:

  1. 尝试为 Postgresdb 显式设置主机名(在您的 中docker-compose.yml);
  2. main以这种方式从容器访问 Postgresdb :

    $ docker exec -it main_container_full_name psql -h postgresdb -U docker mydatabase
    
    Run Code Online (Sandbox Code Playgroud)


Dan*_*iel 5

以下对我有用:

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)

得到外壳

笔记:

  1. 我正在使用 docker-compose 版本 3
  2. 我已经给 psql 容器命名了(db)。这让我可以避免运行docker ps来获取container_id
  3. 允许我以postgres超级用户-u postgres身份访问容器