Docker部署Phoenix+Postgres应用:mix phx.server启动失败求助
问题诊断与解决方案
核心问题:mix not found 原因
- 你的Docker镜像未包含Elixir开发环境:
mix是Elixir的开发工具,仅存在于包含完整Elixir SDK的镜像中。如果你的Dockerfile使用了多阶段构建(比如只保留Erlang运行时的生产镜像),或者基于纯Erlang镜像,自然找不到mix命令。 run.sh脚本直接调用mix phx.server,但镜像中没有安装mix依赖的环境。
修复现有配置的步骤
1. 修改Dockerfile,确保包含Elixir开发环境
如果是开发阶段使用,直接基于官方Elixir镜像构建,示例:
# 使用官方Elixir开发镜像,包含mix FROM elixir:1.15-alpine # 安装系统依赖(Phoenix需要的库) RUN apk add --no-cache build-base git nodejs npm postgresql-client # 设置工作目录 WORKDIR /app # 复制mix配置文件,提前安装依赖(利用Docker缓存) COPY mix.exs mix.lock ./ RUN mix local.hex --force && mix local.rebar --force RUN mix deps.get # 复制项目文件 COPY . . # 编译项目(开发阶段可选,也可以在run.sh里执行) RUN mix compile # 复制run.sh并赋予执行权限 COPY run.sh ./ RUN chmod +x run.sh # 暴露端口 EXPOSE 4000 # 执行启动脚本 CMD ["./run.sh"]
2. 调整run.sh脚本(确保环境变量正确)
#!/bin/sh # 设置Phoenix运行环境(开发/生产) export MIX_ENV=dev # 确保数据库迁移(开发阶段常用) mix ecto.create mix ecto.migrate # 启动服务器 mix phx.server
3. docker-compose.yml 调整(确保数据库链接正确)
version: '3.8' services: web: build: . ports: - "4000:4000" depends_on: - db environment: - DATABASE_URL=postgres://postgres:postgres@db:5432/your_app_dev db: image: postgres:15-alpine environment: - POSTGRES_USER=postgres - POSTGRES_PASSWORD=postgres - POSTGRES_DB=your_app_dev volumes: - postgres_data:/var/lib/postgresql/data/ volumes: postgres_data:
替代Docker化方案(兼顾开发与生产)
如果需要同时支持开发和生产,推荐使用多阶段构建:开发阶段用完整Elixir环境,生产阶段用轻量Erlang运行时。但如果你坚持在生产环境用mix phx.server(不推荐,生产环境更适合Phoenix Releases),可以单独维护开发镜像。
附带Nginx反向代理配置(次要需求)
在docker-compose.yml中添加Nginx服务:
services: # ... 保留原有web、db服务 ... nginx: image: nginx:alpine ports: - "80:80" volumes: - ./nginx.conf:/etc/nginx/conf.d/default.conf depends_on: - web
对应的nginx.conf:
server { listen 80; server_name localhost; location / { proxy_pass http://web:4000; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; } }
内容的提问来源于stack exchange,提问作者zdean
相关产品推荐
相关产品推荐

