为什么 'irb' shell 在 docker 命令中立即退出

时间:2021-06-09 12:38:47

标签: ruby docker docker-compose irb

这是我的 Dockerfile。

FROM ruby:2.4.0-alpine
RUN mkdir /app
WORKDIR /app
COPY Gemfile ./Gemfile
COPY Gemfile.lock ./Gemfile.lock
RUN bundle install -j 20
COPY . .

这是我的 docker-compose 文件:

version: '2'
services:
  web:
    build: .
    command: "irb"
    volumes:
      - .:/app

我预计 docker-compose up 会打开一个 irb 外壳,但是外壳会立即退出。为什么会退出?

如何通过docker使用irb shell?

1 个答案:

答案 0 :(得分:2)

docker-compose up 不会为 IRB 分配 TTY,因此 IRB 会立即退出。您可以使用 docker-compose rundocker-compose exec 来实现您想要的,它们都分配了一个伪 TTY:

$ docker-compose run web irb
Creating compose-irb_web_run ... done
irb(main):001:0>

或者,如果您修改 docker-compose.yml 中的命令(见下文),运行 docker-compose up 您可以使用 irb 在正在运行的容器内执行 docker-compose exec web irb

version: '2'
services:
  web:
    build: .
    command: sh -c 'while true; do sleep 30; done'
    volumes:
      - .:/app
相关问题