Postgres Docker容器在端口映射后仍然监听5432端口。

huangapple go评论117阅读模式
英文:

Postgres docker container still listening at 5432 after port mapping

问题

背景:我正在尝试配置一个本地开发实例,其中包含两个 Docker PostgreSQL 容器:一个用于开发,另一个用于运行 pytest 集成测试。该应用程序使用 fastapi 和 sqlalchemy 与 asyncpg 构建。

我遇到了几个问题:第一个问题是运行健康检查时出现问题,第二个问题是我已经指示 postgres 容器监听端口 5433 而不是端口 5432,但是 docker ps 仍然显示容器在端口 5432 上有一个活动端口。我认为这导致了尝试连接到此测试容器时出现问题。请注意,根据健康检查确定的不健康状态如下所示:

Postgres Docker容器在端口映射后仍然监听5432端口。

请注意上面的健康检查测试行:当包含 -u inclasstoday 标志时,会导致上面的不健康状态。然而,如果我从我的本地机器连接到 Docker 容器(使用 docker-exec it test-db sh,然后运行相同的命令 pg_isready -p 5433 -u inclasstoday),我会得到以下错误:

Postgres Docker容器在端口映射后仍然监听5432端口。

/ # pg_isready -p 5433
/var/run/postgresql:5433 - accepting connections
/ # pg_isready -p 5433 -u inclasstoday
/usr/local/bin/pg_isready: unrecognized option: u
pg_isready: hint: Try "pg_isready --help" for more information.

然而,仅使用 pg_isready -p 5433 进行连接是可以的。当我尝试编辑我的 docker-compose.yml 文件,只使用此命令作为健康检查时,我会得到以下错误:
test-db | 2023-08-08 19:30:47.520 UTC [196] FATAL: role "root" does not exist
还请注意,此数据库日志输出是由我从本地机器在 Docker 容器内执行的上述 pg_isready -p 5433 命令引起的。

以下是我的 docker-compose.yml 文件和日志转储:

Docker-compose.yml

version: "3.8"
services:
  backend:
    build: ./backend
    environment:
      - POSTGRES_USER=fastapi_traefik
      - POSTGRES_PASSWORD=password
      - POSTGRES_DB=fastapi_traefik
      - APP_CONFIG_FILE=local
    command: bash -c 'while !</dev/tcp/db/5432; do sleep 1; done; cd backend; echo "RUNNING ALEMBIC MIGRATIONS"; alembic upgrade head; cd ..; uvicorn backend.main:app --host 0.0.0.0 --port 80 --reload'
    ports:
      - 80:80
    volumes:
      - .:/app
    depends_on:
      db:
        condition: service_healthy
    secrets:
      - db_password
    networks:
      - dock-db-test

  db:
    image: postgres:15-alpine
    container_name: dev-db
    volumes:
      - postgres_data:/var/lib/postgresql/data/
    environment:
      - POSTGRES_USER=fastapi_traefik
      - POSTGRES_PASSWORD=password
      - POSTGRES_DB=fastapi_traefik
    ports:
      - 5432:5432
    restart: unless-stopped
    healthcheck:
      test: [ "CMD-SHELL", "pg_isready -U fastapi_traefik" ]
      interval: 5s
      timeout: 5s
      retries: 5
    secrets:
      - db_password
    networks:
      - dock-db-test

  test_db:
    image: postgres:15-alpine
    container_name: test-db
    environment:
      - POSTGRES_USER=inclasstoday
      - POSTGRES_PASSWORD=inclasstoday1
      - POSTGRES_DB=student-data_test
      - APP_CONFIG_FILE=test
      - POSTGRES_HOST_AUTH_METHOD="trust"
    restart: unless-stopped
    volumes:
      - postgres_data_test:/var/lib/postgresql/data/
    ports:
      - 5433:5433
    healthcheck:
      test: [ "CMD-SHELL", "pg_isready -p 5433 -u inclasstoday" ]
      interval: 5s
      timeout: 5s
      retries: 5
    networks:
      - dock-db-test
    command: postgres -c port=5433

volumes:
  postgres_data:
  postgres_data_test:

secrets:
  db_password:
    file: db.password.txt

networks:
  dock-db-test:
    external: false
    name: dock-db-test

日志转储:

➜  travelapp git:(async) ✗ docker-compose up
Starting dev-db  ... done
Starting test-db ... done
Starting travelapp_backend_1 ... done
Attaching to dev-db, test-db, travelapp_backend_1
dev-db     |
dev-db     | PostgreSQL Database directory appears to contain a database; Skipping initialization
dev-db     |
dev-db     | 2023-08-08 19:29:41.618 UTC [1] LOG:  starting PostgreSQL 15.3 on x86_64-pc-linux-musl, compiled by gcc (Alpine 12.2.1_git20220924-r10) 12.2.1 20220924, 64-bit
dev-db     | 2023-08-08 19:29:41.622 UTC [1] LOG:  listening on IPv4 address "0.0.0.0", port 5432
dev-db     | 2023-08-08 19:29:41.623 UTC [1] LOG:  listening on IPv6 address "::", port 5432
dev-db     | 2023-08-08 19:29:41.630 UTC [1] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
dev-db     | 2023-08-08 19:29:41.660 UTC [62] LOG:  database system was interrupted; last known up at 2023-08-08 19:25:12 UTC
dev-db     | 2023-08-08 19:29:41.878 UTC [62] LOG:  database system was not properly shut down; automatic recovery in progress
dev-db     | 2023-08-08 19:29:41.884 UTC [62] LOG:  redo starts at 0/19A0F80
dev-db     | 2023-08-08 19:29:41.885 UTC [62] LOG:  invalid record length at 0/19A0FB8: wanted 24, got 0
dev-db     | 2023-08-08 19:29:41.885 UTC [62] LOG:  redo done at 0/19A0F80 system usage: CPU: user: 0.00 s, system: 0.00 s, elapsed: 0.00 s
dev-db     | 2023-08-08 19:29:41.891 UTC [58] LOG:  checkpoint starting: end-of-recovery immediate wait
dev-db     | 2023-08-08 19:29:41.902 UTC [58] LOG:  checkpoint complete: wrote 3 buffers (0.0%); 0 WAL file(s) added, 0 removed, 0 recycled; write=0.004 s, sync=0.001 s, total=0.012 s; sync files=2, longest=0.001 s, average=0.001 s; distance=0 kB, estimate=0 kB
dev-db     | 2023-08-08 19:29:41.916 UTC [1] LOG:  database system is ready to accept connections
test-db    |
test-db    | PostgreSQL Database directory appears to contain a database; Skipping initialization
test-db    |
test-db    | 2023-08-08 19:29:41.759 UTC [1] LOG:  starting PostgreSQL 15.3 on x86_64-pc-linux-musl, compiled by gcc (Alpine 12.2.1_git20220924-r10) 12.2.1 20220924, 64-bit
test-db    | 2023-08-08 19:29:41.762 UTC [1] LOG:  listening on IPv4 address "0.0.0.0", port 5433
test-db    | 2023-08-08 19:29:41.762 UTC [1] LOG:  listening on IPv6 address "::", port 5433
test-db    | 2023-08-08 19:29:41.767 UTC [1] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5433"
test-db    | 2023-08-08 19:29:41.783 UTC [62] LOG:  database system was shut down at 2023-08-08 19:27:17 UTC
test-db    | 2023-08-08 19:29:41.804 UTC [1] LOG:  database system is ready to accept connections
backend_1  | RUNNING ALEMBIC MIGRATIONS
backend_1  | INFO  [alembic.runtime.migration] Context impl PostgresqlImpl.
backend_1  | INFO  [alembic.runtime.migration] Will assume transactional DDL.
backend_1  | INFO:     Will watch for changes in these directories: ['/app']
backend_1  | INFO:     Uvicorn running on http://0.0.0.0:80 (Press CTRL+C to quit)
backend_1  | INFO:     Started reloader process [12] using WatchFiles
backend_1  | INFO:     Started server process [14]
backend_1  | INFO:     Waiting for application startup.
backend_1  | INFO:     Application startup complete.
test-db    | 2023-08-08 19:30:47.520 UTC [196] FATAL:  role "root" does not exist
test-db    | 2023-08-08 19:33:55.982 UTC [538] FATAL:  role "root" does not exist
test-db    | 2023-08-08 19:34:41.809 UTC [58] LOG:  checkpoint starting: time
test-db    | 2023-08-08 19:34:41.879 UTC [58] LOG:  checkpoint complete: wrote 3 buffers (0.0%); 0 WAL file(s) added, 0 removed, 0 recycled; write=0.017 s, sync=0.003 s, total=0.070 s; sync files=2, longest=0.002 s, average=0.001 s; distance=0 kB, estimate=0 kB

预期结果是 test-db 的 postgres 端口不再接受 5432 端口的连接,并且可以使用 -u 标志在 ps_isready 中连接。

英文:

Background: I'm trying to configure a local dev instance of a service with two docker postgres containers: one for dev and one that I can run pytest integration tests on. The app is built using fastapi and sqlalchemy with asyncpg.

