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

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

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端口。

  1. / # pg_isready -p 5433
  2. /var/run/postgresql:5433 - accepting connections
  3. / # pg_isready -p 5433 -u inclasstoday
  4. /usr/local/bin/pg_isready: unrecognized option: u
  5. 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

  1. version: "3.8"
  2. services:
  3. backend:
  4. build: ./backend
  5. environment:
  6. - POSTGRES_USER=fastapi_traefik
  7. - POSTGRES_PASSWORD=password
  8. - POSTGRES_DB=fastapi_traefik
  9. - APP_CONFIG_FILE=local
  10. 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'
  11. ports:
  12. - 80:80
  13. volumes:
  14. - .:/app
  15. depends_on:
  16. db:
  17. condition: service_healthy
  18. secrets:
  19. - db_password
  20. networks:
  21. - dock-db-test
  22. db:
  23. image: postgres:15-alpine
  24. container_name: dev-db
  25. volumes:
  26. - postgres_data:/var/lib/postgresql/data/
  27. environment:
  28. - POSTGRES_USER=fastapi_traefik
  29. - POSTGRES_PASSWORD=password
  30. - POSTGRES_DB=fastapi_traefik
  31. ports:
  32. - 5432:5432
  33. restart: unless-stopped
  34. healthcheck:
  35. test: [ "CMD-SHELL", "pg_isready -U fastapi_traefik" ]
  36. interval: 5s
  37. timeout: 5s
  38. retries: 5
  39. secrets:
  40. - db_password
  41. networks:
  42. - dock-db-test
  43. test_db:
  44. image: postgres:15-alpine
  45. container_name: test-db
  46. environment:
  47. - POSTGRES_USER=inclasstoday
  48. - POSTGRES_PASSWORD=inclasstoday1
  49. - POSTGRES_DB=student-data_test
  50. - APP_CONFIG_FILE=test
  51. - POSTGRES_HOST_AUTH_METHOD="trust"
  52. restart: unless-stopped
  53. volumes:
  54. - postgres_data_test:/var/lib/postgresql/data/
  55. ports:
  56. - 5433:5433
  57. healthcheck:
  58. test: [ "CMD-SHELL", "pg_isready -p 5433 -u inclasstoday" ]
  59. interval: 5s
  60. timeout: 5s
  61. retries: 5
  62. networks:
  63. - dock-db-test
  64. command: postgres -c port=5433
  65. volumes:
  66. postgres_data:
  67. postgres_data_test:
  68. secrets:
  69. db_password:
  70. file: db.password.txt
  71. networks:
  72. dock-db-test:
  73. external: false
  74. name: dock-db-test

