如何避免Odoo容器因错误退出导致VS Code断开连接?
问题描述
我基于Odoo容器开发,每当主进程终止且VS Code已连接时,必须重新加载才能再次连接。为解决这个问题,我尝试在.devcontainer目录下修改docker-compose.yml,添加命令阻止进程结束后容器关闭:
#....more configs # Overrides default command so things don't shut down after the process ends. command: /bin/sh -c "while sleep 1000; do :; done"
我的devcontainer.json配置如下:
// For format details, see https://aka.ms/devcontainer.json. For config options, see the README at: // https://github.com/microsoft/vscode-dev-containers/tree/v0.245.2/containers/docker-existing-docker-compose // If you want to run as a non-root user in the container, see .devcontainer/docker-compose.yml. { "name": "Existing Docker Compose (Extend)", // Update the 'dockerComposeFile' list if you have more compose files or use different names. // The .devcontainer/docker-compose.yml file contains any overrides you need/want to make. "dockerComposeFile": [ "../docker-compose.yml", "docker-compose.yml" ], // The 'service' property is the name of the service for the container that VS Code should // use. Update this value and .devcontainer/docker-compose.yml to the real service name. "service": "web", // The optional 'workspaceFolder' property is the path VS Code should open by default when // connected. This is typically a file mount in .devcontainer/docker-compose.yml "workspaceFolder": "/home", // Use 'forwardPorts' to make a list of ports inside the container available locally. // "forwardPorts": [], // Uncomment the next line if you want start specific services in your Docker Compose config. // "runServices": [], // Uncomment the next line if you want to keep your containers running after VS Code shuts down. // "shutdownAction": "none", // Uncomment the next line to run commands after the container is created - for example installing curl. // "postCreateCommand": "apt-get update && apt-get install -y curl", // Uncomment to connect as a non-root user if you've added one. See https://aka.ms/vscode-remote/containers/non-root. "remoteUser": "root" }
即便做了以上配置,出现错误时仍需执行docker-compose up重启容器,导致VS Code断开连接。容器内我用了一个类似Node.js nodemon的配置,检测代码变更时重启服务,但遇到致命“无法编译”错误时,服务会完全停止并返回错误码。如何避免这种情况?有没有办法忽略错误码,不用重新加载VS Code?
更新:我的根目录docker-compose.yml示例如下:
version: '3.1' services: web: image: odoo:14.0 depends_on: - db ports: - "8069:8069" volumes: - odoo-web-data:/var/lib/odoo - ./config:/etc/odoo - ./addons:/mnt/extra-addons environment: - PASSWORD_FILE=/run/secrets/postgresql_password secrets: - postgresql_password db: image: postgres:13 environment: - POSTGRES_DB=postgres - POSTGRES_PASSWORD_FILE=/run/secrets/postgresql_password - POSTGRES_USER=odoo - PGDATA=/var/lib/postgresql/data/pgdata volumes: - odoo-db-data:/var/lib/postgresql/data/pgdata secrets: - postgresql_password volumes: odoo-web-data: odoo-db-data: secrets: postgresql_password: file: odoo_pg_pass
解决方案
1. 修改容器启动命令,自动重启Odoo服务
当前的command仅让容器挂起,未处理服务重启逻辑。更新.devcontainer/docker-compose.yml中的command为循环重启脚本:
command: /bin/sh -c "while true; do odoo; echo 'Odoo 退出,错误码 $?,即将重启...'; sleep 2; done"
此命令会在Odoo因错误退出后,自动等待2秒并重启服务,容器始终保持运行,VS Code连接不会断开。
2. 配置VS Code容器关闭策略
在devcontainer.json中取消注释shutdownAction并设置为none,确保VS Code关闭后容器仍持续运行:
"shutdownAction": "none"
3. 可选:使用Supervisor进行进程管理
如果需要更稳定的进程监控,可在容器内安装supervisor工具:
- 进入容器执行安装命令:
apt-get update && apt-get install -y supervisor
- 创建Supervisor配置文件
/etc/supervisor/conf.d/odoo.conf:
[program:odoo] command=odoo autostart=true autorestart=true stderr_logfile=/var/log/odoo.err.log stdout_logfile=/var/log/odoo.out.log user=root
- 修改
.devcontainer/docker-compose.yml的command为:
command: supervisord -n
Supervisor会作为容器主进程,自动监控并重启Odoo服务,即使服务崩溃也能快速恢复。
注意事项
- 所有命令修改仅在
.devcontainer/docker-compose.yml中进行,避免影响根目录的生产环境配置。 - 自动重启逻辑可避免编译错误导致的容器退出,无需重新加载VS Code连接。
内容的提问来源于stack exchange,提问作者Legna
相关产品推荐
相关产品推荐

