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

标签 docker docker-compose pgadmin-4

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

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': 'slack',
        'USER': 'snowflake',
        'PASSWORD': '1Stepcloser',
        'HOST': 'db',
        'PORT': 5432,
    }
}

那是我的 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

一切正常似乎都很好:
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

但我仍然无法连接,我不知道如何像我们一样为 Postgres 默认用户设置密码
sudo docker run --name test -e POSTGRES_PASSWORD=1Stepcloser -d postgres

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

最佳答案

主机名应该是在 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

在这种情况下,

主机名:db

端口:5432

用户:雪花

通过:1Stepclose

希望这可以帮助 :)

关于docker - 使用 docker-compose 时无法连接到 postgres,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48329899/

相关文章:

docker - 在docker-compose构建之后,docker-compose up运行旧的未更新的容器

postgresql - 无法使用 pgadmin4 创建新数据库

database - PGAdmin:未连接到服务器或与服务器的连接已关闭

docker - 使用 docker swarm 从私有(private)注册表运行容器

azure - 无法使用 docker-compose 文件将 docker 镜像推送到 Azure 注册表容器

docker - 构建docker-compose时如何使用网络?

haskell - 如何将 Haskell Stack 与 Docker Compose 结合使用?

docker - docker-compose up后清空卷文件夹

sql - 有没有办法将文本数据加载到 PostgreSQL 数据库中?

php - 使用 Docker 将 Elastic Beanstalk 环境变量公开给 Laravel 的 artisan