日志转储:

  1. travelapp git:(async) docker-compose up
  2. Starting dev-db ... done
  3. Starting test-db ... done
  4. Starting travelapp_backend_1 ... done
  5. Attaching to dev-db, test-db, travelapp_backend_1
  6. dev-db |
  7. dev-db | PostgreSQL Database directory appears to contain a database; Skipping initialization
  8. dev-db |
  9. 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
  10. dev-db | 2023-08-08 19:29:41.622 UTC [1] LOG: listening on IPv4 address "0.0.0.0", port 5432
  11. dev-db | 2023-08-08 19:29:41.623 UTC [1] LOG: listening on IPv6 address "::", port 5432
  12. dev-db | 2023-08-08 19:29:41.630 UTC [1] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
  13. 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
  14. dev-db | 2023-08-08 19:29:41.878 UTC [62] LOG: database system was not properly shut down; automatic recovery in progress
  15. dev-db | 2023-08-08 19:29:41.884 UTC [62] LOG: redo starts at 0/19A0F80
  16. dev-db | 2023-08-08 19:29:41.885 UTC [62] LOG: invalid record length at 0/19A0FB8: wanted 24, got 0
  17. 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
  18. dev-db | 2023-08-08 19:29:41.891 UTC [58] LOG: checkpoint starting: end-of-recovery immediate wait
  19. 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
  20. dev-db | 2023-08-08 19:29:41.916 UTC [1] LOG: database system is ready to accept connections
  21. test-db |
  22. test-db | PostgreSQL Database directory appears to contain a database; Skipping initialization
  23. test-db |
  24. 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
  25. test-db | 2023-08-08 19:29:41.762 UTC [1] LOG: listening on IPv4 address "0.0.0.0", port 5433
  26. test-db | 2023-08-08 19:29:41.762 UTC [1] LOG: listening on IPv6 address "::", port 5433
  27. test-db | 2023-08-08 19:29:41.767 UTC [1] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5433"
  28. 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
  29. test-db | 2023-08-08 19:29:41.804 UTC [1] LOG: database system is ready to accept connections
  30. backend_1 | RUNNING ALEMBIC MIGRATIONS
  31. backend_1 | INFO [alembic.runtime.migration] Context impl PostgresqlImpl.
  32. backend_1 | INFO [alembic.runtime.migration] Will assume transactional DDL.
  33. backend_1 | INFO: Will watch for changes in these directories: ['/app']
  34. backend_1 | INFO: Uvicorn running on http://0.0.0.0:80 (Press CTRL+C to quit)
  35. backend_1 | INFO: Started reloader process [12] using WatchFiles
  36. backend_1 | INFO: Started server process [14]
  37. backend_1 | INFO: Waiting for application startup.
  38. backend_1 | INFO: Application startup complete.
  39. test-db | 2023-08-08 19:30:47.520 UTC [196] FATAL: role "root" does not exist
  40. test-db | 2023-08-08 19:33:55.982 UTC [538] FATAL: role "root" does not exist
  41. test-db | 2023-08-08 19:34:41.809 UTC [58] LOG: checkpoint starting: time
  42. 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端口。

  1. / # pg_isready -p 5433
  2. /var/run/postgresql:5433 - accepting connections
  3. / # pg_isready -p 5433 -u inclasstoday
  4. /usr/local/bin/pg_isready: unrecognized option: u
  5. 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

  1. version: &quot;3.8&quot;
  2. services:
  3. backend:
  4. build: ./backend
  5. environment:
  6. - POSTGRES_USER=fastapi_traefik
  7. - POSTGRES_PASSWORD=password
  8. - POSTGRES_DB=fastapi_traefik
  9. - APP_CONFIG_FILE=local
  10. 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;
  11. ports:
  12. - 80:80
  13. volumes:
  14. - .:/app
  15. depends_on:
  16. db:
  17. condition: service_healthy
  18. secrets:
  19. - db_password
  20. networks:
  21. - dock-db-test
  22. db:
  23. image: postgres:15-alpine
  24. container_name: dev-db
  25. volumes:
  26. - postgres_data:/var/lib/postgresql/data/
  27. environment:
  28. - POSTGRES_USER=fastapi_traefik
  29. - POSTGRES_PASSWORD=password
  30. - POSTGRES_DB=fastapi_traefik
  31. ports:
  32. - 5432:5432
  33. restart: unless-stopped
  34. healthcheck:
  35. test: [ &quot;CMD-SHELL&quot;, &quot;pg_isready -U fastapi_traefik&quot; ]
  36. interval: 5s
  37. timeout: 5s
  38. retries: 5
  39. secrets:
  40. - db_password
  41. networks:
  42. - dock-db-test
  43. test_db:
  44. image: postgres:15-alpine
  45. container_name: test-db
  46. environment:
  47. - POSTGRES_USER=inclasstoday
  48. - POSTGRES_PASSWORD=inclasstoday1
  49. - POSTGRES_DB=student-data_test
  50. - APP_CONFIG_FILE=test
  51. - POSTGRES_HOST_AUTH_METHOD=&quot;trust&quot;
  52. restart: unless-stopped
  53. volumes:
  54. - postgres_data_test:/var/lib/postgresql/data/
  55. ports:
  56. - 5433:5433
  57. healthcheck:
  58. test: [ &quot;CMD-SHELL&quot;, &quot;pg_isready -p 5433 -u inclasstoday&quot; ]
  59. interval: 5s
  60. timeout: 5s
  61. retries: 5
  62. networks:
  63. - dock-db-test
  64. command: postgres -c port=5433
  65. volumes:
  66. postgres_data:
  67. postgres_data_test:
  68. secrets:
  69. db_password:
  70. file: db.password.txt
  71. networks:
  72. dock-db-test:
  73. external: false
  74. name: dock-db-test

