首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在Ubuntu Docker映像旁边启动Postgresql数据库容器?

如何在Ubuntu Docker映像旁边启动Postgresql数据库容器?
EN

Stack Overflow用户
提问于 2022-09-23 02:31:28
回答 1查看 210关注 0票数 0

如何在一个容器中启动PostgreSQL,并允许Ubuntu 22容器访问它?

我的docker-compose.yml看起来像:

代码语言:javascript
运行
复制
version: "3.6"
services:
  db:
    image: postgres:14-alpine
    environment:
      - POSTGRES_USER=test
      - POSTGRES_PASSWORD=test
      - POSTGRES_DB=test
    command: -c fsync=off -c synchronous_commit=off -c full_page_writes=off --max-connections=200 --shared-buffers=4GB --work-mem=20MB
    tmpfs:
      - /var/lib/postgresql
  app_test:
    build:
      context: ..
      dockerfile: Dockerfile
      shm_size: '2gb'
    volumes:
      - /dev/shm:/dev/shm

我的Dockerfile刚刚运行了一个Django单元,它使用相同的凭据连接到PostgreSQL数据库。然而,它看起来像数据库周期性崩溃或停止和启动,中断了与测试的连接。

当我跑步时:

代码语言:javascript
运行
复制
docker-compose -f docker-compose.yml -p myproject up --build --exit-code-from myproject_app

我得到的输出如下:

代码语言:javascript
运行
复制
Successfully built 45c74650b75f
Successfully tagged myproject_app:latest
Creating myproject_app_test_1 ... done
Creating myproject_db_1       ... done
Attaching to myproject_app_test_1, myproject_db_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 dynamic shared memory implementation ... posix
db_1        | selecting default max_connections ... 100
db_1        | selecting default shared_buffers ... 128MB
db_1        | selecting default time zone ... UTC
db_1        | creating configuration files ... ok
app_test_1  | SITE not set. Defaulting to myproject.
app_test_1  | Initialized settings for site "myproject".
db_1        | running bootstrap script ... ok
db_1        | performing post-bootstrap initialization ... sh: locale: not found
db_1        | 2022-09-23 02:06:54.175 UTC [30] WARNING:  no usable system locales were found
db_1        | ok
db_1        | syncing data to disk ... initdb: 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        | ok
db_1        | 
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        | waiting for server to start....2022-09-23 02:06:56.736 UTC [36] LOG:  starting PostgreSQL 14.4 on x86_64-pc-linux-musl, compiled by gcc (Alpine 11.2.1_git20220219) 11.2.1 20220219, 64-bit
db_1        | 2022-09-23 02:06:56.737 UTC [36] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
db_1        | 2022-09-23 02:06:56.743 UTC [37] LOG:  database system was shut down at 2022-09-23 02:06:56 UTC
db_1        | 2022-09-23 02:06:56.750 UTC [36] LOG:  database system is ready to accept connections
db_1        |  done
db_1        | server started
db_1        | CREATE DATABASE
db_1        | 
db_1        | 
db_1        | /usr/local/bin/docker-entrypoint.sh: ignoring /docker-entrypoint-initdb.d/*
db_1        | 
db_1        | 2022-09-23 02:06:57.100 UTC [36] LOG:  received fast shutdown request
db_1        | 2022-09-23 02:06:57.101 UTC [36] LOG:  aborting any active transactions
db_1        | 2022-09-23 02:06:57.104 UTC [36] LOG:  background worker "logical replication launcher" (PID 43) exited with exit code 1
db_1        | 2022-09-23 02:06:57.105 UTC [38] LOG:  shutting down
db_1        | waiting for server to shut down....2022-09-23 02:06:57.222 UTC [36] 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        | 2022-09-23 02:06:57.566 UTC [1] LOG:  starting PostgreSQL 14.4 on x86_64-pc-linux-musl, compiled by gcc (Alpine 11.2.1_git20220219) 11.2.1 20220219, 64-bit
db_1        | 2022-09-23 02:06:57.568 UTC [1] LOG:  listening on IPv4 address "0.0.0.0", port 5432
db_1        | 2022-09-23 02:06:57.569 UTC [1] LOG:  listening on IPv6 address "::", port 5432
db_1        | 2022-09-23 02:06:57.571 UTC [1] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
db_1        | 2022-09-23 02:06:57.576 UTC [50] LOG:  database system was shut down at 2022-09-23 02:06:57 UTC
db_1        | 2022-09-23 02:06:57.583 UTC [1] LOG:  database system is ready to accept connections
db_1        | 2022-09-23 02:06:58.805 UTC [57] ERROR:  relation "django_site" does not exist at character 78
db_1        | 2022-09-23 02:06:58.805 UTC [57] STATEMENT:  SELECT "django_site"."id", "django_site"."domain", "django_site"."name" FROM "django_site" WHERE "django_site"."id" = 2 LIMIT 21

为什么它要读取shutting downdatabase system was shut down,这意味着数据库会多次重新启动?为什么Django无法访问它来初始化模式?

EN

回答 1

Stack Overflow用户

发布于 2022-09-23 03:53:16

当您在中使用数据库时,您总是需要等待它们完全启动。要么您的程序需要ping和等待(而不是在第一次连接到数据库的尝试失败之后崩溃,而数据库可能还在启动),或者您可以使用著名的“现在等待-it.sh”脚本。

下面是第二种方法的例子。

Dockerfile:

代码语言:javascript
运行
复制
FROM debian:stable

WORKDIR /scripts

RUN apt-get update && apt-get install -y curl telnet

# there are many versions on the internet, I just picked one
RUN curl -sO https://raw.githubusercontent.com/vishnubob/wait-for-it/master/wait-for-it.sh && chmod a+x *.sh

ENTRYPOINT ["/scripts/wait-for-it.sh"]

它只准备带有等待-it.sh脚本和telnet的映像(用于测试数据库连接)。

docker-compose.yml

代码语言:javascript
运行
复制
version: "3.6"

services:
  db:
    image: postgres:14-alpine
    environment:
      - POSTGRES_USER=test
      - POSTGRES_PASSWORD=test
      - POSTGRES_DB=test
    command: -c fsync=off -c synchronous_commit=off -c full_page_writes=off --max-connections=200 --shared-buffers=4GB --work-mem=20MB
    tmpfs:
      - /var/lib/postgresql

  test:
    build:
      context: .
    command: db:5432 -t 3000 -- telnet db 5432

test服务将在启动其主进程之前等待数据库可用。

测试它的最佳方法是:

在一个终端开始:

代码语言:javascript
运行
复制
docker-compose up test

在第二个终端中:

代码语言:javascript
运行
复制
# make the operation even longer
docker rmi postgres:14-alpine

# start database
docker-compose up db

这些注释很好地解释了数据库在启动期间重新启动的原因。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73822430

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档