为什么 'irb' shell 在 docker 命令中瞬间退出

Why 'irb' shell exits instantaneously in docker command

这是我的 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 shell,但是 shell 会立即退出。为什么会退出?

如何使用 irb shell 到 docker?

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