您如何强制 rackup 使用端口 0.0.0.0 以便它可以从 docker 容器内部与您的本地环境进行通信?这是我的代码:
# docker-compose.yml
version: '3'
services:
backend:
build: ./ruby-backend
ports:
- "4567:4567"
# ruby-backend/Dockerfile
FROM ruby:2.4.1
COPY Gemfile /app/Gemfile
WORKDIR /app
RUN bundle install
COPY . /app
EXPOSE 4567
CMD ["bundle", "exec", "rackup", "-p", "4567", "--host", "0.0.0.0"
如果我从容器外部运行此命令,它会非常有效。这是我从本地执行此操作时的终端输出:
$ bundle exec rackup -s puma -p 4567 -o "0.0.0.0"
Puma starting in single mode...
* Version 3.11.0 (ruby 2.4.1-p111), codename: Love Song
* Min threads: 0, max threads: 16
* Environment: development
* Listening on tcp://0.0.0.0:4567
Use Ctrl-C to stop
注意正确的主机:tcp://0.0.0.0:4567
. 不幸的是,它在容器中不起作用。这是我使用 docker 执行此操作时的终端输出:
$ dc up backend
Starting devopscourse1_backend_1 ...
Starting devopscourse1_backend_1 ... done
Attaching to devopscourse1_backend_1
backend_1 | Puma starting in single mode...
backend_1 | * Version 3.11.0 (ruby 2.4.1-p111), codename: Love Song
backend_1 | * Min threads: 0, max threads: 16
backend_1 | * Environment: development
backend_1 | * Listening on tcp://localhost:4567
backend_1 | Use Ctrl-C to stop
任何想法我做错了什么?