解决fly.io部署Django项目时创建两台机器的问题
问题
在fly.io上部署Django项目时,系统自动尝试创建2台机器,超出免费额度导致报错,报错信息如下:
Running nameless-paper-520 release_command: python hw10/manage.py migrate
release_command 5683d609cd778e completed successfully
This deployment will:
- create 2 "app" machines
No machines in group app, launching a new machine
Error: error creating a new machine: failed to launch VM: To create more than 1 machine per app please add a payment method.
已尝试在fly.toml的[experimental]段设置max_machines = 1但无效,当前fly.toml配置:
app = "fly-builder-throbbing-forest-5735" primary_region = "waw" console_command = "/code/myapp/manage.py shell" [deploy] release_command = "python myapp/manage.py migrate" [env] ... [http_service] internal_port = 8000 force_https = true auto_stop_machines = true auto_start_machines = true min_machines_running = 0 [[statics]] guest_path = "/code/static" url_prefix = "/static/" [[services]] internal_port = 8000 protocol = "tcp" [services.concurrency] hard_limit = 1 soft_limit = 1 [experimental] max_machines = 1
使用的Dockerfile:
ARG PYTHON_VERSION=3.10 FROM python:${PYTHON_VERSION} ENV PYTHONDONTWRITEBYTECODE 1 ENV PYTHONUNBUFFERED 1 RUN mkdir -p /code WORKDIR /code RUN pip install pipenv COPY Pipfile Pipfile.lock /code/ RUN pipenv install --deploy --system COPY . /code EXPOSE 8000 CMD ["gunicorn", "--bind", ":8000", "--workers", "2", "myapp.wsgi"]
需要修改配置以仅部署1台机器。
解决方案
- 移除重复服务配置:
fly.toml中同时存在[http_service]和[[services]]配置,这会触发fly.io为两种服务部署机器,导致多机器创建。删除整个[[services]]代码段,保留[http_service]即可。 - 设置正确的机器数量限制:
[experimental]下的max_machines已被废弃,需在[http_service]段内添加max_machines_running = 1,修改后的[http_service]配置如下:[http_service] internal_port = 8000 force_https = true auto_stop_machines = true auto_start_machines = true min_machines_running = 0 max_machines_running = 1 - 可选优化:Dockerfile中Gunicorn设置了
--workers 2,针对免费规格的单机器,可调整为--workers 1以匹配资源,修改后的CMD如下:CMD ["gunicorn", "--bind", ":8000", "--workers", "1", "myapp.wsgi"]
内容的提问来源于stack exchange,提问作者Sergio
相关产品推荐
相关产品推荐

