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

Asa*_*bal 1 ruby irb docker docker-compose

这是我的 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 . .
Run Code Online (Sandbox Code Playgroud)

这是我的 docker-compose 文件:

version: '2'
services:
  web:
    build: .
    command: "irb"
    volumes:
      - .:/app
Run Code Online (Sandbox Code Playgroud)

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

我可以通过 docker 使用 irb shell 做什么?

Eze*_*lin 5

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>
Run Code Online (Sandbox Code Playgroud)

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

version: '2'
services:
  web:
    build: .
    command: sh -c 'while true; do sleep 30; done'
    volumes:
      - .:/app
Run Code Online (Sandbox Code Playgroud)

  • @AsadIqbal 因为 `docker-compose up` 不分配 TTY 而 irb 需要一个。 (2认同)