Log dump:

  1. travelapp git:(async) docker-compose up
  2. Starting dev-db ... done
  3. Starting test-db ... done
  4. Starting travelapp_backend_1 ... done
  5. Attaching to dev-db, test-db, travelapp_backend_1
  6. dev-db |
  7. dev-db | PostgreSQL Database directory appears to contain a database; Skipping initialization
  8. dev-db |
  9. 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
  10. dev-db | 2023-08-08 19:29:41.622 UTC [1] LOG: listening on IPv4 address &quot;0.0.0.0&quot;, port 5432
  11. dev-db | 2023-08-08 19:29:41.623 UTC [1] LOG: listening on IPv6 address &quot;::&quot;, port 5432
  12. dev-db | 2023-08-08 19:29:41.630 UTC [1] LOG: listening on Unix socket &quot;/var/run/postgresql/.s.PGSQL.5432&quot;
  13. 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
  14. dev-db | 2023-08-08 19:29:41.878 UTC [62] LOG: database system was not properly shut down; automatic recovery in progress
  15. dev-db | 2023-08-08 19:29:41.884 UTC [62] LOG: redo starts at 0/19A0F80
  16. dev-db | 2023-08-08 19:29:41.885 UTC [62] LOG: invalid record length at 0/19A0FB8: wanted 24, got 0
  17. 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
  18. dev-db | 2023-08-08 19:29:41.891 UTC [58] LOG: checkpoint starting: end-of-recovery immediate wait
  19. 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
  20. dev-db | 2023-08-08 19:29:41.916 UTC [1] LOG: database system is ready to accept connections
  21. test-db |
  22. test-db | PostgreSQL Database directory appears to contain a database; Skipping initialization
  23. test-db |
  24. 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
  25. test-db | 2023-08-08 19:29:41.762 UTC [1] LOG: listening on IPv4 address &quot;0.0.0.0&quot;, port 5433
  26. test-db | 2023-08-08 19:29:41.762 UTC [1] LOG: listening on IPv6 address &quot;::&quot;, port 5433
  27. test-db | 2023-08-08 19:29:41.767 UTC [1] LOG: listening on Unix socket &quot;/var/run/postgresql/.s.PGSQL.5433&quot;
  28. 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
  29. test-db | 2023-08-08 19:29:41.804 UTC [1] LOG: database system is ready to accept connections
  30. backend_1 | RUNNING ALEMBIC MIGRATIONS
  31. backend_1 | INFO [alembic.runtime.migration] Context impl PostgresqlImpl.
  32. backend_1 | INFO [alembic.runtime.migration] Will assume transactional DDL.
  33. backend_1 | INFO: Will watch for changes in these directories: [&#39;/app&#39;]
  34. backend_1 | INFO: Uvicorn running on http://0.0.0.0:80 (Press CTRL+C to quit)
  35. backend_1 | INFO: Started reloader process [12] using WatchFiles
  36. backend_1 | INFO: Started server process [14]
  37. backend_1 | INFO: Waiting for application startup.
  38. backend_1 | INFO: Application startup complete.
  39. test-db | 2023-08-08 19:30:47.520 UTC [196] FATAL: role &quot;root&quot; does not exist
  40. test-db | 2023-08-08 19:33:55.982 UTC [538] FATAL: role &quot;root&quot; does not exist
  41. test-db | 2023-08-08 19:34:41.809 UTC [58] LOG: checkpoint starting: time
  42. 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**(大写)。此外,你可以传递一个数据库名称。
尝试以下命令:

  1. 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:

  1. 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:

确定