Rails开发容器未安装Ruby和Bundler启动报错求助
Rails开发容器启动失败:Ruby/Bundler命令找不到
问题现象
启动容器时出现以下错误:
[2025-03-19T16:39:10.740Z] Error response from daemon: failed to create task for container: failed to create shim task: OCI runtime create failed: runc create failed: unable to start container process: error during container init: exec: "bundle": executable file not found in $PATH: unknown
进入容器后执行相关命令,发现Ruby、Gem、Bundler命令均不存在:
root ➜ / $ bundle bash: bundle: command not found root ➜ / $ gem bash: gem: command not found root ➜ / $ ruby bash: ruby: command not found
配置文件
Dockerfile
ARG RUBY_VERSION=3.4.2 FROM ghcr.io/rails/devcontainer/images/ruby:$RUBY_VERSION # Install packages needed to build gems RUN apt-get update -qq \ && apt-get install --no-install-recommends -y \ build-essential \ libpq-dev \ libvips \ curl \ imagemagick \ postgresql-client \ nodejs \ vim \ && apt-get clean \ && rm -rf /var/lib/apt/lists/*
compose.yml
name: "escrow_safe" services: web: build: dockerfile: .devcontainer/Dockerfile context: .. ports: - "3000:3000" - "12345:12345" command: sleep infinity environment: - ES_DEV_PHONE_OVERRIDE - DEV_EMAIL_PREFIX=${USER} - DATABASE_URL=postgres://root:password@postgres-db/ - EDITOR=vim - ES_DEBUG_ENABLED=true volumes: - ../..:/workspaces:cached depends_on: postgres-db: condition: service_healthy solidq: build: dockerfile: .devcontainer/Dockerfile context: .. command: bundle exec rails solid_queue:start environment: - DATABASE_URL=postgres-db://root:password@postgres-db/ volumes: - ../..:/workspaces:cached # - bundler_gems:/usr/local/bundle depends_on: postgres-db: condition: service_healthy postgres-db: image: postgres:16-alpine environment: POSTGRES_USER: root POSTGRES_PASSWORD: password volumes: - db_data:/var/lib/postgresql/data ports: - "5432:5432" healthcheck: test: pg_isready interval: 2s timeout: 5s retries: 30 volumes: db_data:
devcontainer.json
{ "name": "escrow_safe", "dockerComposeFile": "compose.yaml", "service": "web", // "workspaceFolder": "/rails", "workspaceFolder": "/workspaces/${localWorkspaceFolderBasename}", // Features to add to the dev container. "features": { "ghcr.io/devcontainers/features/github-cli:1": {}, "ghcr.io/rails/devcontainer/features/activestorage": {}, "ghcr.io/rails/devcontainer/features/postgres-client": {}, "ghcr.io/rails/devcontainer/features/bundler-cache:1": {} }, "containerEnv": { "CAPYBARA_SERVER_PORT": "45678", "SELENIUM_HOST": "selenium", "DB_HOST": "postgres" }, // Use 'forwardPorts' to make a list of ports inside the container available locally. "forwardPorts": [ 3000, 5432, 6379 ], // Configure tool-specific properties. // "customizations": {}, // Uncomment to connect as root instead. // "remoteUser": "root", // Use 'postCreateCommand' to run commands after the container is created. "postCreateCommand": "bin/setup" }
解决办法
- 验证基础镜像完整性
直接拉取并运行指定的Ruby基础镜像,确认Ruby是否内置:
docker run --rm -it ghcr.io/rails/devcontainer/images/ruby:3.4.2 ruby -v
若返回Ruby版本信息说明镜像正常;若报错,重新拉取镜像或检查版本号是否正确。
- 排查镜像构建问题
手动执行镜像构建命令,查看过程是否有异常:
# 从项目根目录执行 docker build -t rails-dev-test .devcontainer/
构建完成后运行测试容器检查Ruby命令:
docker run --rm -it rails-dev-test ruby -v
若此处正常,清理旧镜像后重新启动compose:
docker compose down --rmi all docker compose up -d
排除挂载目录影响
暂时注释掉compose.yml中web服务的volumes配置,重启容器后检查Ruby命令是否存在。若恢复正常,说明本地挂载可能意外覆盖了容器内关键路径。排查devcontainer特性冲突
暂时注释掉devcontainer.json中的bundler-cache特性,重新构建容器。部分特性可能修改Ruby环境或PATH变量导致命令丢失。确认用户权限与PATH设置
devcontainer默认使用非root用户(如vscode或rails),避免用root执行命令。若必须用root,需将非root用户的Ruby路径添加到PATH:
# 示例:假设Ruby安装在/home/rails/.rbenv/shims export PATH="/home/rails/.rbenv/shims:$PATH"
也可在devcontainer.json中取消注释"remoteUser": "root",确保基础镜像中Ruby安装在全局路径(如/usr/local/bin)。
内容的提问来源于stack exchange,提问作者James Klein
相关产品推荐
相关产品推荐

