添加/链接一个Docker PostgreSQL数据库容器到我的现有Docker Python容器。

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

Add/link a Docker postgres database container to my existing Docker python container

问题

我有一个已经docker化的Python应用程序,运行得非常好,现在我想添加一个PostgreSQL数据库。我认为最好的方法是将数据库docker化,并将其与我的Python应用程序连接起来,以便我的应用程序可以将数据存储在其中,但每次构建容器时,都会遇到相同的错误:

这是尝试构建容器时我遇到的错误:

[23165 ms] 错误:设置容器时发生错误。
[23165 ms] 在 Pte (c:\Users\IT.vscode\extensions\ms-vscode-remote.remote-containers-0.299.0\dist\spec-node\devContainersSpecCLI.js:1954:3572)
[23165 ms] 在 process.processTicksAndRejections (node:internal/process/task_queues:96:5)
[23165 ms] 异步执行 rre (c:\Users\IT.vscode\extensions\ms-vscode-remote.remote-containers-0.299.0\dist\spec-node\devContainersSpecCLI.js:2024:3833)
[23166 ms] 异步执行 Yf (c:\Users\IT.vscode\extensions\ms-vscode-remote.remote-containers-0.299.0\dist\spec-node\devContainersSpecCLI.js:2024:4775)
[23166 ms] 异步执行 Dne (c:\Users\IT.vscode\extensions\ms-vscode-remote.remote-containers-0.299.0\dist\spec-node\devContainersSpecCLI.js:2156:12193)
[23166 ms] 异步执行 Sne (c:\Users\IT.vscode\extensions\ms-vscode-remote.remote-containers-0.299.0\dist\spec-node\devContainersSpecCLI.js:2156:11934)
[23176 ms] 退出码 1
[23180 ms] 命令失败:C:\Users\IT\AppData\Local\Programs\Microsoft VS Code\Code.exe --ms-enable-electron-run-as-node c:\Users\IT.vscode\extensions\ms-vscode-remote.remote-containers-0.299.0\dist\spec-node\devContainersSpecCLI.js up --user-data-folder c:\Users\IT\AppData\Roaming\Code\User\globalStorage\ms-vscode-remote.remote-containers\data --container-session-data-folder /tmp/devcontainers-f9b48955-34b9-42ed-b7eb-f5c37f6b7fb61689235866772 --workspace-folder c:\Users\IT\Documents\gys_test -- workspace-mount-consistency cached --id-label devcontainer.local_folder=c:\Users\IT\Documents\gys_test --id-label devcontainer.config_file=c:\Users\IT\Documents\gys_test.devcontainer\devcontainer.json --log-level debug --log-format json --config c:\Users\IT\Documents\gys_test.devcontainer\devcontainer.json --default-user-env-probe loginInteractiveShell --mount type=volume,source=vscode,target=/vscode,external=true -- skip-post-create --update-remote-user-uid-default on --mount-workspace-git-root true
[23180 ms] 退出码 1

这些是我的文件。它们都存储在项目主文件夹的相同目录“.devcontainer/”中:

Dockerfile:

  1. FROM mcr.microsoft.com/vscode/devcontainers/python:3.9
  2. RUN pip3 install pytest black

devcontainer.json(当前文件的版本):

  1. {
  2. "name": "Test development env",
  3. "dockerComposeFile": "./docker-compose.yml",
  4. "service": "python-app",
  5. "workspaceFolder": "/workspace",
  6. "settings": {
  7. "files.eol": "\n",
  8. "python.languageServer": "Pylance",
  9. "python.formatting.provider": "black",
  10. "editor.formatOnSave": true,
  11. "files.exclude": {
  12. "**/__pycache__": true,
  13. "**/.pytest_cache": true,
  14. "**/.venv": true,
  15. "**/*.egg-info": true
  16. },
  17. "python.pythonPath": "/usr/local/bin/python",
  18. "python.testing.pytestPath": "/usr/local/bin/pytest",
  19. "python.testing.pytestEnabled": true,
  20. "python.testing.autoTestDiscoverOnSaveEnabled": true,
  21. "python.linting.ignorePatterns": [
  22. ".vscode/",
  23. "**/site-packages/",
  24. "**/__pycache__/",
  25. "**/.pytest_cache/",
  26. "**/*.egg-info"
  27. ]
  28. },
  29. "extensions": [
  30. "ms-python.vscode-pylance",
  31. "mhutchie.git-graph"
  32. ],
  33. "remoteUser": "vscode",
  34. "containerUser": "vscode",
  35. "runArgs": [
  36. "run",
  37. "--service-ports",
  38. "--rm",
  39. "python-app"
  40. ],
  41. "overrideCommand": true,
  42. "postCreateCommand": "pip install -U --force-reinstall -e ."
  43. }

