使用 docker-compose 启动 Postgres 会立即关闭

Posted

技术标签:

【中文标题】使用 docker-compose 启动 Postgres 会立即关闭【英文标题】:Postgres shuts down immediately when started with docker-compose 【发布时间】:2016-09-12 13:51:26 【问题描述】:

Postgres 在使用 docker-compose 启动时会立即关闭。使用的yaml文件如下

version: '2'

services:   
    postgres:
        image: postgres:9.5
        container_name: local-postgres9.5
        ports:
          - "5432:5432"

docker-compose up 命令执行时的日志

Creating local-postgres9.5
Attaching to local-postgres9.5
local-postgres9.5 | The files belonging to this database system will be owned by user "postgres".
local-postgres9.5 | This user must also own the server process.
local-postgres9.5 |
local-postgres9.5 | The database cluster will be initialized with locale "en_US.utf8".
local-postgres9.5 | The default database encoding has accordingly been set to "UTF8".
local-postgres9.5 | The default text search configuration will be set to "english".
local-postgres9.5 |
local-postgres9.5 | Data page checksums are disabled.
local-postgres9.5 |
local-postgres9.5 | fixing permissions on existing directory /var/lib/postgresql/data ... ok
local-postgres9.5 | creating subdirectories ... ok
local-postgres9.5 | selecting default max_connections ... 100
local-postgres9.5 | selecting default shared_buffers ... 128MB
local-postgres9.5 | selecting dynamic shared memory implementation ... posix
local-postgres9.5 | creating configuration files ... ok
local-postgres9.5 | creating template1 database in /var/lib/postgresql/data/base/1 ... ok
local-postgres9.5 | initializing pg_authid ... ok
local-postgres9.5 | initializing dependencies ... ok
local-postgres9.5 | creating system views ... ok
local-postgres9.5 | loading system objects' descriptions ... ok
local-postgres9.5 | creating collations ... ok
local-postgres9.5 | creating conversions ... ok
local-postgres9.5 | creating dictionaries ... ok
local-postgres9.5 | setting privileges on built-in objects ... ok
local-postgres9.5 | creating information schema ... ok
local-postgres9.5 | loading PL/pgSQL server-side language ... ok
local-postgres9.5 | vacuuming database template1 ... ok
local-postgres9.5 | copying template1 to template0 ... ok
local-postgres9.5 | copying template1 to postgres ... ok
local-postgres9.5 | syncing data to disk ... ok
local-postgres9.5 |
local-postgres9.5 | WARNING: enabling "trust" authentication for local connections
local-postgres9.5 | You can change this by editing pg_hba.conf or using the option -A, or
local-postgres9.5 | --auth-local and --auth-host, the next time you run initdb.
local-postgres9.5 |
local-postgres9.5 | Success. You can now start the database server using:
local-postgres9.5 |
local-postgres9.5 |     pg_ctl -D /var/lib/postgresql/data -l logfile start
local-postgres9.5 |
local-postgres9.5 | ****************************************************
local-postgres9.5 | WARNING: No password has been set for the database.
local-postgres9.5 |          This will allow anyone with access to the
local-postgres9.5 |          Postgres port to access your database. In
local-postgres9.5 |          Docker's default configuration, this is
local-postgres9.5 |          effectively any other container on the same
local-postgres9.5 |          system.
local-postgres9.5 |
local-postgres9.5 |          Use "-e POSTGRES_PASSWORD=password" to set
local-postgres9.5 |          it in "docker run".
local-postgres9.5 | ****************************************************
local-postgres9.5 | waiting for server to start....LOG:  database system was shut down at 2016-05-16 16:51:54 UTC
local-postgres9.5 | LOG:  MultiXact member wraparound protections are now enabled
local-postgres9.5 | LOG:  database system is ready to accept connections
local-postgres9.5 | LOG:  autovacuum launcher started
local-postgres9.5 |  done
local-postgres9.5 | server started
local-postgres9.5 | ALTER ROLE
local-postgres9.5 |
local-postgres9.5 |
local-postgres9.5 | /docker-entrypoint.sh: ignoring /docker-entrypoint-initdb.d/*
local-postgres9.5 |
local-postgres9.5 | LOG:  received fast shutdown request
local-postgres9.5 | LOG:  aborting any active transactions
local-postgres9.5 | LOG:  autovacuum launcher shutting down
local-postgres9.5 | LOG:  shutting down
local-postgres9.5 | waiting for server to shut down....LOG:  database system is shut down
local-postgres9.5 |  done
local-postgres9.5 | server stopped
local-postgres9.5 |
local-postgres9.5 | PostgreSQL init process complete; ready for start up.
local-postgres9.5 |
local-postgres9.5 | LOG:  database system was shut down at 2016-05-16 16:51:55 UTC
local-postgres9.5 | LOG:  MultiXact member wraparound protections are now enabled
local-postgres9.5 | LOG:  database system is ready to accept connections
local-postgres9.5 | LOG:  autovacuum launcher started

当容器使用与 docker run 相同的图像启动时,Postgres 似乎工作正常

docker run --name local-postgres9.5 -p 5432:5432 postgres:9.5

【问题讨论】:

如果您的问题解决了,请与我们分享! FWIW,看起来这是故意行为:github.com/docker-library/postgres/blob/… 您发布的日志中提到了解决方案 ----------------- local-postgres9.5 |使用“-e POSTGRES_PASSWORD=password”设置 我遇到了同样的问题,这在我尝试对数据库运行 liquibase 时有点痛苦。对我来说发生的事情是我的 liquibase 等待脚本看到数据库已启动并正在运行,因此它尝试开始运行,然后服务器像您提到的那样停止并启动,而它正在停止 liquibase 错误,因为它无法到达数据库。任何建议将不胜感激。 【参考方案1】:

我尝试了您的 docker-compose,服务似乎在容器中运行:

root@0afe99de0f0b:/# ps aux
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
postgres     1  0.5  0.8 227148 16128 ?        Ss   14:42   0:00 postgres
postgres    74  0.0  0.0 227148  1768 ?        Ss   14:42   0:00 postgres: checkpointer process  
postgres    75  0.0  0.0 227148  1772 ?        Ss   14:42   0:00 postgres: writer process  
postgres    76  0.0  0.0 227148  1768 ?        Ss   14:42   0:00 postgres: wal writer process  
postgres    77  0.0  0.1 227576  2720 ?        Ss   14:42   0:00 postgres: autovacuum launcher process  
postgres    78  0.0  0.0  82132  1888 ?        Ss   14:42   0:00 postgres: stats collector process  
root        79  2.0  0.0  21820  1984 ?        Ss   14:42   0:00 /bin/bash
root        84  0.0  0.0  19092  1296 ?        R+   14:42   0:00 ps aux

无论如何,对于我的项目,我为 postgresql 使用了另一个图像:https://github.com/sameersbn/docker-postgresql。这个很好用。

【讨论】:

谢谢。你是对的 postgres 实际上正在运行。我认为由于 docker-compose 并没有超越旋转 postgres 容器,所以这是一个问题。当我使用选项 -d 时,一切似乎都正常。 如果数据目录不存在,容器运行并重启是正确的。这是因为官方图像引导程序是如何编写脚本的。更多细节在这里:***.com/questions/65417340/…【参考方案2】:

如果您查看日志输出,则会在末尾出现以下几行:

local-postgres9.5 | server stopped
local-postgres9.5 |
local-postgres9.5 | PostgreSQL init process complete; ready for start up.

显然,停止和重新启动 Postgres 服务器是初始化过程的一部分。事实上,倒数第二行说

local-postgres9.5 | LOG:  database system is ready to accept connections.

【讨论】:

问题是它在启动、停止、启动时两次显示“数据库系统已准备好接受连接”。【参考方案3】:

在 docker-compose.yml 中的 postgres 服务下添加密码,如屏幕截图所示。谢谢你。 click to see the screenshot

version: '3'
services:
     postgres:
          image: postgres
          environment:
              - POSTGRES_PASSWORD=postgres_password

【讨论】:

请在您的答案中包含任何代码作为文本,这样人们可以轻松地复制和尝试。此外,这使其对搜索引擎可见,因此可以在第一时间找到。

以上是关于使用 docker-compose 启动 Postgres 会立即关闭的主要内容,如果未能解决你的问题,请参考以下文章

使用 docker-compose 启动 Postgres 会立即关闭

禁用 docker-compose 项目的自动启动

docker-compose 启动容器

docker-compose快速启动nginx

docker-compose启动容器失败了怎么办

docker-compose启动并使用MongoDB