postgresql - 无法启动用 docker-compose 编写的 kong 服务

标签 postgresql docker docker-compose kong

我有以下 docker-compose.yml 文件来运行 kongpostgresql。我在那里也有我现在删除的迁移。想知道为什么它会抛出与 postgresql 的连接问题。

  • postgresql 容器已启动并正在运行。
  • 问题出在香港

这是我的 docker-compose.yml:

version: '3.2'
networks:
  kong-net:

services:

  kong-database:
    image: postgres:9.5
    container_name: kong-database
    networks:
      - kong-net
    ports:
      - 5435:5435
    environment:
      - POSTGRES_DB=kong
      - POSTGRES_USER=kong
      - POSTGRES_PASSWORD=password

  kong:
    image: kong:latest
    restart: always
    networks:
      - kong-net
    depends_on:
      - kong-database
    ports:
      - 8000:8000
      - 8443:8443
      - 8009:8009
      - 7947:7947
      - 7947:7947/udp
    environment:
      - KONG_DATABASE=postgres
      - KONG_PG_HOST=kong-database
      - KONG_PG_PASSWORD=password
      - KONG_ADMIN_LISTEN=0.0.0.0:8009

I am changing the ports for kong since some other service is already running in the port 8001

这是来自 docker-compose logs 的错误日志

日志:

Attaching to kong_kong_1, kong-database
kong-database    | The files belonging to this database system will be owned by user "postgres".
kong-database    | This user must also own the server process.
kong-database    | 
kong-database    | The database cluster will be initialized with locale "en_US.utf8".
kong-database    | The default database encoding has accordingly been set to "UTF8".
kong-database    | The default text search configuration will be set to "english".
kong-database    | 
kong-database    | Data page checksums are disabled.
kong-database    | 
kong-database    | fixing permissions on existing directory /var/lib/postgresql/data ... ok
kong-database    | creating subdirectories ... ok
kong-database    | selecting default max_connections ... 100
kong-database    | selecting default shared_buffers ... 128MB
kong-database    | selecting dynamic shared memory implementation ... posix
kong-database    | creating configuration files ... ok
kong-database    | creating template1 database in /var/lib/postgresql/data/base/1 ... ok
kong-database    | initializing pg_authid ... ok
kong-database    | initializing dependencies ... ok
kong-database    | creating system views ... ok
kong-database    | loading system objects' descriptions ... ok
kong-database    | creating collations ... ok
kong-database    | creating conversions ... ok
kong-database    | creating dictionaries ... ok
kong-database    | setting privileges on built-in objects ... ok
kong-database    | creating information schema ... ok
kong-database    | loading PL/pgSQL server-side language ... ok
kong-database    | vacuuming database template1 ... ok
kong-database    | copying template1 to template0 ... ok
kong-database    | copying template1 to postgres ... ok
kong_1           | prefix directory /usr/local/kong not found, trying to create it
kong_1           | nginx: [error] init_by_lua error: /usr/local/share/lua/5.1/kong/init.lua:169: [postgres error] could not retrieve server_version: connection refused
kong_1           | stack traceback:
kong_1           |  [C]: in function 'error'
kong_1           |  /usr/local/share/lua/5.1/kong/init.lua:169: in function 'init'
kong_1           |  init_by_lua:3: in main chunk
kong_1           | nginx: [error] init_by_lua error: /usr/local/share/lua/5.1/kong/init.lua:169: [postgres error] could not retrieve server_version: connection refused
kong_1           | stack traceback:
kong_1           |  [C]: in function 'error'
kong_1           |  /usr/local/share/lua/5.1/kong/init.lua:169: in function 'init'
kong_1           |  init_by_lua:3: in main chunk

最佳答案

您的第一个问题是 kong 容器在 kong-database 容器完成其初始化过程之前启动。这是“连接被拒绝”错误的来源。您可以通过首先启动数据库来验证这一点:

docker-compose up kong-database

然后等到你看到:

PostgreSQL init process complete; ready for start up.

然后在另一个窗口中,启动kong容器:

docker-compose up kong

你会看到一个新的错误:

kong_1           | nginx: [warn] [lua] log.lua:63: log(): postgres
  database 'kong' is missing migration: (response-transformer)
  2016-05-04-160000_resp_trans_schema_changes
kong_1           | nginx: [error] init_by_lua error:
  /usr/local/share/lua/5.1/kong/init.lua:172: [postgres error] the
  current database schema does not match this version of Kong.
  Please run `kong migrations up` to update/initialize the database
  schema. Be aware that Kong migrations should only run from a
  single node, and that nodes running migrations concurrently will
  conflict with each other and might corrupt your database schema!

这表明 kong 已成功连接到 postgres,但是 看起来你需要运行某种数据库模式 在它工作之前进行初始化。

这种启动依赖的典型解决方案是实现 数据库客户端启动时的等待循环,循环直到 数据库可用。像这样的东西:

while ! psql -h kong-database -U kong -c 'select 1'; do
  sleep 1
done

关于postgresql - 无法启动用 docker-compose 编写的 kong 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50552241/

相关文章:

docker - 如何完全重建Docker容器

docker - 限制docker-compose的日志数量

postgresql - SQLAlchemy SELECT WITH 子句/语句 (pgsql)

postgresql - pg_restore 到在 docker 容器中运行的 postgres

powershell - Windows Server 2016 Dockerfile安装服务

apache - 如何正确配置具有自动续订的 certbot?

docker - 有没有一种方法可以指定使用撰写文件运行服务的持续时间

python - 如何按用户 ID 按小时迭代选择平均值?

php - 如何在 Postgresql 中保存/读取 mPDF 生成的 pdf

docker - 使用 Docker 设置多个 MQTT 代理