docker-compose.yml(当然,在我的原始文件中,用户/密码/数据库是正确的,这里我隐藏了它们):

  1. version: '3'
  2. services:
  3. db:
  4. image: postgres:14.8
  5. restart: unless-stopped //no difference with restart: always
  6. environment:
  7. POSTGRES_USER: <user>
  8. POSTGRES_PASSWORD: <password>
  9. POSTGRES_DB: <db>
  10. volumes:
  11. - ./postgres-data:/var/lib/postgresql/data
  12. pgadmin:
  13. image: dpage/pgadmin4
  14. restart: always
  15. environment:
  16. PGADMIN_DEFAULT_EMAIL: <email>
  17. PGADMIN_DEFAULT_PASSWORD: <password>
  18. ports:
  19. - 5432:5432
  20. depends_on:
  21. - db
  22. python-app:
  23. build:
  24. context: .
  25. dockerfile: Dockerfile
  26. restart: always
  27. volumes:
  28. - .:/app
  29. depends_on:
  30. - db

这些是我用来构建仅运行良好的Python应用程序的文件:

Dockerfile:与之前相同

devcontainer.json

  1. {
  2. "name": "Test development env",
  3. "dockerFile": "Dockerfile",
  4. "settings": {
  5. "files.eol": "\n",
  6. "python.languageServer": "Pylance",
  7. "python.formatting.provider": "black",
  8. "editor.formatOnSave": true,
  9. "files.exclude": {
  10. "**/__pycache__": true,
  11. "**/.pytest_cache": true,
  12. "**/.venv": true,
  13. "**/*.egg-info": true
  14. },
  15. "python.pythonPath": "/usr/local/bin/python",
  16. "python.testing.pytestPath": "/usr/local/bin/pytest",
  17. "python.testing.pytestEnabled": true,
  18. "python.testing.autoTestDiscoverOnSaveEnabled": true,
  19. "python.linting
  20. <details>
  21. <summary>英文:</summary>
  22. I have a python application dockerized that works really fine and now I want to add a postgres database. I thought the best idea is to dockerize the database and link it with my python app, so my app can store data in it, but every time the container is built, i get the same error:
  23. This is the error I get when trying to build container:
  24. [23165 ms] Error: An error occurred setting up the container.
  25. [23165 ms] at Pte (c:\Users\IT\.vscode\extensions\ms-vscode-remote.remote-
  26. containers-0.299.0\dist\spec-node\devContainersSpecCLI.js:1954:3572)
  27. [23165 ms] at process.processTicksAndRejections
  28. (node:internal/process/task_queues:96:5)
  29. [23165 ms] at async rre (c:\Users\IT\.vscode\extensions\ms-vscode-remote.remote-
  30. containers-0.299.0\dist\spec-node\devContainersSpecCLI.js:2024:3833)
  31. [23166 ms] at async Yf (c:\Users\IT\.vscode\extensions\ms-vscode-remote.remote-
  32. containers-0.299.0\dist\spec-node\devContainersSpecCLI.js:2024:4775)
  33. [23166 ms] at async Dne (c:\Users\IT\.vscode\extensions\ms-vscode-remote.remote-
  34. containers-0.299.0\dist\spec-node\devContainersSpecCLI.js:2156:12193)
  35. [23166 ms] at async Sne (c:\Users\IT\.vscode\extensions\ms-vscode-remote.remote-
  36. containers-0.299.0\dist\spec-node\devContainersSpecCLI.js:2156:11934)
  37. [23176 ms] Exit code 1
  38. [23180 ms] Command failed: C:\Users\IT\AppData\Local\Programs\Microsoft VS Code\Code.exe
  39. --ms-enable-electron-run-as-node c:\Users\IT\.vscode\extensions\ms-vscode-remote.remote-
  40. containers-0.299.0\dist\spec-node\devContainersSpecCLI.js up --user-data-folder
  41. c:\Users\IT\AppData\Roaming\Code\User\globalStorage\ms-vscode-remote.remote-
  42. containers\data --container-session-data-folder /tmp/devcontainers-f9b48955-34b9-42ed-
  43. b7eb-f5c37f6b7fb61689235866772 --workspace-folder c:\Users\IT\Documents\gys_test --
  44. workspace-mount-consistency cached --id-label
  45. devcontainer.local_folder=c:\Users\IT\Documents\gys_test --id-label
  46. devcontainer.config_file=c:\Users\IT\Documents\gys_test\.devcontainer\devcontainer.json
  47. --log-level debug --log-format json --config
  48. c:\Users\IT\Documents\gys_test\.devcontainer\devcontainer.json --default-user-env-probe
  49. loginInteractiveShell --mount type=volume,source=vscode,target=/vscode,external=true --
  50. skip-post-create --update-remote-user-uid-default on --mount-workspace-git-root true
  51. [23180 ms] Exit code 1
  52. These are my files. All of them are stored in the same directory &quot;.devcontainer/&quot; in the main folder of the project:
  53. **Dockerfile**:
  54. FROM mcr.microsoft.com/vscode/devcontainers/python:3.9
  55. RUN pip3 install pytest black
  56. **devcontainer.json** (current version of the file):
  57. {
  58. &quot;name&quot;: &quot;Test development env&quot;,
  59. &quot;dockerComposeFile&quot;: &quot;./docker-compose.yml&quot;,
  60. &quot;service&quot;: &quot;python-app&quot;,
  61. &quot;workspaceFolder&quot;: &quot;/workspace&quot;,
  62. &quot;settings&quot;: {
  63. &quot;files.eol&quot;: &quot;\n&quot;,
  64. &quot;python.languageServer&quot;: &quot;Pylance&quot;,
  65. &quot;python.formatting.provider&quot;: &quot;black&quot;,
  66. &quot;editor.formatOnSave&quot;: true,
  67. &quot;files.exclude&quot;: {
  68. &quot;**/__pycache__&quot;: true,
  69. &quot;**/.pytest_cache&quot;: true,
  70. &quot;**/.venv&quot;: true,
  71. &quot;**/*.egg-info&quot;: true
  72. },
  73. &quot;python.pythonPath&quot;: &quot;/usr/local/bin/python&quot;,
  74. &quot;python.testing.pytestPath&quot;: &quot;/usr/local/bin/pytest&quot;,
  75. &quot;python.testing.pytestEnabled&quot;: true,
  76. &quot;python.testing.autoTestDiscoverOnSaveEnabled&quot;: true,
  77. &quot;python.linting.ignorePatterns&quot;: [
  78. &quot;.vscode/&quot;,
  79. &quot;**/site-packages/&quot;,
  80. &quot;**/__pycache__/&quot;,
  81. &quot;**/.pytest_cache/&quot;,
  82. &quot;**/*.egg-info&quot;
  83. ]
  84. },
  85. &quot;extensions&quot;: [
  86. &quot;ms-python.vscode-pylance&quot;,
  87. &quot;mhutchie.git-graph&quot;
  88. ],
  89. &quot;remoteUser&quot;: &quot;vscode&quot;,
  90. &quot;containerUser&quot;: &quot;vscode&quot;,
  91. &quot;runArgs&quot;: [
  92. &quot;run&quot;,
  93. &quot;--service-ports&quot;,
  94. &quot;--rm&quot;,
  95. &quot;python-app&quot;
  96. ],
  97. &quot;overrideCommand&quot;: true, // get the same error with/out this line, no difference
  98. &quot;postCreateCommand&quot;: &quot;pip install -U --force-reinstall -e .&quot;
  99. }
  100. **docker-compose.yml**. (of course users/passwords/db are right in my original files, I hide them here)
  101. version: &#39;3&#39;
  102. services:
  103. db:
  104. image: postgres:14.8
  105. restart: unless-stopped //no difference with restart: always
  106. environment:
  107. POSTGRES_USER: &lt;user&gt;
  108. POSTGRES_PASSWORD: &lt;password&gt;
  109. POSTGRES_DB: &lt;db&gt;
  110. volumes:
  111. - ./postgres-data:/var/lib/postgresql/data
  112. pgadmin:
  113. image: dpage/pgadmin4
  114. restart: always
  115. environment:
  116. PGADMIN_DEFAULT_EMAIL: &lt;email&gt;
  117. PGADMIN_DEFAULT_PASSWORD: &lt;password&gt;
  118. ports:
  119. - 5432:5432
  120. depends_on:
  121. - db
  122. python-app:
  123. build:
  124. context: .
  125. dockerfile: Dockerfile
  126. restart: always
  127. volumes:
  128. - .:/app
  129. depends_on:
  130. - db
  131. And these are the files I&#39;m using to build only my python app that works well:
  132. **Dockerfile**: same as before
  133. **devcontainer.json**
  134. {
  135. &quot;name&quot;: &quot;Test development env&quot;,
  136. &quot;dockerFile&quot;: &quot;Dockerfile&quot;,
  137. &quot;settings&quot;: {
  138. &quot;files.eol&quot;: &quot;\n&quot;,
  139. &quot;python.languageServer&quot;: &quot;Pylance&quot;,
  140. &quot;python.formatting.provider&quot;: &quot;black&quot;,
  141. &quot;editor.formatOnSave&quot;: true,
  142. &quot;files.exclude&quot;: {
  143. &quot;**/__pycache__&quot;: true,
  144. &quot;**/.pytest_cache&quot;: true,
  145. &quot;**/.venv&quot;: true,
  146. &quot;**/*.egg-info&quot;: true
  147. },
  148. &quot;python.pythonPath&quot;: &quot;/usr/local/bin/python&quot;,
  149. &quot;python.testing.pytestPath&quot;: &quot;/usr/local/bin/pytest&quot;,
  150. &quot;python.testing.pytestEnabled&quot;: true,
  151. &quot;python.testing.autoTestDiscoverOnSaveEnabled&quot;: true,
  152. &quot;python.linting.ignorePatterns&quot;: [
  153. &quot;.vscode/&quot;,
  154. &quot;**/site-packages/&quot;,
  155. &quot;**/__pycache__/&quot;,
  156. &quot;**/.pytest_cache/&quot;,
  157. &quot;**/*.egg-info&quot;
  158. ],
  159. },
  160. &quot;extensions&quot;: [
  161. &quot;ms-python.vscode-pylance&quot;,
  162. &quot;mhutchie.git-graph&quot;,
  163. ],
  164. &quot;remoteUser&quot;: &quot;vscode&quot;,
  165. &quot;containerUser&quot;: &quot;vscode&quot;,
  166. &quot;runArgs&quot;: [
  167. &quot;--network=host&quot;,
  168. ],
  169. &quot;postCreateCommand&quot;: &quot;pip install -U --force-reinstall -e .&quot;
  170. }
  171. Any ideas on how to do this? I think I have something wrong in my files but I also think I&#39;m probably missing something... in my VSCode I have the Dev Containers and Remote Cevelopment extensions installed, could be the problem any of them? How do I fix it?
  172. Thanks in advance, regards!
  173. </details>
  174. # 答案1
  175. **得分**: 0
  176. 我尝试了这个并且有效。保留了我的原始devcontainer.json和Dockerfile文件,在docker-compose.yml中添加了以下内容:

version: '3'
services:
db:
image: postgres:15.3
restart: always
environment:
POSTGRES_USER: <my_user>
POSTGRES_PASSWORD: <my_pass>
POSTGRES_DB: <my_db>
volumes:
- ./postgres-data:/var/lib/postgresql/data
ports:
- 5432:5432

  1. <details>
  2. <summary>英文:</summary>
  3. I tryied this and worked. Kept my original devcontainer.json &amp; Dockerfile files, and in the docker-compose.yml I put this:
  4. version: &#39;3&#39;
  5. services:
  6. db:
  7. image: postgres:15.3
  8. restart: always
  9. environment:
  10. POSTGRES_USER: &lt;my_user&gt;
  11. POSTGRES_PASSWORD: &lt;my_pass&gt;
  12. POSTGRES_DB: &lt;my_db&gt;
  13. volumes:
  14. - ./postgres-data:/var/lib/postgresql/data
  15. ports:
  16. - 5432:5432
  17. </details>

huangapple
  • 本文由 发表于 2023年7月13日 16:30:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/76677380.html
匿名

发表评论

匿名网友

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

确定