[英]How to launch a Postgresql database container alongside an Ubuntu Docker image?
如何使用 docker-compose 在一個容器中啟動 PostgreSQL,並允許 Ubuntu 22 容器訪問它?
我的docker-compose.yml
看起來像:
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 數據庫。 但是,看起來數據庫會定期崩潰或停止和啟動,從而中斷與測試的連接。
當我運行時:
docker-compose -f docker-compose.yml -p myproject up --build --exit-code-from myproject_app
我得到 output 喜歡:
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 down
和database system was shut down
,意味着數據庫重新啟動了幾次? 為什么 Django 無法訪問它來初始化架構?
在 docker-compose 中使用數據庫時,您總是需要等待它們完全啟動。 您的程序需要 ping 並等待(在第一次嘗試連接可能仍在啟動的數據庫失敗后不會崩潰),或者您可以使用著名的 now wait-for-it.sh 腳本。
下面是第二種方法的示例。
Dockerfile:
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"]
它只准備一個帶有 wait-for-it.sh 腳本和 telnet 的圖像(以測試數據庫連接)
docker-compose.yml
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
服務將等待數據庫可用,然后再啟動它的主進程。
最好的測試方法:
在一個終端開始:
docker-compose up test
在第二個終端:
# make the operation even longer
docker rmi postgres:14-alpine
# start database
docker-compose up db
數據庫在啟動過程中重啟的原因在評論中有很好的解釋。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.