英文:
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:
FROM mcr.microsoft.com/vscode/devcontainers/python:3.9
RUN pip3 install pytest black
devcontainer.json(当前文件的版本):
{
"name": "Test development env",
"dockerComposeFile": "./docker-compose.yml",
"service": "python-app",
"workspaceFolder": "/workspace",
"settings": {
"files.eol": "\n",
"python.languageServer": "Pylance",
"python.formatting.provider": "black",
"editor.formatOnSave": true,
"files.exclude": {
"**/__pycache__": true,
"**/.pytest_cache": true,
"**/.venv": true,
"**/*.egg-info": true
},
"python.pythonPath": "/usr/local/bin/python",
"python.testing.pytestPath": "/usr/local/bin/pytest",
"python.testing.pytestEnabled": true,
"python.testing.autoTestDiscoverOnSaveEnabled": true,
"python.linting.ignorePatterns": [
".vscode/",
"**/site-packages/",
"**/__pycache__/",
"**/.pytest_cache/",
"**/*.egg-info"
]
},
"extensions": [
"ms-python.vscode-pylance",
"mhutchie.git-graph"
],
"remoteUser": "vscode",
"containerUser": "vscode",
"runArgs": [
"run",
"--service-ports",
"--rm",
"python-app"
],
"overrideCommand": true,
"postCreateCommand": "pip install -U --force-reinstall -e ."
}
docker-compose.yml(当然,在我的原始文件中,用户/密码/数据库是正确的,这里我隐藏了它们):
version: '3'
services:
db:
image: postgres:14.8
restart: unless-stopped //no difference with restart: always
environment:
POSTGRES_USER: <user>
POSTGRES_PASSWORD: <password>
POSTGRES_DB: <db>
volumes:
- ./postgres-data:/var/lib/postgresql/data
pgadmin:
image: dpage/pgadmin4
restart: always
environment:
PGADMIN_DEFAULT_EMAIL: <email>
PGADMIN_DEFAULT_PASSWORD: <password>
ports:
- 5432:5432
depends_on:
- db
python-app:
build:
context: .
dockerfile: Dockerfile
restart: always
volumes:
- .:/app
depends_on:
- db
这些是我用来构建仅运行良好的Python应用程序的文件:
Dockerfile:与之前相同
devcontainer.json:
{
"name": "Test development env",
"dockerFile": "Dockerfile",
"settings": {
"files.eol": "\n",
"python.languageServer": "Pylance",
"python.formatting.provider": "black",
"editor.formatOnSave": true,
"files.exclude": {
"**/__pycache__": true,
"**/.pytest_cache": true,
"**/.venv": true,
"**/*.egg-info": true
},
"python.pythonPath": "/usr/local/bin/python",
"python.testing.pytestPath": "/usr/local/bin/pytest",
"python.testing.pytestEnabled": true,
"python.testing.autoTestDiscoverOnSaveEnabled": true,
"python.linting
<details>
<summary>英文:</summary>
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:
This is the error I get when trying to build container:
[23165 ms] Error: An error occurred setting up the container.
[23165 ms] at Pte (c:\Users\IT\.vscode\extensions\ms-vscode-remote.remote-
containers-0.299.0\dist\spec-node\devContainersSpecCLI.js:1954:3572)
[23165 ms] at process.processTicksAndRejections
(node:internal/process/task_queues:96:5)
[23165 ms] at async rre (c:\Users\IT\.vscode\extensions\ms-vscode-remote.remote-
containers-0.299.0\dist\spec-node\devContainersSpecCLI.js:2024:3833)
[23166 ms] at async Yf (c:\Users\IT\.vscode\extensions\ms-vscode-remote.remote-
containers-0.299.0\dist\spec-node\devContainersSpecCLI.js:2024:4775)
[23166 ms] at async Dne (c:\Users\IT\.vscode\extensions\ms-vscode-remote.remote-
containers-0.299.0\dist\spec-node\devContainersSpecCLI.js:2156:12193)
[23166 ms] at async Sne (c:\Users\IT\.vscode\extensions\ms-vscode-remote.remote-
containers-0.299.0\dist\spec-node\devContainersSpecCLI.js:2156:11934)
[23176 ms] Exit code 1
[23180 ms] Command failed: 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] Exit code 1
These are my files. All of them are stored in the same directory ".devcontainer/" in the main folder of the project:
**Dockerfile**:
FROM mcr.microsoft.com/vscode/devcontainers/python:3.9
RUN pip3 install pytest black
**devcontainer.json** (current version of the file):
{
"name": "Test development env",
"dockerComposeFile": "./docker-compose.yml",
"service": "python-app",
"workspaceFolder": "/workspace",
"settings": {
"files.eol": "\n",
"python.languageServer": "Pylance",
"python.formatting.provider": "black",
"editor.formatOnSave": true,
"files.exclude": {
"**/__pycache__": true,
"**/.pytest_cache": true,
"**/.venv": true,
"**/*.egg-info": true
},
"python.pythonPath": "/usr/local/bin/python",
"python.testing.pytestPath": "/usr/local/bin/pytest",
"python.testing.pytestEnabled": true,
"python.testing.autoTestDiscoverOnSaveEnabled": true,
"python.linting.ignorePatterns": [
".vscode/",
"**/site-packages/",
"**/__pycache__/",
"**/.pytest_cache/",
"**/*.egg-info"
]
},
"extensions": [
"ms-python.vscode-pylance",
"mhutchie.git-graph"
],
"remoteUser": "vscode",
"containerUser": "vscode",
"runArgs": [
"run",
"--service-ports",
"--rm",
"python-app"
],
"overrideCommand": true, // get the same error with/out this line, no difference
"postCreateCommand": "pip install -U --force-reinstall -e ."
}
**docker-compose.yml**. (of course users/passwords/db are right in my original files, I hide them here)
version: '3'
services:
db:
image: postgres:14.8
restart: unless-stopped //no difference with restart: always
environment:
POSTGRES_USER: <user>
POSTGRES_PASSWORD: <password>
POSTGRES_DB: <db>
volumes:
- ./postgres-data:/var/lib/postgresql/data
pgadmin:
image: dpage/pgadmin4
restart: always
environment:
PGADMIN_DEFAULT_EMAIL: <email>
PGADMIN_DEFAULT_PASSWORD: <password>
ports:
- 5432:5432
depends_on:
- db
python-app:
build:
context: .
dockerfile: Dockerfile
restart: always
volumes:
- .:/app
depends_on:
- db
And these are the files I'm using to build only my python app that works well:
**Dockerfile**: same as before
**devcontainer.json**
{
"name": "Test development env",
"dockerFile": "Dockerfile",
"settings": {
"files.eol": "\n",
"python.languageServer": "Pylance",
"python.formatting.provider": "black",
"editor.formatOnSave": true,
"files.exclude": {
"**/__pycache__": true,
"**/.pytest_cache": true,
"**/.venv": true,
"**/*.egg-info": true
},
"python.pythonPath": "/usr/local/bin/python",
"python.testing.pytestPath": "/usr/local/bin/pytest",
"python.testing.pytestEnabled": true,
"python.testing.autoTestDiscoverOnSaveEnabled": true,
"python.linting.ignorePatterns": [
".vscode/",
"**/site-packages/",
"**/__pycache__/",
"**/.pytest_cache/",
"**/*.egg-info"
],
},
"extensions": [
"ms-python.vscode-pylance",
"mhutchie.git-graph",
],
"remoteUser": "vscode",
"containerUser": "vscode",
"runArgs": [
"--network=host",
],
"postCreateCommand": "pip install -U --force-reinstall -e ."
}
Any ideas on how to do this? I think I have something wrong in my files but I also think I'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?
Thanks in advance, regards!
</details>
# 答案1
**得分**: 0
我尝试了这个并且有效。保留了我的原始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
<details>
<summary>英文:</summary>
I tryied this and worked. Kept my original devcontainer.json & Dockerfile files, and in the docker-compose.yml I put this:
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
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论