使用 docker-compose 时无法连接到 postgres

Sno*_*ake 3 docker docker-compose pgadmin-4

我是 docker 新手,仍在学习如何使用它,我正在尝试使用 docker-compose 一起运行 Django 和 Postgres,它们运行完美,迁移完成等等,但我有一个问题,我无法连接到数据库使用pgAdmin4 查看数据库,这是我的 setting.py

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': 'slack',
        'USER': 'snowflake',
        'PASSWORD': '1Stepcloser',
        'HOST': 'db',
        'PORT': 5432,
    }
}
Run Code Online (Sandbox Code Playgroud)

那是我的 docker-compose.yml

version: '3'

services:
  db:
    image: postgres
    environment:
        POSTGRES_DB: slack
        POSTGRES_USER: snowflake
        POSTGRES_PASSWORD: 1Stepcloser
  web:
    build: .
    command: python3 manage.py runserver 0.0.0.0:8000
    volumes:
      - .:/slack_code
    ports:
      - "8000:8000"
    depends_on:
      - db
Run Code Online (Sandbox Code Playgroud)

一切正常似乎都很好:

sudo docker-compose up
slackwebapp_db_1 is up-to-date
Creating slackwebapp_web_1 ... done
Attaching to slackwebapp_db_1, slackwebapp_web_1
db_1   | The files belonging to this database system will be owned by user "postgres".
db_1   | This user must also own the server process.
db_1   | 
db_1   | The database cluster will be initialized with locale "en_US.utf8".
db_1   | The default database encoding has accordingly been set to "UTF8".
db_1   | The default text search configuration will be set to "english".
db_1   | 
db_1   | Data page checksums are disabled.
db_1   | 
db_1   | fixing permissions on existing directory /var/lib/postgresql/data ... ok
db_1   | creating subdirectories ... ok
db_1   | selecting default max_connections ... 100
db_1   | selecting default shared_buffers ... 128MB
db_1   | selecting dynamic shared memory implementation ... posix
db_1   | creating configuration files ... ok
db_1   | running bootstrap script ... ok
db_1   | performing post-bootstrap initialization ... ok
db_1   | syncing data to disk ... ok
db_1   | 
db_1   | Success. You can now start the database server using:
db_1   | 
db_1   |     pg_ctl -D /var/lib/postgresql/data -l logfile start
db_1   | 
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   | waiting for server to start....2018-01-18 19:46:43.851 UTC [38] LOG:  listening on IPv4 address "127.0.0.1", port 5432
db_1   | 2018-01-18 19:46:43.851 UTC [38] LOG:  could not bind IPv6 address "::1": Cannot assign requested address
db_1   | 2018-01-18 19:46:43.851 UTC [38] HINT:  Is another postmaster already running on port 5432? If not, wait a few seconds and retry.
db_1   | 2018-01-18 19:46:43.853 UTC [38] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
db_1   | 2018-01-18 19:46:43.864 UTC [39] LOG:  database system was shut down at 2018-01-18 19:46:43 UTC
db_1   | 2018-01-18 19:46:43.867 UTC [38] LOG:  database system is ready to accept connections
db_1   |  done
db_1   | server started
db_1   | CREATE DATABASE
db_1   | 
db_1   | CREATE ROLE
db_1   | 
db_1   | 
db_1   | /usr/local/bin/docker-entrypoint.sh: ignoring /docker-entrypoint-initdb.d/*
db_1   | 
db_1   | 2018-01-18 19:46:44.388 UTC [38] LOG:  received fast shutdown request
db_1   | waiting for server to shut down....2018-01-18 19:46:44.389 UTC [38] LOG:  aborting any active transactions
db_1   | 2018-01-18 19:46:44.390 UTC [38] LOG:  worker process: logical replication launcher (PID 45) exited with exit code 1
db_1   | 2018-01-18 19:46:44.391 UTC [40] LOG:  shutting down
db_1   | 2018-01-18 19:46:44.402 UTC [38] LOG:  database system is shut down
db_1   |  done
db_1   | server stopped
db_1   | 
db_1   | PostgreSQL init process complete; ready for start up.
db_1   | 
db_1   | 2018-01-18 19:46:44.501 UTC [1] LOG:  listening on IPv4 address "0.0.0.0", port 5432
db_1   | 2018-01-18 19:46:44.501 UTC [1] LOG:  listening on IPv6 address "::", port 5432
db_1   | 2018-01-18 19:46:44.502 UTC [1] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
db_1   | 2018-01-18 19:46:44.514 UTC [65] LOG:  database system was shut down at 2018-01-18 19:46:44 UTC
db_1   | 2018-01-18 19:46:44.518 UTC [1] LOG:  database system is ready to accept connections
web_1  | Performing system checks...
web_1  | 
web_1  | System check identified no issues (0 silenced).
web_1  | 
web_1  | You have 14 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.
web_1  | Run 'python manage.py migrate' to apply them.
web_1  | January 18, 2018 - 19:48:49
web_1  | Django version 2.0.1, using settings 'slack_webapp.settings'
web_1  | Starting development server at http://0.0.0.0:8000/
web_1  | Quit the server with CONTROL-C.
web_1  | [18/Jan/2018 19:56:03] "GET / HTTP/1.1" 200 16559
web_1  | [18/Jan/2018 19:56:03] "GET /static/admin/css/fonts.css HTTP/1.1" 200 423
web_1  | [18/Jan/2018 19:56:04] "GET /static/admin/fonts/Roboto-Bold-webfont.woff HTTP/1.1" 200 82564
web_1  | [18/Jan/2018 19:56:04] "GET /static/admin/fonts/Roboto-Regular-webfont.woff HTTP/1.1" 200 80304
web_1  | [18/Jan/2018 19:56:04] "GET /static/admin/fonts/Roboto-Light-webfont.woff HTTP/1.1" 200 81348
web_1  | [18/Jan/2018 19:56:08] "GET /admin/ HTTP/1.1" 302 0
web_1  | [18/Jan/2018 19:56:09] "GET /admin/login/?next=/admin/ HTTP/1.1" 200 1855
web_1  | [18/Jan/2018 19:56:09] "GET /static/admin/css/base.css HTTP/1.1" 200 16106
web_1  | [18/Jan/2018 19:56:09] "GET /static/admin/css/responsive.css HTTP/1.1" 200 17894
web_1  | [18/Jan/2018 19:56:09] "GET /static/admin/css/login.css HTTP/1.1" 200 1203
web_1  | [18/Jan/2018 19:58:58] "POST /admin/login/?next=/admin/ HTTP/1.1" 302 0
web_1  | [18/Jan/2018 19:58:58] "GET /admin/ HTTP/1.1" 200 2988
web_1  | [18/Jan/2018 19:58:58] "GET /static/admin/css/base.css HTTP/1.1" 304 0
web_1  | [18/Jan/2018 19:58:58] "GET /static/admin/css/dashboard.css HTTP/1.1" 200 412
web_1  | [18/Jan/2018 19:58:58] "GET /static/admin/css/responsive.css HTTP/1.1" 304 0
web_1  | [18/Jan/2018 19:58:58] "GET /static/admin/css/fonts.css HTTP/1.1" 304 0
web_1  | [18/Jan/2018 19:58:58] "GET /static/admin/fonts/Roboto-Bold-webfont.woff HTTP/1.1" 304 0
web_1  | [18/Jan/2018 19:58:58] "GET /static/admin/fonts/Roboto-Light-webfont.woff HTTP/1.1" 304 0
web_1  | [18/Jan/2018 19:58:58] "GET /static/admin/fonts/Roboto-Regular-webfont.woff HTTP/1.1" 304 0
web_1  | [18/Jan/2018 19:58:58] "GET /static/admin/img/icon-addlink.svg HTTP/1.1" 200 331
web_1  | [18/Jan/2018 19:58:58] "GET /static/admin/img/icon-changelink.svg HTTP/1.1" 200 380
web_1  | [18/Jan/2018 19:59:05] "GET /admin/ HTTP/1.1" 200 2988
web_1  | [18/Jan/2018 19:59:07] "GET /admin/ HTTP/1.1" 200 2988
web_1  | [18/Jan/2018 19:59:11] "GET /admin/ HTTP/1.1" 200 2988
^CGracefully stopping... (press Ctrl+C again to force)
Stopping slackwebapp_web_1 ... done
Stopping slackwebapp_db_1  ... done
Run Code Online (Sandbox Code Playgroud)

但我仍然无法连接,我不知道如何像我们一样为 Postgres 默认用户设置密码

sudo docker run --name test -e POSTGRES_PASSWORD=1Stepcloser -d postgres
Run Code Online (Sandbox Code Playgroud)

因为我猜我不能用 docker-compose 做同样的事情,提前致谢。

sid*_*eni 13

主机名应该是在 docker-compose.yml 中定义的服务名称

这是因为您在 docker 网络中

此处不能使用 localhost 或 127.0.0.1,因为 pgadmin 位于容器中,而此处的 localhost 表示“pgadmin 容器”。

让我们考虑一下你的情况:

version: '3'

services:
  db:
    image: postgres
    ports:
      - 5432:5432
    environment:
        POSTGRES_DB: slack
        POSTGRES_USER: snowflake
        POSTGRES_PASSWORD: 1Stepclose

  pgadmin:
    image: chorss/docker-pgadmin4
    ports:
      - 5050:5050
Run Code Online (Sandbox Code Playgroud)

在这种情况下,

主机名:db

端口:5432

用户:雪花

通过:1Stepclose

希望这可以帮助 :)


Dov*_*fen 3

为了从外部程序访问 postgres 数据库,您需要将 postgres 服务公开的端口 5432 挂载到主机上的端口。通过对 docker-compose.yml 文件进行以下更改,您应该能够使用 pgadmin (在 localhost:5432 上)连接到数据库,并让 postgres 为您创建用户。

db:
  image: postgres
  ports:
  - "5432:5432"
  environment:
    - POSTGRES_DB=slack
    - POSTGRES_USER=snowflake
    - POSTGRES_PASSWORD=1Stepclose
Run Code Online (Sandbox Code Playgroud)

编辑:我没有意识到您正在尝试连接在另一个 docker 容器中运行的 pgadmin4。设置它以允许 pgadmin4 容器与 postgres 容器通信的最简单方法是将 pgadmin 作为服务添加到 docker-compose.yml 文件中。更新 docker-compose.yml 文件以包含以下配置:

version: '3'

services:
  db:
    image: postgres
    ports:
      - 5432:5432
    environment:
        POSTGRES_DB: slack
        POSTGRES_USER: snowflake
        POSTGRES_PASSWORD: 1Stepclose

  pgadmin:
    image: chorss/docker-pgadmin4
    ports:
      - 5050:5050

  web:
    build: .
    command: python3 manage.py runserver 0.0.0.0:8000
    volumes:
      - .:/slack_code
    ports:
      - "8000:8000"
    depends_on:
      - db
Run Code Online (Sandbox Code Playgroud)

浏览到localhost:5050> 单击“添加新服务器”> 输入任意名称 > 单击“连接”选项卡 > 输入db主机名/地址 > 输入snowflake用户名 > 输入1Stepclose密码 > 单击“保存”