105-Frappe-docker
如果你是使用 Frappe 官方 Docker 部署的环境,可以在 容器内部 运行 Python 代码。以下是正确的执行步骤:
方法 1:进入容器并执行 Python 代码¶
步骤¶
- 列出运行中的 Docker 容器(找到 Frappe 站点的容器):
docker ps
你会看到类似:
CONTAINER ID IMAGE COMMAND STATUS PORTS NAMES
abc123 frappe/erpnext-worker "docker-entrypoint.s…" Up 10 hours frappe_worker
def456 frappe/erpnext-nginx "nginx -g 'daemon of…" Up 10 hours 0.0.0.0:8080->8080/tcp frappe_nginx
ghi789 frappe/erpnext-python "docker-entrypoint.s…" Up 10 hours frappe_backend
- 进入
frappe_backend
容器(通常是frappe/erpnext-python
):
docker exec -it frappe_backend bash
docker exec -it frappe_docker-backend-1 bash
(将 frappe_backend
替换为你的 CONTAINER ID
或 NAMES
)
- 进入 Frappe Shell(Python 交互模式)
bench list-apps
bench --site your-site console
bench --site frontend console
your-site
替换为你的站点名称,比如site1.local
。-
运行后,你会进入
frappe@your-site
交互模式。 -
粘贴 Python 代码并执行 在
frappe@your-site
提示符下,复制粘贴你的 方法 1 代码 并回车执行。
方法 2:创建 Python 脚本并执行¶
如果你不想手动输入代码,可以把代码写到 Python 文件中并执行。
步骤¶
- 进入
frappe_backend
容器
docker exec -it frappe_backend bash
- 进入你的 Frappe 站点目录
cd /home/frappe/frappe-bench
- 创建 Python 脚本
nano add_fields.py
-
在
nano
编辑器中粘贴 方法 1 代码,然后Ctrl + X
退出并按Y
保存。 -
执行 Python 脚本
bench --site your-site execute add_fields.add_custom_fields
(your-site
替换为你的站点名称)
方法 3:使用 Frappe REST API(远程执行)¶
如果你的 Docker 运行在远程服务器上,而你不想 SSH 进入容器,你可以通过 REST API 远程创建字段:
curl -X POST https://your-site.com/api/resource/Custom%20Field \
-H "Authorization: token your-api-key:your-api-secret" \
-H "Content-Type: application/json" \
-d '{
"dt": "YourDocType",
"fieldname": "new_field",
"label": "New Field",
"fieldtype": "Data"
}'
(需要在 Frappe 用户设置中 生成 API 密钥)
总结¶
方式 | 适用场景 | 执行方式 |
---|---|---|
方法 1:Frappe Shell | 手动执行小段代码 | bench --site your-site console |
方法 2:Python 脚本 | 需要批量创建字段 | bench --site your-site execute add_fields.add_custom_fields |
方法 3:REST API | 远程调用,无需进入容器 | 发送 curl 或 Python requests 请求 |
如果你想快速测试代码,用 方法 1;如果你要长期使用,可以用 方法 2。
你更倾向哪种方式?需要我帮你调整代码吗?😊