I've got a couple issues here: the first is a problem running my healthcheck, and the second is that I've instructed the postgres container to listen at port 5433 instead of port 5432, but docker ps still shows the container has an active port at 5432. I think this is causing problems trying to connect to this test container. Note the unhealthy status as determined by healthcheck:

Postgres Docker容器在端口映射后仍然监听5432端口。

Note the healthcheck test line below: when the -u inclasstoday flag is included, it yields an unhealthy status above. However, if I connect to the docker container from my local machine (using docker-exec it test-db sh and then run the same command pg_isready -p 5433 -u inclasstoday, I get the following error:

Postgres Docker容器在端口映射后仍然监听5432端口。

/ # pg_isready -p 5433
/var/run/postgresql:5433 - accepting connections
/ # pg_isready -p 5433 -u inclasstoday
/usr/local/bin/pg_isready: unrecognized option: u
pg_isready: hint: Try &quot;pg_isready --help&quot; for more information.

However, connecting with only pg_isready -p 5433 works. When I try to edit my docker-compose.yml file to just use this command as a healthcheck, I then get the error:
test-db | 2023-08-08 19:30:47.520 UTC [196] FATAL: role &quot;root&quot; does not exist
Also note that this db log output is caused by the above pg_isready -p 5433 command executed inside the docker container from my local machine.

Included my docker-compose.yml file and a log dump below.

Docker-compose.yml

version: &quot;3.8&quot;
services:
backend:
build: ./backend
environment:
- POSTGRES_USER=fastapi_traefik
- POSTGRES_PASSWORD=password
- POSTGRES_DB=fastapi_traefik
- APP_CONFIG_FILE=local
command: bash -c &#39;while !&lt;/dev/tcp/db/5432; do sleep 1; done; cd backend; echo &quot;RUNNING ALEMBIC MIGRATIONS&quot;; alembic upgrade head; cd ..; uvicorn backend.main:app --host 0.0.0.0 --port 80 --reload&#39;
ports:
- 80:80
volumes:
- .:/app
depends_on:
db:
condition: service_healthy
secrets:
- db_password
networks:
- dock-db-test
db:
image: postgres:15-alpine
container_name: dev-db
volumes:
- postgres_data:/var/lib/postgresql/data/
environment:
- POSTGRES_USER=fastapi_traefik
- POSTGRES_PASSWORD=password
- POSTGRES_DB=fastapi_traefik
ports:
- 5432:5432
restart: unless-stopped
healthcheck:
test: [ &quot;CMD-SHELL&quot;, &quot;pg_isready -U fastapi_traefik&quot; ]
interval: 5s
timeout: 5s
retries: 5
secrets:
- db_password
networks:
- dock-db-test
test_db:
image: postgres:15-alpine
container_name: test-db
environment:
- POSTGRES_USER=inclasstoday
- POSTGRES_PASSWORD=inclasstoday1
- POSTGRES_DB=student-data_test
- APP_CONFIG_FILE=test
- POSTGRES_HOST_AUTH_METHOD=&quot;trust&quot;
restart: unless-stopped
volumes:
- postgres_data_test:/var/lib/postgresql/data/
ports:
- 5433:5433
healthcheck:
test: [ &quot;CMD-SHELL&quot;, &quot;pg_isready -p 5433 -u inclasstoday&quot; ]
interval: 5s
timeout: 5s
retries: 5
networks:
- dock-db-test
command: postgres -c port=5433
volumes:
postgres_data:
postgres_data_test:
secrets:
db_password:
file: db.password.txt
networks:
dock-db-test:
external: false
name: dock-db-test

Log dump:

➜  travelapp git:(async) ✗ docker-compose up
Starting dev-db  ... done
Starting test-db ... done
Starting travelapp_backend_1 ... done
Attaching to dev-db, test-db, travelapp_backend_1
dev-db     |
dev-db     | PostgreSQL Database directory appears to contain a database; Skipping initialization
dev-db     |
dev-db     | 2023-08-08 19:29:41.618 UTC [1] LOG:  starting PostgreSQL 15.3 on x86_64-pc-linux-musl, compiled by gcc (Alpine 12.2.1_git20220924-r10) 12.2.1 20220924, 64-bit
dev-db     | 2023-08-08 19:29:41.622 UTC [1] LOG:  listening on IPv4 address &quot;0.0.0.0&quot;, port 5432
dev-db     | 2023-08-08 19:29:41.623 UTC [1] LOG:  listening on IPv6 address &quot;::&quot;, port 5432
dev-db     | 2023-08-08 19:29:41.630 UTC [1] LOG:  listening on Unix socket &quot;/var/run/postgresql/.s.PGSQL.5432&quot;
dev-db     | 2023-08-08 19:29:41.660 UTC [62] LOG:  database system was interrupted; last known up at 2023-08-08 19:25:12 UTC
dev-db     | 2023-08-08 19:29:41.878 UTC [62] LOG:  database system was not properly shut down; automatic recovery in progress
dev-db     | 2023-08-08 19:29:41.884 UTC [62] LOG:  redo starts at 0/19A0F80
dev-db     | 2023-08-08 19:29:41.885 UTC [62] LOG:  invalid record length at 0/19A0FB8: wanted 24, got 0
dev-db     | 2023-08-08 19:29:41.885 UTC [62] LOG:  redo done at 0/19A0F80 system usage: CPU: user: 0.00 s, system: 0.00 s, elapsed: 0.00 s
dev-db     | 2023-08-08 19:29:41.891 UTC [58] LOG:  checkpoint starting: end-of-recovery immediate wait
dev-db     | 2023-08-08 19:29:41.902 UTC [58] LOG:  checkpoint complete: wrote 3 buffers (0.0%); 0 WAL file(s) added, 0 removed, 0 recycled; write=0.004 s, sync=0.001 s, total=0.012 s; sync files=2, longest=0.001 s, average=0.001 s; distance=0 kB, estimate=0 kB
dev-db     | 2023-08-08 19:29:41.916 UTC [1] LOG:  database system is ready to accept connections
test-db    |
test-db    | PostgreSQL Database directory appears to contain a database; Skipping initialization
test-db    |
test-db    | 2023-08-08 19:29:41.759 UTC [1] LOG:  starting PostgreSQL 15.3 on x86_64-pc-linux-musl, compiled by gcc (Alpine 12.2.1_git20220924-r10) 12.2.1 20220924, 64-bit
test-db    | 2023-08-08 19:29:41.762 UTC [1] LOG:  listening on IPv4 address &quot;0.0.0.0&quot;, port 5433
test-db    | 2023-08-08 19:29:41.762 UTC [1] LOG:  listening on IPv6 address &quot;::&quot;, port 5433
test-db    | 2023-08-08 19:29:41.767 UTC [1] LOG:  listening on Unix socket &quot;/var/run/postgresql/.s.PGSQL.5433&quot;
test-db    | 2023-08-08 19:29:41.783 UTC [62] LOG:  database system was shut down at 2023-08-08 19:27:17 UTC
test-db    | 2023-08-08 19:29:41.804 UTC [1] LOG:  database system is ready to accept connections
backend_1  | RUNNING ALEMBIC MIGRATIONS
backend_1  | INFO  [alembic.runtime.migration] Context impl PostgresqlImpl.
backend_1  | INFO  [alembic.runtime.migration] Will assume transactional DDL.
backend_1  | INFO:     Will watch for changes in these directories: [&#39;/app&#39;]
backend_1  | INFO:     Uvicorn running on http://0.0.0.0:80 (Press CTRL+C to quit)
backend_1  | INFO:     Started reloader process [12] using WatchFiles
backend_1  | INFO:     Started server process [14]
backend_1  | INFO:     Waiting for application startup.
backend_1  | INFO:     Application startup complete.
test-db    | 2023-08-08 19:30:47.520 UTC [196] FATAL:  role &quot;root&quot; does not exist
test-db    | 2023-08-08 19:33:55.982 UTC [538] FATAL:  role &quot;root&quot; does not exist
test-db    | 2023-08-08 19:34:41.809 UTC [58] LOG:  checkpoint starting: time
test-db    | 2023-08-08 19:34:41.879 UTC [58] LOG:  checkpoint complete: wrote 3 buffers (0.0%); 0 WAL file(s) added, 0 removed, 0 recycled; write=0.017 s, sync=0.003 s, total=0.070 s; sync files=2, longest=0.002 s, average=0.001 s; distance=0 kB, estimate=0 kB

Expected the postgres port to no longer be accepting connections at port 5432 for test-db, and to be able to connect using the user -u flag in ps_isready.

答案1

得分: 1

你传递了一个无效的参数。你传递了**-u**(小写),正确的是**-U**(大写)。此外,你可以传递一个数据库名称。
尝试以下命令:

test: [ "CMD-SHELL", "pg_isready -p 5433 -U inclasstoday -d student-data_test" ]
英文:

You are passed an invalid parameter. You passed -u (with lowercase), the correct one is -U (with uppercase). Also, you can pass a database name.
Try the following command:

test: [ &quot;CMD-SHELL&quot;, &quot;pg_isready -p 5433 -U inclasstoday -d student-data_test&quot; ]

huangapple
  • 本文由 发表于 2023年8月9日 03:38:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/76862746.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定