我在MacOS上使用Docker Desktop。
我在主机上启动了一个postgres实例,所以我选择使用network=host
模式。
这是我的Dockerfile
FROM ruby:2.6.6-alpine3.12
RUN apk --update add build-base nodejs tzdata git \
postgresql-dev libxslt-dev libxml2-dev \
yarn libcurl \
&& rm -rf /var/cache/apk/*
RUN gem install bundler
WORKDIR /var/www/root
COPY Gemfile* ./
RUN bundle install
COPY . ./
EXPOSE 3001
CMD bundle exec rails s -b 0.0.0.0 -p 3001
该应用程序可以在本地运行
→ bundle exec rails s -b 0.0.0.0 -p 3001
=> Booting Puma
=> Rails 6.0.3.4 application starting in development
=> Run `rails server --help` for more startup options
Puma starting in single mode...
* Version 4.3.6 (ruby 2.6.6-p146), codename: Mysterious Traveller
* Min threads: 5, max threads: 5
* Environment: development
* Listening on tcp://0.0.0.0:3001
Use Ctrl-C to stop
→ netstat -na|grep 3001
tcp4 0 0 *.3001 *.* LISTEN
但是当我在容器中构建并运行它时,它没有监听端口。我签入了容器,它在容器内进行了侦听,但是没有暴露端口。当我尝试显式公开端口时,它警告我在network=host
模式下不赞成这样做。
→ docker run --rm --network host -it rails-demo
=> Booting Puma
=> Rails 6.0.3.4 application starting in development
=> Run `rails server --help` for more startup options
Puma starting in single mode...
* Version 4.3.6 (ruby 2.6.6-p146), codename: Mysterious Traveller
* Min threads: 5, max threads: 5
* Environment: development
* Listening on tcp://0.0.0.0:3001
Use Ctrl-C to stop
→ netstat -na|grep 3001
<nothing>
→ docker exec -it 69020de7b8b5 ash
/var/www/root # netstat -na|grep 3001
tcp 0 0 0.0.0.0:3001 0.0.0.0:* LISTEN
→ docker run --rm -p 3001:3001 --network host -it rails-demo
WARNING: Published ports are discarded when using host network mode
=> Booting Puma
=> Rails 6.0.3.4 application starting in development
=> Run `rails server --help` for more startup options
Puma starting in single mode...
* Version 4.3.6 (ruby 2.6.6-p146), codename: Mysterious Traveller
* Min threads: 5, max threads: 5
* Environment: development
* Listening on tcp://0.0.0.0:3001
Use Ctrl-C to stop